Queue队列设计

在头尾两端进行操作,先进先出FIFO

队列的接口设计

  1. int size(); //队列元素个数
  2. boolean isEmpty(); //队列是否为空
  3. void clear(); //清空队列元素
  4. void enQueue(E element); //入队
  5. E deQueue(); // 出队
  6. E front(); //获取队列的头元素
  1. package com.wztlink1013.ds.queue;
  2. import com.wztlink1013.ds.queue.list.LinkedList;
  3. import com.wztlink1013.ds.queue.list.List;
  4. public class Queue<E> {
  5. private List<E> list = new LinkedList<>();
  6. public int size() {
  7. return list.size();
  8. }
  9. public boolean isEmpty() {
  10. return list.isEmpty();
  11. }
  12. public void clear() {
  13. list.clear();
  14. }
  15. public void enQueue(E element) {
  16. list.add(element);
  17. }
  18. public E deQueue() {
  19. return list.remove(0);
  20. }
  21. public E front() {
  22. return list.get(0);
  23. }
  24. }

Deque双端队列设计

双端队列是能在头尾两端添加、删除的队列

  1. package com.wztlink1013.ds.queue;
  2. import com.wztlink1013.ds.queue.list.LinkedList;
  3. import com.wztlink1013.ds.queue.list.List;
  4. public class Deque<E> {
  5. private List<E> list = new LinkedList<>();
  6. public int size() {
  7. return list.size();
  8. }
  9. public boolean isEmpty() {
  10. return list.isEmpty();
  11. }
  12. public void clear() {
  13. list.clear();
  14. }
  15. public void enQueueRear(E element) {
  16. list.add(element);
  17. }
  18. public E deQueueFront() {
  19. return list.remove(0);
  20. }
  21. public void enQueueFront(E element) {
  22. list.add(0, element);
  23. }
  24. public E deQueueRear() {
  25. return list.remove(list.size() - 1);
  26. }
  27. public E front() {
  28. return list.get(0);
  29. }
  30. public E rear() {
  31. return list.get(list.size() - 1);
  32. }
  33. }