数据结构——队列
一、理解队列
1. 队列的定义
队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。
队列中数据的特点:先进先出,后进后出。
2. 队列的操作
允许插入的一端称为队尾,允许删除的一端称为队头。
我们可以将其想象成一个链表,队头就是这个链表中的第一个节点,队尾就是这个链表中的最后一个节点,然而我们只能对这个链表进行 尾插、头删 操作。
二、测试 Java 实现队列及其方法
程序清单1:
public class Test1 {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
queue.offer(2);//尾插
queue.offer(4);
queue.offer(6);
queue.offer(8);
System.out.println(queue);
System.out.println(queue.peek());//访问队列头元素
System.out.println(queue);
System.out.println(queue.poll());//删除队列头元素
System.out.println(queue);
}
}
输出结果:
三、通过单链表实现自己的队列
第一个 java 文件放的是两个类
一个类实现节点 Node,另一个类实现队列 Queue
程序清单2:
class Node{
public int val;
public Node next;
public Node(int val){
this.val = val;
}
}
public class Queue {
public Node head;
public Node last;
//尾插
public void offer(int val){
Node node = new Node(val);
if(this.head == null){
head = node;
last = node;
return;
}
last.next = node;
last = last.next;
}
//头删
public int poll(){
if(head == null){
throw new RuntimeException("队列为空");
}
int headVal = head.val;
head = head.next;
return headVal;
}
public int peek(){
if(head == null){
throw new RuntimeException("队列为空");
}
return head.val;
}
public void print(){
Node cur = head;
while(cur != null){
System.out.print(cur.val+" ");
cur = cur.next;
}
System.out.println();
}
}
第二个 java 文件放的是一个类 Test
目的是使用主函数来测试队列的一系列功能
程序清单3:
public class Test {
public static void main(String[] args) {
Queue queue = new Queue();
queue.offer(2);
queue.offer(4);
queue.offer(6);
queue.offer(8);
queue.print();
System.out.println(queue.peek());
queue.print();
System.out.println(queue.poll());
queue.print();
System.out.println(queue.poll());
queue.print();
}
}
输出结果:
四、理解双端队列
双端队列可以实现两端都能进行出队和入队操作
1. 测试 Java 实现双端队列及其方法
程序清单4:
public class Test4 {
public static void main(String[] args) {
Deque<Integer> deque = new LinkedList<>();
deque.offer(2); //默认尾插
deque.offerLast(4);
deque.offerFirst(6);//头插
deque.offerLast(8);//尾插
System.out.println(deque);
System.out.println(deque.peek());//默认访问队列头元素
System.out.println(deque.peekFirst());//访问队列头元素
System.out.println(deque.peekLast());//访问队列尾元素
}
}
输出结果:
程序清单5:
public class Test5 {
public static void main(String[] args) {
Deque<Integer> deque = new LinkedList<>();
deque.offer(2); //默认尾插
deque.offerLast(4);
deque.offerFirst(6);//头插
deque.offerLast(8);//尾插
System.out.println(deque);
System.out.println(deque.poll());//默认删除队列头元素
System.out.println(deque);
System.out.println(deque.pollFirst());//删除队列头元素
System.out.println(deque);
System.out.println(deque.pollLast());//删除队列尾元素
System.out.println(deque);
}
}
输出结果:
五、环形队列
环形队列底层是通过带环的顺序表实现的队列。这里我们要时刻清楚队头和队尾的位置!
六、通过OJ题深入理解队列
题目一 循环队列
程序清单6:
public class MyCircularQueue {
public int[] elem;
public int front; //队头下标
public int rear; //队尾下标
public MyCircularQueue(int k) {
elem = new int[k+1];
}
//入列
public boolean enQueue(int value) {
//队列满了,无法添加元素
if(isFull() == true){
return false;
}
//队列没满,正常添加
elem[rear] = value;
rear = (rear+1) % elem.length;
return true;
}
//出列
public boolean deQueue() {
//队列空了,无法删除元素
if(isEmpty() == true){
return false;
}
//队列没空,正常删除
front = (front+1) % elem.length;
return true;
}
//拿到队列的首元素
public int Front() {
if(isEmpty() == true){
return -1;
}
return elem[front];
}
//拿到队列的末尾元素
public int Rear() {
if (isEmpty() == true) {
return -1;
}
int lastRear = (rear + elem.length - 1) % elem.length;
return elem[lastRear];
}
public boolean isEmpty() {
if(front == rear){
return true;
}
return false;
}
public boolean isFull() {
int nextRear = (rear+1) % elem.length;
if(nextRear == front){
return true;
}
return false;
}
}
结论:
① 当元素入队的时候,让 rear 开始跑,在 front 和 rear 重合的情况下,那么这时的队列已满。
② 当元素出队的时候,让 front 开始跑,在 front 和 rear 重合的情况下,那么这时的队列已空。
思路:
① 元素入队:先判断队列是否满了,若满了,就返回 false;若没满,就正常添加,正常添加的时候,我们要考虑到顺序表的最后一个元素,比如说:数组的最后一个下标为 3,那么既然是循环队列,我们要让元素放在数组下标为0的位置,此时我们就不能使用 3 + 1 = 4。
rear = (rear+1) % elem.length; //true
//rear = rear + 1 //false
② 元素出队:先判断队列是否为空,若为空,就返回 false;若不为空,就正常删除元素,正常删除的时候,我们只需要让 front 遍历数组下标即可,不用管上一个元素还存在数组中,因为下一次元素入队的时候,就会将未删除的元素直接覆盖。
③ 拿到队首元素:正常情况下,直接返回 front 下标的元素。
④ 拿到队尾元素:正常情况下,我们要让 rear 下标退一格,这里也要注意不能使数组越界。
int lastRear = (rear + elem.length - 1) % elem.length;
return elem[lastRear];
⑤ 另外在做 leetcode 题目的时候,我们要让数组的长度为 k+1,为什么这么做呢?因为我们通过 front 和 rear 实现的时候,这会造成 rear 总是表示的是队尾的下一个下标位,因为我们总是通过 rear + 1 来判断队列元素是不是已满的问题,如果满了,就不往里面放元素了,所以假设当数组长度为4时,我们只能放3个元素进行,其余一个空位就是用来判断队列是否已满。
public MyCircularQueue(int k) {
elem = new int[k+1];
}
题目二 利用队列实现栈
程序清单7:
class MyStack {
Queue<Integer> queue1 = new LinkedList<>();
Queue<Integer> queue2 = new LinkedList<>();
public MyStack() {
}
//往栈中放数据
public void push(int x) {
if(queue1.isEmpty() == true){
queue2.offer(x);
}else if(queue2.isEmpty() == true){
queue1.offer(x);
}else {
queue1.offer(x);
}
}
//从栈顶拿数据
public int pop() {
if(empty() == true){
return -1;
}
int size1 = queue1.size()-1;
int size2 = queue2.size()-1;
if(queue2.size() == 0){
for (int i = 0; i < size1; i++) {
queue2.offer(queue1.poll());
}
return queue1.poll();
}else {
for (int i = 0; i < size2; i++) {
queue1.offer(queue2.poll());
}
}
return queue2.poll();
}
//访问栈顶的元素
public int top() {
if(empty() == true){
return -1;
}
int midVal = 0;
int size1 = queue1.size();
int size2 = queue2.size();
if(queue2.size() == 0){
for (int i = 0; i < size1; i++) {
midVal = queue1.poll();
queue2.offer(midVal);
}
return midVal;
}else {
for (int i = 0; i < size2; i++) {
midVal = queue2.poll();
queue1.offer(midVal);
}
return midVal;
}
}
public boolean empty() {
if(queue1.isEmpty() == true && queue2.isEmpty() == true){
return true;
}
return false;
}
}
题目三 利用栈实现队列
程序清单8:
class MyQueue {
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
public MyQueue() {
}
public void push(int x) {
stack1.push(x);
}
public int pop() {
if(empty()){
return -1;
}
int size1 = stack1.size();
if(stack2.size() == 0){
for (int i = 0; i < size1; i++) {
stack2.push(stack1.pop());
}
return stack2.pop();
}else {
return stack2.pop();
}
}
public int peek() {
if(empty()){
return -1;
}
int size1 = stack1.size();
if(stack2.size() == 0){
for (int i = 0; i < size1; i++) {
stack2.push(stack1.pop());
}
return stack2.peek();
}else {
return stack2.peek();
}
}
public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}
}
利用栈实现队列其实与利用队列实现栈的思想是一样的。
我们始终要清楚一件事情:
栈:先进后出
队列:先进先出
我简明地说一下思路:
① 模拟入队:利用栈 stack1 实现,而且 stack1 只用来实现入队。
② 模拟出队:利用栈 stack2 实现,而且 stack2 只用来实现出队。
当 stack2 不为空,就直接拿栈顶元素;
当 stack2 为空,就将 stack1 一直 pop 进入 stack2,然后再拿 stack2 的栈顶元素即可。