I. What is a stack?
Stack, also known as Stack, has nothing to do with the heap. In fact, stack and stack are two different concepts. Stack is a linear data structure that can only be inserted and deleted at one end.
The characteristics of stack: LIFO (Last In First Out), or FILO (First In Last Out), we can only operate elements from one end.
Generally speaking, stack has two main operations: one is Push, also known as Push, stack, and the other is POP, or back stack.
We can use arrays to implement a simple stack. In the implementation code of the following stack, for example, integer elements, in Java and other high-level languages, data types can be replaced by objects.
public class Stack {
private int size = 0;
private int[] array;
public Stack() {
this(10);
}
public Stack(int init) {
if (init <= 0) {
init = 10;
}
array = new int[init];
}
/**
* Push
* @param item Stack elements
*/
public void push(int item) {
if (size == array.length) {
array = Arrays.copyOf(array, size*2);
}
array[size++] = item;
}
/**
* Get the top element of the stack, but not out of the stack
* @return Top element of stack
*/
public int peek() {
if (size ==0) {
throw new IndexOutOfBoundsException("The stack is empty.");
}
return array[size -1];
}
/**
* Get out of the stack and get the top element of the stack at the same time
* @return Top element of stack
*/
public int pop() {
int item = peek();
size --;
return item;
}
/**
* Is the stack full?
* @return
*/
public boolean isFull() {
return size == array.length;
}
/**
*Is the stack empty?
* @return
*/
public boolean isEmpty() {
return size ==0;
}
public int size() {
return size;
}
}
public class StackTest {
public static void main(String[] args) {
Stack stack = new Stack(1);
stack.push(1);
stack.push(2);
System.out.println(stack.size());//The number of elements in the stack is 2, and the current array length is 2.
stack.push(3);
System.out.println(stack.size());//The number of elements in the stack is 3, and the current array length is 4
System.out.println(stack.peek());//Get the top element of the stack as 3, but not out of the stack
System.out.println(stack.size());//The number of elements is also 3
System.out.println(stack.pop());//The top element of the stack goes out of the stack and returns 3
System.out.println(stack.pop());//The top element of the stack goes out of the stack and returns 2
System.out.println(stack.size());//After two trips out of the stack, the current number of elements is 1
}
}
Application scenarios for stacks
- Reverse output
Because the stack has the characteristics of first in first out, it is easy to get the reverse output by putting elements into the stack in order and then out of the stack in order.
- Grammar check, symbols appear in pairs
In programming languages, {} []() < > and so on all appear in pairs. When encountering the first half of the symbol, the stack operation (PUSH) is performed. When encountering the second half, the stack matches the top element (PEEK). If it matches, the stack (POP) will be produced. Otherwise, the matching error will occur.
- Numerical conversion
When we do numerical conversion, the final quotient needs to be output in reverse order, which can be easily realized by stack. Of course, stack has many applications, such as the "function stack" that we often hear is when we call methods, the computer will execute PUSH method, record calls, when return ing. At the end of the method, the POP method is executed and the correspondence is completed.
2. What is queue
Queue is also a data structure with limited operation. Insertion can only be done from one end. This is called queue tail. Removal can only be done from the other end. This is called queue head.
Generally speaking, there are two ways to implement queues, arrays and linked lists. Arrays are implemented in two ways: sequential and circular queues. If there is a full queue, when there is a new element to join the queue, but there is no place, at this time, either throw it away, regardless of it, or wait, waiting time is controlled by the program.
Implementing sequential queues:
public class ArrayQueue {
private final Object[] items;
private int head = 0;
private int tail = 0;
/**
* Initialization queue
* @param capacity queue length
*/
public ArrayQueue(int capacity) {
this.items = new Object[capacity];
}
/**
* Join the team
* @param item
* @return
*/
public boolean put(Object item) {
if (head == ((tail + 1) % items.length)) {
//Description team full
return false;
}
items[tail] = item;
tail = (tail + 1) % items.length; //tail tag moved one bit back
return true;
}
/**
* Get the queue head element, not queue
* @return
*/
public Object peek() {
if (head == tail) {
//s Note to Air
return null;
}
return items[head];
}
/**
* Team out
* @return
*/
public Object poll() {
if (head == tail) {
//Explain team empty
return null;
}
Object item = items[head];
items[head] = null;//The useless element assigns a null value, of course, without setting it, because the tag will be overwritten after it moves.
head = (head +1 ) % items.length;//head tag moved one bit back
return item;
}
public boolean isFull() {
return head == (tail + 1) % items.length;
}
public boolean isEmpty() {
return head == tail;
}
public int Size() {
if (tail >= head) {
return tail - head;
}else {
return tail + items.length - head;
}
}
}
public class ArrayQueueTest {
public static void main(String[] args) {
ArrayQueue queue = new ArrayQueue(4);
System.out.println(queue.put("A"));//true
System.out.println(queue.put("B"));//true
System.out.println(queue.put("C"));//true
System.out.println(queue.put("D"));//false
System.out.println(queue.isFull());//true, the queue is full, the queue length is 4, because it's a circular queue, with a maximum of 3 elements stored.
System.out.println(queue.Size());//3
System.out.println(queue.peek());//A Get the team leader element, not the team
System.out.println(queue.poll());//A
System.out.println(queue.poll());//B
System.out.println(queue.poll());//C
System.out.println(queue.isEmpty());//true, the current queue is empty
}
}
Applicable scenarios for queues:
-
Shopping, secondkill
It's easy to think of this scenario, from online shopping to queuing for breakfast. First come first out, first come first go
-
Producer and consumer models
Do you remember the producer-consumer model? The producer is responsible for the production, put it on the conveyor belt after production, and the consumer takes it down. When this model is realized, there is no more than a queue. Several producers add elements to the queue, and several consumers get elements from the queue.