Stack, Heap and Queue in Data Structures

In the process of learning data structures, we often see stacks, heaps, and queues. So what is the relationship between these three? Let me explain it to you today.

Stack : Also known as stack , it is a linear table with limited operations. Insertion and deletion of elements are only allowed at the top of the stack. The top of the stack is the low bit, and the bottom of the stack is the high bit. When there are no elements in the stack, it is called an empty stack. The stack follows the first-in, last-out principle.

To put it simply, the insertion and deletion of the stack is like stacking boxes. We take out the objects placed at the bottom of the box (objects placed earlier). We first need to remove the objects on top of them (objects placed earlier). late objects).
The most important operations in the stack are push (add) and pop (delete)
push: Put an element at the top of the stack
pop: Remove an element at the top of the stack and return the deleted element

Insert image description here Java code implementation (anyone who is interested can run it yourself)

import java.util.Stack;

public class Test  {
    
    
    public static void main(String[] args) {
    
    
        //定义一个栈 stack 存放字符串
        Stack<String> stack = new Stack<>();
        stack.push("VIP1");
        stack.push("VIP2");
        System.out.println("栈初始化:"+stack);
        //往栈中添加一个VIP3
        stack.push("VIP3");
        System.out.println("添加后的栈:"+stack);
        //对栈删除一个元素
        String pop = stack.pop();
        System.out.println("被删除的元素:"+pop);
        System.out.println("删除后的栈:"+stack);
    }
}

Insert image description here
Heap : A heap is a sorted tree data structure in which each node has a value. Usually what we call the data structure of a heap refers to a complete binary tree, which is also a binary heap . The characteristic of a heap is that the root node has the smallest (or largest) value, and the two subtrees of the root node are also a heap . If the values ​​of a node in the heap are greater than or equal to the values ​​of its child nodes, we call it a big heap. If the values ​​of the nodes in the heap are less than or equal to the values ​​of its child nodes, we call it a small heap.
Due to this characteristic of the heap, it is often used to implement priority queues

Since the heap is too long, a blog will be dedicated to the details of the heap later.

Queue : A queue is a linear list with limited operations. The special thing is that it only allows deletion operations at the front end of the queue, and insertion operations at the back end of the queue . When there are no elements in the queue, it is called an empty queue. Inserting a queue element into the queue is called enqueuing , and deleting a queue element from the queue is called dequeuing . Unlike the stack's first-in-last-out principle, the queue's principle is first-in-first-out .

Insert image description here
In Java, LinkedList implements the Queue interface, so LinkedList can be used to represent a linked list
(of course, it can also be implemented using an array and two stacks)

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


public class Test  {
    
    
    public static void main(String[] args) {
    
    
        //定义一个链表
        Queue<String> queue = new LinkedList<String>();
        //添加元素
        queue.offer("a");
        queue.offer("b");
        queue.offer("c");
        //初始化的链表
        for(String q : queue){
    
    
            System.out.println(q);
        }
        System.out.println("新增一个元素d:"+queue.offer("d"));
        System.out.println("遍历链表");
        for(String q : queue){
    
    
            System.out.println(q);
        }
        System.out.println("删除一个元素:"+queue.poll()); //返回第一个元素,并在队列中删除
        System.out.println("遍历链表");
        for(String q : queue){
    
    
            System.out.println(q);
        }
    }
}

Insert image description here

Guess you like

Origin blog.csdn.net/qq_43649799/article/details/124029908