[Java data structure] detailed explanation of stack and queue

catalogue

1, Stack

1. Concept

2. Stack operation

3. Implementation of stack

① Stack

② Out of stack

③ Get stack top element

④ Determine whether the stack is empty

  4. Implement mystack

2, Queue

1. Concept

  2. Implementation of queue

① Join the team

② Out of the team

③ Get team leader element

  3. Implement myqueue

1, Stack

1. Concept

In our software applications, stack, a last in first out data structure, is very common. For example, when you use the browser to surf the Internet, no matter what browser has a "back" button. After you click it, you can load the viewed web pages in reverse order.

 

Many similar software, such as word, Photoshop and other document or image editing software, also use stack to realize the operation. Of course, the specific implementation of different software will be very different, but the principle is actually the same.

A stack is a linear table that is restricted to insertion and deletion only at the end of the table

Stack: a special linear table that allows insertion and deletion of elements only at a fixed end. One end for data insertion and deletion is called the top of the stack, and the other end is called the bottom of the stack. The data elements in the stack follow the principle of Last In First Out LIFO (Last In First Out).

2. Stack operation

Stack pressing: the stack insertion operation is called stack entering / stack pressing / stack entering, and the input data is at the top of the stack.

Stack out: stack deletion is called stack out. The output data is at the top of the stack.

3. Implementation of stack

① Stack

 

 public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        int ret = stack.push(4);
        System.out.println(ret);
    }

② Out of stack

  public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        int ret1 = stack.pop();
        int ret2 = stack.pop();
        System.out.println(ret1);
        System.out.println(ret2);
    }

③ Get stack top element

 public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        int ret1 = stack.pop();
        int ret2 = stack.pop();
        int ret3 = stack.peek();
        System.out.println(ret1);
        System.out.println(ret2);
        System.out.println(ret3);
    }

④ Determine whether the stack is empty

  public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        int ret1 = stack.pop();
        int ret2 = stack.pop();
        int ret3 = stack.peek();
        System.out.println(ret1);
        System.out.println(ret2);
        System.out.println(ret3);
        stack.pop();
        boolean flag = stack.empty();
        System.out.println(flag);
    }

  4. Implement mystack

public class MyStack<T> {
    private T[] elem;//array
    private int top;//Currently, the subscript of the data element can be stored - "stack top pointer"

    public MyStack() {
        this.elem = (T[])new Object[10];
    }

    /**
     * Stack operation
     * @param item Stacked elements
     */
    public void push(T item) {
        //1. Determine whether the current stack is full
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        //2,elem[top] = item  top++;
        this.elem[this.top++] = item;
    }

    public boolean isFull(){
        return this.elem.length == this.top;
    }

    /**
     * Out of stack
     * @return Out of stack elements
     */
    public T pop() {
        if(empty()) {
            throw new UnsupportedOperationException("Stack is empty!");
        }
        T ret = this.elem[this.top-1];
        this.top--;//Really changed the value of top
        return ret;
    }

    /**
     * Get the top element of the stack, but do not delete it
     * @return
     */
    public T peek() {
        if(empty()) {
            throw new UnsupportedOperationException("Stack is empty!");
        }
        //this.top--;// Really changed the value of top
        return this.elem[this.top-1];
    }
    public boolean empty(){
        return this.top == 0;
    }
}
public static void main(String[] args) {
        MyStack<Integer> myStack = new MyStack<>();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        System.out.println(myStack.peek());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.empty());
        System.out.println("============================");
        MyStack<String> myStack2 = new MyStack<>();
        myStack2.push("hello");
        myStack2.push("word");
        myStack2.push("thank");
        System.out.println(myStack2.peek());
        System.out.println(myStack2.pop());
        System.out.println(myStack2.pop());
        System.out.println(myStack2.pop());
        System.out.println(myStack2.empty());

    }

2, Queue

1. Concept

For customer service calls such as China Mobile, China Unicom and China Telecom, there are always a few customer service personnel compared with customers. When all customer service personnel are busy, customers will be asked to wait until a customer service personnel is free, so that the customer who waits first can connect the phone. Here, all customers who currently dial customer service calls are queued.

In the operating system and customer service system, a data structure is applied to realize the first in first out queuing function just mentioned, which is queue.

A queue is a linear table that only allows insertion at one end and deletion at the other

Queue: a special linear table that only allows inserting data at one end and deleting data at the other end. The queue has a first in first out FIFO(First In First Out) into the queue: the end at which inserting is called Tail/Rear out of the queue: the end at which deleting is called Head/Front

  2. Implementation of queue

① Join the team

 public static void main(String[] args) {
        Deque<Integer> queue = new LinkedList<>();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        
    }

② Out of the team

  public static void main(String[] args) {
        Deque<Integer> queue = new LinkedList<>();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        System.out.println(queue.poll());
        System.out.println(queue.poll());

    }

③ Get team leader element

public static void main(String[] args) {
        Deque<Integer> queue = new LinkedList<>();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println("-----------------");
        System.out.println(queue.peek());
    }

  3. Implement myqueue

class Node {
    private int val;
    private Node next;
    public int getVal() {
        return val;
    }
    public void setVal(int val) {
        this.val = val;
    }
    public Node getNext() {
        return next;
    }
    public void setNext(Node next) {
        this.next = next;
    }
    public Node(int val) {
        this.val = val;
    }
}
public class MyQueue {
    private Node first;
    private Node last;
    //Join the team
    public void offer(int val) {
        //The tail interpolation method needs to judge whether it is the first insertion
        Node node = new Node(val);
        if(this.first == null) {
            this.first = node;
            this.last = node;
        }else {
            this.last.setNext(node);//last.next = node;
            this.last = node;
        }
    }
    //Out of the team
    public int poll() {
        //1 judge whether it is empty
        if(isEmpty()) {
            throw new UnsupportedOperationException("The queue is empty!");
        }
        //this.first = this.first.next;
        int ret = this.first.getVal();
        this.first = this.first.getNext();
        return ret;
    }
    //Get the team header element but don't delete it
    public int peek() {
        //Do not move first
        if(isEmpty()) {
            throw new UnsupportedOperationException("The queue is empty!");
        }
        return this.first.getVal();
    }
    //Is the queue empty
    public boolean isEmpty() {
        return this.first == null;
    }
}
 public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.offer(1);
        myQueue.offer(2);
        myQueue.offer(3);
        System.out.println(myQueue.peek());
        System.out.println(myQueue.poll());
        System.out.println(myQueue.poll());
        System.out.println(myQueue.poll());
        System.out.println(myQueue.isEmpty());
       
    }

 

Tags: Java data structure linked list

Posted on Sun, 28 Nov 2021 15:38:55 -0500 by RedRasper