Java core API (6) - queue, stack

Posted by t2birkey on Mon, 13 Apr 2020 17:23:01 +0200

I. queue

Queue is a common data structure, which can be regarded as a special linear table. Queue restricts the access to the linear table: you can only add (offer) elements from one end of the linear table, and take (poll) elements from the other end.
Queues follow the FIFO First Input First Output principle.
The JDK provides a Queue interface, which is implemented by LinkedList (the reason for LinkedList to implement Queue is that Queue often needs to be added and deleted, while LinkedList is more efficient in this respect.)
The main methods in the Queue interface are as follows:
Boolean offer (E); -- adds an object to the end of the pair, and returns true if it is added successfully.
E poll(); -- removes and returns an element from the pair header.
E peek(); -- returns the element of the first pair (but not deleted).

package day05;

import java.util.LinkedList;
import java.util.Queue;

/**
 * queue
 * Queues can also hold a set of elements, but access elements must follow the principle of first in, first out.
 * @author xxx
 *
 */
public class QueueDemo {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<String>(); 

        /*
         *boolean offer(E e)
         *Join operation, add a new element to the end of the team.
         */
        queue.offer("one");
        queue.offer("two");
        queue.offer("three");
        queue.offer("four");
        System.out.println(queue);

        /*
         * E poll()
         * To get an element from the head of the queue, the element is deleted from the queue.
         */
        String str = queue.poll();
        System.out.println(str);
        System.out.println(queue);

        /*
         * E peek()
         * Reference team head element, but do not make team operation
         */
        str = queue.peek();
        System.out.println(str);
        System.out.println(queue);

        /*
         * Note: queue.size() will change. As more and more elements are taken out, the value of size will be smaller and smaller.
         * So, we need to traverse backwards here, and if we are going to traverse backwards, we will not traverse completely.
         */
        /*for (int i = queue.size();i > 0;i--) {
            str = queue.poll();
            System.out.println("Element: "+ str);
        }*/

        while (queue.size() > 0) {
            str = queue.poll();
            System.out.println("Elements:"+str);
        }
    }
}

Two, stack

Deque is the sub interface of Queue, which defines the so-called "two terminal Queue", that is, from both ends of the Queue, you can enter (offer) and leave (poll), and LinkedList implements the interface.
If Deque is limited to only entering and leaving the queue from one end, the data structure of "stack" can be realized. For the stack, the input is called push, and the output is called pop.
The stack follows the principle of FILO First Input Last Output.

package day05;

import java.util.Deque;
import java.util.LinkedList;

/**
 * Stack
 * Store a set of elements, but access elements must follow the principle of first in first out.
 * The stack is usually used in order to implement such functions as backward.
 * @author xxx
 *
 */
public class StackDemo {
    public static void main(String[] args) {
        Deque<String> stack = new LinkedList<String>();

        /*
         * void push(E e)
         * Stack in operation, the last element to stack is at the top of the stack (position of the first element)
         */
        stack.push("one");
        stack.push("two");
        stack.push("three");
        stack.push("four");
        System.out.println(stack);

        /*
         * Stack operation
         * E pop()
         */
        String str = stack.pop();
        System.out.println(str);
        System.out.println(stack);

        str = stack.peek();
        System.out.println(str);
        System.out.println(stack);

        while (stack.size() > 0) {
            str = stack.pop();
            System.out.println(str);
        }
        System.out.println(stack);
    }
}

Topics: Java JDK