Detailed Explanation of Stack Queue Deque in Java Set Framework
- 2021-12-04 10:01:32
- OfStack
1. Stack
1.1 Introduction
The Stack stack is a subclass of Vector, which implements a standard LIFO stack. At the bottom of it is an array.
The stack only defines the default constructor, which is used to create an empty stack. In addition to all the methods defined by Vector, the stack also defines its own methods.
1.2 Common Methods
方法 | 描述 |
---|---|
CODE_TAG_REPLACE_MARK_0 | 压栈 |
CODE_TAG_REPLACE_MARK_1 | 出栈 |
CODE_TAG_REPLACE_MARK_2 | 查看栈顶元素,不删除 |
CODE_TAG_REPLACE_MARK_3 | 判断栈是否为空 |
Note: The following examples are all taken out separately from one code, and there is a logical relationship between the top and bottom
Example 1: Constructing a stack with an element as shaping with Stack
Stack<Integer> stack = new Stack<>();
Example 2: Stack pressing
stack.push(1);
stack.push(2);
stack.push(3);
// The results are: [1, 2, 3]
Example 3: View the top element of the stack without deleting it
System.out.println(stack.peek());
System.out.println(stack);
// The results are: 3 And [1, 2, 3]
Example 4: Out of Stack
System.out.println(stack.pop());
System.out.println(stack);
// The results are: 3 And [1, 2]
Example 5: Determining whether the stack is empty
System.out.println(stack.empty());
// The results are: false
2. Queue
2.1 Introduction
The Queue queue is a special linear table that allows only delete operations at the front end of the table and insert operations at the back end of the table.
The LinkedList class implements the Queue interface, so we can use LinkedList as Queue.
2.2 Common Methods
方法 | 描述 |
---|---|
CODE_TAG_REPLACE_MARK_4 | 入队列(出现错误返回特殊值) |
CODE_TAG_REPLACE_MARK_5 | 入队列(出现错误抛异常) |
CODE_TAG_REPLACE_MARK_6 | 出队列(出现错误返回特殊值) |
CODE_TAG_REPLACE_MARK_7 | 出队列(出现错误抛异常) |
CODE_TAG_REPLACE_MARK_2 | 得到队首元素,不删除(出现错误返回特殊值) |
CODE_TAG_REPLACE_MARK_9 | 得到队首元素,不删除(出现错误抛异常) |
CODE_TAG_REPLACE_MARK_10 | 判断队列是否为空 |
Note: Next, use LinkedList to demonstrate how to use queues, as long as you grasp the first-in, first-out principle. The following examples are all taken out separately from 1 code, and there is a logical relationship between the top and bottom
Example 1: Creating a queue with integer elements using LinkedList
LinkedList<Integer> linkedList = new LinkedList<>();
Example 2: Queue
linkedList.offer(1);
linkedList.offer(2);
linkedList.offer(3);
Example 3: Out of the queue
System.out.println(linkedList.poll());
// The results are: 1
Example 4: Get the first element of the queue without deleting it
System.out.println(linkedList.peek());
// The results are: 2
Example 5: Determining whether the queue is empty
System.out.println(linkedList.isEmpty());
// The results are: false
3. Deque
3.1 Introduction
Two-ended queue refers to a queue that allows both ends to enter and leave the queue. Elements can leave and enter the team from the head of the team, and can also leave and enter the team from the end of the team
The LinkedList class implements the Deque interface, so we can use LinkedList as Deque.
3.2 Common Methods
方法 | 描述 |
---|---|
CODE_TAG_REPLACE_MARK_11 | 从队头入队 |
CODE_TAG_REPLACE_MARK_12 | 从队尾入队 |
CODE_TAG_REPLACE_MARK_13 | 从队头出队 |
CODE_TAG_REPLACE_MARK_14 | 从队尾出队 |
CODE_TAG_REPLACE_MARK_15 | 得到队头元素,不删除 |
CODE_TAG_REPLACE_MARK_16 | 得到队尾元素,不删除 |
Note: Next, use LinkedList to demonstrate how to use queues, as long as you grasp the first-in, first-out principle. The following examples are all taken out separately from 1 code, and there is a logical relationship between the top and bottom
Example 1: Creating a queue with integer elements using LinkedList
stack.push(1);
stack.push(2);
stack.push(3);
// The results are: [1, 2, 3]
0
Example 2: Enter the team from the head of the team
stack.push(1);
stack.push(2);
stack.push(3);
// The results are: [1, 2, 3]
1
Example 3: Enter the team from the end of the team
stack.push(1);
stack.push(2);
stack.push(3);
// The results are: [1, 2, 3]
2
Example 4: Leaving the queue from the head of the queue
System.out.println(linkedList.pollFirst());
// The results are: 3
Example 5: Leaving the queue from the end of the queue
stack.push(1);
stack.push(2);
stack.push(3);
// The results are: [1, 2, 3]
4
Example 6: Get the queue header element without deleting it
stack.push(1);
stack.push(2);
stack.push(3);
// The results are: [1, 2, 3]
5
Example 7: Get the queue header element without deleting it
stack.push(1);
stack.push(2);
stack.push(3);
// The results are: [1, 2, 3]
6