Article Directory
1. The relationship between the two
1. Both Quene and Deque inherit from Collection, and Deque is a subinterface of Quene.
public interface Deque<E> extends Queue<E>
Quene is a first-in, first-out one-way queue, and Deque is a two-way queue.
2. The following table lists the interfaces corresponding to Deque and Queue:
3. The following table lists the interfaces corresponding to Deque and Stack:
Two, Quene interface
1. Quene is a sub-interface of the collection framework Collection . It is a common data structure. Quene has a direct subclass PriorityQuene. Quene is a commonly used data structure. Quene can be regarded as a special linear table. The structure follows the first-in-first-out principle. In Java, LinkedList implements the Quene interface, because LinkedList performs insertion and deletion operations more efficiently.
poll():将队首的元素删除,并返回该元素。
peek():返回队首的元素,但不进行删除操作。
offer():将元素添加到队尾,如果成功,则返回true。
2. The specific code operation of Quene
package com.edu.leetcode;
import java.util.*;
public class QueneDemo {
public static void testQuene(){
Queue<String> qu = new LinkedList<>();
qu.add("苹果");
qu.add("华为");
System.out.println("原始队列:");
System.out.println(qu);
System.out.println("通过add往队尾添加元素:");
qu.add("OPPO");
System.out.println(qu);
System.out.println("通过offer往队列尾添加元素:");
qu.offer("vivo");
System.out.println(qu);
System.out.println("使用remove删除队列头元素:");
qu.remove();
System.out.println(qu);
System.out.println("使用poll删除对列头元素:");
qu.poll();
System.out.println(qu);
}
public static void main(String[] args){
QueneDemo.testQuene();
}
}
The execution results are as follows:
// An highlighted block
原始队列:
[苹果, 华为]
通过add往队尾添加元素:
[苹果, 华为, OPPO]
通过offer往队列尾添加元素:
[苹果, 华为, OPPO, vivo]
使用remove删除队列头元素:
[华为, OPPO, vivo]
使用poll删除对列头元素:
[OPPO, vivo]
3. Deque interface (two-way queue)
1. Double-ended queue (Deque), Quene is a sub-interface. A two-way queue means that elements at both ends of the queue can enter the queue (offer) and leave the queue (poll). If the Deque is limited to only enter from one end Team (push) and out of the team (pop), you can limit the data structure of the stack. For the stack, there is push into the stack, and the principle of first in, last out is followed.
It can be used as both a stack and a queue
add()\offer(e):将元素增加到队列的末尾,如果成功,返回true。
remove()\poll():将元素从队列的队首删除。
element()\peek():返回队首的元素,但不进行删除
栈:
push(e):入栈,添加到队首
pop(e):出栈,删除队首元素
peek():返回栈首元素,但不进行删除
Reference link:
[1] Reference link .