[Data structure] stack, queue and hash table

Reference: "Comic Algorithm - Xiao Hui's Algorithmic Journey"

directory

1. stack

1.1 Definition of stack

1.2 Basic operation of the stack

1.3 Application of the stack

2. queue

2.1 Definition of Queue

2.2 Basic operation of the queue

2.3 Application of Queue

3. Hash table


Stacks and queues are logical structures, and their physical implementation can be done using either arrays or linked lists.

1. stack

1.1 Definition of stack

The stack is a linear data structure. The elements in the stack can only be entered first and then exited . The location where the earliest entered element is stored is called the bottom of the stack , and the location where the last entered element is stored is called the top of the stack. The implementation of stack with array and linked list is as follows:

1.2 Basic operation of the stack

1. Push into the stack: The push operation is to put new elements into the stack, and only allow elements to be placed from the top side of the stack, and the position of the new element will become the new top of the stack.

2. Popping: The popping operation is to pop the element from the stack. Only the element at the top of the stack is allowed to pop, and the previous element of the popped element will become the top of the stack.

——>The time complexity of stacking and popping is O(1).

1.3 Application of the stack

The output order of the stack is opposite to the input order, so the stack is usually used to trace the "history", that is, to trace the "history" upstream. For example, to implement recursive logic, you can use a stack instead, because the stack can trace back the call chain of the method.

 Another well-known application scenario of the stack is breadcrumb navigation, which allows users to easily go back to the previous or higher level page when browsing the page.

2. queue

2.1 Definition of Queue

The queue is also a linear data structure. The elements in the queue can only be first in , first out . The exit segment of the queue is called the head of the queue , and the entry segment of the queue is called the tail of the queue. The implementation of queues with arrays and linked lists is as follows:

2.2 Basic operation of the queue

1. Joining the queue: Joining the queue is to put new elements into the queue, and only allow elements to be placed at the end of the queue, and the next position of the new element will become the new tail.

2. Dequeue: Dequeue means to remove elements from the queue, only allow elements to be removed from the queue head side, and the element after the dequeue element will become the new queue head.

——> The time complexity of entering and exiting the team is O(1).

2.3 Application of Queue

The output order of the queue is the same as the input order, so the queue is usually used for "history" playback, that is, to repeat the history in the order of history. For example, in multi-threading, the waiting queue for competing for fair locks is to determine the order of threads in the queue according to the order of access.

3. Hash table

A hash table is also called a hash table. This data structure provides a mapping relationship between a key (key) and a value (value). As long as a key is given, the matching value can be found, and the time complexity is close to O(1).

Guess you like

Origin blog.csdn.net/weixin_45922730/article/details/128861682