Java implementation of stack, queue, and heap

One, the stack

Features: First in, first out (FILO).
Code implementation: Java officially recommends using the Deque (double-ended queue) interface to implement the stack. Don't use Stack class (Vector class subclass) to achieve low efficiency.
Insert picture description here
Deque has two commonly used implementation classes, namely ArrayDeque (bottom array) and LinkedList (bottom linked list).
As for how to choose between these two? I think both have advantages and disadvantages.

  • LinkedList involves the creation and destruction of a Node every time an element is inserted or deleted. The efficiency is low, and the storage structure of LinkedList is not continuous, and the access cannot make good use of the CPU cache. You can insert null.
  • ArrayDeque is more efficient when inserting or deleting an element because the bottom layer is an array. But when the data is fully assembled, it needs to be copied again. Cannot insert null. It is
    Introduction to ArrayDeque
    recommended to use ArrayDeque. When the size of the stack is known, it is best to pass in the size when creating it. The default size (without passing parameters) is 16, the minimum is 8, and other values ​​are passed in. The size is greater than the value of 2 to the power of n, and the maximum is 2 30 2^{30}230.
public void testStack() {
    
    
        Deque<Integer> stack = new ArrayDeque<>();

        //入栈
        stack.push(1);
        stack.push(2);

        //返回栈顶元素,但不出栈
        Integer a1 = stack.peek();
        System.out.println(a1);//2

        //出栈
        Integer a2 = stack.pop();
        System.out.println(a2);//2
        Integer a3 = stack.pop();
        System.out.println(a3);//1
        
    }

Second, the queue

Features: First In First Out (FIFO)

Code implementation: Use the Queue interface, or use the Deque interface. Deque inherited Queue. The implementation class is selected as above.

public void test1() {
    
    
        Queue<Integer> queue = new ArrayDeque<>();

        //入队
        queue.offer(1);
        queue.offer(2);

        //出队
        Integer a1 = queue.poll();
        System.out.println(a1);//1

        //查看队首元素,但不出队
        Integer a2 = queue.peek();
        System.out.println(a2);//2
    }

3. Heap

Features:

  1. The value of any non-leaf node in the heap is not less than (large top heap) or not greater than (small top heap) the value of its child nodes.
  2. The heap is always a complete binary tree.
    In summary, meet the big top pile heap[i]>=heap[2*i+1]and heap[i]>=heap[2(i+1)]small top pile heap[i]<=heap[2*i+1]andheap[i]<=heap[2(i+1)]

Complete binary tree: If the depth of the binary tree is k, the number of nodes in each layer except for the kth layer has reached the maximum number, and all the nodes of the kth layer are contiguously concentrated on the leftmost side, which is a complete binary tree. As shown below:

Insert picture description here
Code implementation: Use the PriorityQueue class to implement, pay attention to the stored elements must be natural sorting (Comparable) or custom sorting (Comparator). It is not possible to store null values. The default is small top heap. The bottom layer is an array, and the default size is 11; expansion: if the array size is less than 64, it will be expanded twice when it is full, otherwise it will be expanded by 50%.

@Test
    public void test1() {
    
    
        //小顶堆
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        heap.addAll(Arrays.asList(6, 3, 8, 9, 1));
        System.out.println(heap);//[1, 3, 8, 9, 6]

        //入堆
        heap.offer(2);
        System.out.println(heap);//[1, 3, 2, 9, 6, 8]

        //出堆
        Integer poll = heap.poll();
        System.out.println(poll);//1

        //查看堆顶元素但不出堆
        Integer peek = heap.peek();
        System.out.println(peek);//2

        //大顶堆
        PriorityQueue<Integer> heap2 = new PriorityQueue<>((o1, o2) -> -Integer.compare(o1, o2));
        heap2.addAll(Arrays.asList(6, 3, 8, 9, 1));
        System.out.println(heap2);//[9, 8, 6, 3, 1]
    }

Guess you like

Origin blog.csdn.net/AmorFati1996/article/details/111869074