队列
只允许在一端插入数据操作,在另一端进行删除数据操作的特殊线性表;进行插入操作的一端称为队尾(入队列),进行删除操作的一端称为队头(出队列);队列具有先进先出(FIFO)的特性。
三种实现方式
- 顺序存储实现方式
- 链式存储实现方式
- 基于LinkedList实现队列结构
顺序存储实现方式
public class MyQueue { private Object[] data; private int maxSize = 10; private int front; private int rear; // 初始化队列 public MyQueue(int maxSize) { if (maxSize >= 0) { this.maxSize = maxSize; data = new Object[maxSize]; front = rear = 0; } else { throw new RuntimeException("初始化队列大小不能小于0"); } } // 判空 public boolean empty() { return rear == front ? true : false; } // 入队 public boolean add(Object obj) { if (rear == maxSize) { throw new RuntimeException("队满"); } else { data[rear++] = obj; return true; } } // 出队 public Object poll() { if (front == rear) { throw new RuntimeException("队空"); } else { Object value = data[front]; data[front++] = null; return value; } } // 查看队首元素 public Object peek() { if (front == rear) { throw new RuntimeException("队空"); } else { return data[front]; } } // 返回队列长度 public int length() { return rear - front; } public static void main(String[] args) { MyQueue queue = new MyQueue(5); queue.add(1); queue.add(2); queue.add(3); queue.add(4); queue.add(5); System.out.println(queue.length()); System.out.println(queue.empty()); System.out.println(queue.peek()); System.out.println(queue.poll()); System.out.println(queue.peek()); } }
链式存储实现方式
public class MyLinkedQueue { // 定义节点的内部类 public class Node { Node next; Object data; public Node(Object data) { super(); this.data = data; } } private Node rear; private Node front; private int maxSize = 10; private int size; // 初始化队列 public MyLinkedQueue(int maxSize) { if (maxSize >= 0) { this.maxSize = maxSize; rear = front = null; size = 0; } else { throw new RuntimeException("初始化队列大小不能小于0"); } } // 判空 public boolean empty() { return size == 0 ? true : false; } // 入队 public boolean add(Object data) { if (size == maxSize) { throw new RuntimeException("队满"); } else if (empty()) { front = new Node(data); rear = front; size++; return true; } else { Node node = new Node(data); rear.next = node; rear = node; size++; return true; } } // 出队 public Object poll() { if (empty()) { throw new RuntimeException("队空"); } else { Node node = front; front = front.next; node.next = null; size--; return node.data; } } // 返回队首元素 public Object peek() { if (empty()) { throw new RuntimeException("队空"); } else { return front.data; } } // 返回长度 public int length() { return size; } public static void main(String[] args) { MyLinkedQueue queue = new MyLinkedQueue(5); queue.add(1); queue.add(2); queue.add(3); queue.add(4); queue.add(5); System.out.println(queue.length()); System.out.println(queue.empty()); System.out.println(queue.peek()); System.out.println(queue.poll()); System.out.println(queue.peek()); } }
基于LinkedList实现队列结构
import java.util.LinkedList; import java.util.Queue; public class MyListQueue { private Queue<Object> queue = new LinkedList<>(); // 将元素入队,如果当前没有可用的空间,则抛出 IllegalStateException public boolean add(Object data) { queue.add(data); return true; } // 将元素入队,此方法通常要优于 add(E),后者可能无法插入元素,而只是抛出一个异常。 public boolean offer(Object data) { queue.offer(data); return true; } // 取队首元素 public Object element() { return queue.element(); } // 取队首元素方法二 public Object peek() { return queue.peek(); } // 获取并移除此队列的队首元素 public Object poll() { return queue.poll(); } // 获取并移除此队列的队首元素方法二 public Object remove() { return queue.remove(); } // 获取并移除此队列的队首元素方法三,指定移除元素 public boolean remove1(Object data) { return queue.remove(data); } // 判空 public boolean empty() { return queue.isEmpty(); } // 打印 public String toString() { return queue.toString(); } public static void main(String[] args) { MyListQueue queue = new MyListQueue(); queue.add(1); queue.add(2); queue.add(3); queue.offer(4); queue.offer(5); queue.offer(6); System.out.println(queue.toString()); System.out.println(queue.empty()); System.out.println(queue.element()); System.out.println(queue.peek()); System.out.println(queue.toString()); System.out.println(queue.poll()); System.out.println(queue.remove()); System.out.println(queue.remove1(5)); System.out.println(queue.toString()); } }