MindMap Gallery Data Structure Chapter 3 - Stack, Queue and Array
Chapter 3 of "Data Structure" - sorting out the knowledge of stacks, queues and arrays, a picture will help you fully understand the relevant content, and help you improve efficiency through mind mapping. Come and give it a try~
Edited at 2022-11-23 16:06:33Avatar 3 centers on the Sully family, showcasing the internal rift caused by the sacrifice of their eldest son, and their alliance with other tribes on Pandora against the external conflict of the Ashbringers, who adhere to the philosophy of fire and are allied with humans. It explores the grand themes of family, faith, and survival.
This article discusses the Easter eggs and homages in Zootopia 2 that you may have discovered. The main content includes: character and archetype Easter eggs, cinematic universe crossover Easter eggs, animal ecology and behavior references, symbol and metaphor Easter eggs, social satire and brand allusions, and emotional storylines and sequel foreshadowing.
[Zootopia Character Relationship Chart] The idealistic rabbit police officer Judy and the cynical fox conman Nick form a charmingly contrasting duo, rising from street hustlers to become Zootopia police officers!
Avatar 3 centers on the Sully family, showcasing the internal rift caused by the sacrifice of their eldest son, and their alliance with other tribes on Pandora against the external conflict of the Ashbringers, who adhere to the philosophy of fire and are allied with humans. It explores the grand themes of family, faith, and survival.
This article discusses the Easter eggs and homages in Zootopia 2 that you may have discovered. The main content includes: character and archetype Easter eggs, cinematic universe crossover Easter eggs, animal ecology and behavior references, symbol and metaphor Easter eggs, social satire and brand allusions, and emotional storylines and sequel foreshadowing.
[Zootopia Character Relationship Chart] The idealistic rabbit police officer Judy and the cynical fox conman Nick form a charmingly contrasting duo, rising from street hustlers to become Zootopia police officers!
Stacks, Queues and Arrays
stack
basic concept
definition
A stack is a linear list that only allows insertion or deletion operations at one end (last in first out)
Top of stack: The end of the linear table that allows insertion and deletion
Bottom of the stack: the end where insertion and deletion are not allowed
Empty stack: an empty list without any elements
n different elements are pushed onto the stack, and the number of different arrangements of elements popped out of the stack is:
Basic operations
Initstack (&S): Initialize an empty stack s
StackEmpty (S): Determines whether a stack is empty. If stack s is empty, it returns true, otherwise it returns false.
Push (&S, x): Push into the stack. If stack s is not full, add x to make it the top of the new stack.
Pop (&S, &x): Pop the stack. If the stack s is not empty, pop the top element of the stack and return with x.
GetTop (s,&x): Read the top element of the stack. If the stack s is not empty, use x to return the top element of the stack.
Destroystack (&S): Destroy the stack and release the storage space occupied by stack s ("&" indicates a reference call)
The sequential storage structure of the stack (sequential stack)
Implementation of sequential stack
A set of storage units with consecutive addresses is used to store data elements from the bottom of the stack to the top of the stack, and a pointer (top) is attached to indicate the location of the current top element of the stack.
Basic operations of sequential stack
initialization
The stack is empty
push into stack
pop
Read the top element of the stack
shared stack
Taking advantage of the relatively unchanged position of the stack bottom, let two sequential stacks share a one-dimensional array space. Set the bottoms of the two stacks at both ends of the shared space, and the tops of the two stacks extend toward the middle of the space.
Call it short
top0==-1
top1==MaxSize-1
The sentence is full
top1-top0==1
The chain storage structure of the stack (chain stack)
The chain stack has no head node, and Lhead points to the top element of the stack.
queue
basic concept
definition
A queue is a linear list (first in, first out) that only allows insertions at one end of the table and deletions at the other end of the table.
Enqueue: Insert elements to the end of the queue
Dequeue: delete elements at the head of the queue
Basic operations
InitQueue (&Q): Initialize the queue and construct an empty queue Q
QueueEmpty (Q): If the queue is empty, it returns true if the queue Q is empty, otherwise it returns false.
EnQueue (&Q,x): Enter the queue. If the queue Q is not full, add x to make it the new end of the queue.
DeQueue (&Q,&x): Dequeue, if queue Q is not empty, delete the head element of the queue and return with x
GetHead (Q,&x): Read the head element of the queue. If the queue Q is not empty, assign the head element to x
The sequential storage structure of the queue
Queue sequential storage
There is an "overflow" phenomenon
circular queue
Initially: Q. front=Q. rear=0.
The front pointer of the team advances by 1: Q. front=(Q. front 1)%MaxSize.
The rear pointer of the queue advances by 1: Q. rear= (Q. rear 1)%MaxSize.
Queue length (number of elements): (Q. rear MaxSize-Q. front)%MaxSize.
When entering or exiting the queue: the pointers advance by 1 in a clockwise direction (as shown in Figure 3.7)
Queue chain storage structure
Queue chain storage
A linked queue is a singly linked list with both a head pointer and a tail pointer.
Basic operations
initialization
Team is empty
Join the team
Dequeue
deque
A queue that can perform enqueue and dequeue operations on both ends
The elements entered from the front end are arranged in front of the elements entered from the rear end in the queue.
Regardless of whether it is dequeued by the front end or the back end, the elements that come out first are arranged in front of the elements that come out later.
Output-restricted deque: A deque that allows insertions and deletions at one end, but only insertions at the other end
Input-restricted deque: A deque that allows insertions and deletions at one end, but only deletions at the other end
Applications of stacks and queues
stack
Application of stack in bracket matching
An empty stack is initially set, and parentheses are read sequentially.
If it is a right parenthesis, it either eliminates the most urgent expectation placed on the top of the stack, or it is an illegal situation (the sequence of brackets does not match, and the program exits).
If it is a left parenthesis, it will be pushed onto the stack as a new more urgent expectation, which will naturally reduce the urgency of all the original unresolved expectations in the stack by one level. At the end of the algorithm, the stack is empty, otherwise the bracket sequence does not match.
Application of stack in expression evaluation
infix expression → postfix expression
The stack is used to store operators whose operation order is not yet certain. icp represents the priority of the currently scanned operator ch. The priority of this operator after being pushed onto the stack is isp.
conversion process
1||| Add "#" to the beginning and end of the expression
2||| Scan infix expressions starting from left to right
If it is a number, add a suffix expression
If it is an operator
If it is ‘(’, push it onto the stack
If it is ‘)’, add the operators in the stack to the postfix expression in sequence until ‘(’ appears, delete ‘(’ from the stack
If it is an operator other than parentheses, when its priority is higher than the operator on the top of the stack except '(', it will be pushed directly onto the stack. Otherwise, starting from the top of the stack, the operator with a higher priority than the currently processed operator will be popped out in sequence. Operators of equal precedence until one with a lower precedence or an opening parenthesis is encountered.
PS: Only symbols with higher priority can be pushed onto the top of the stack
3||| When the scanned infix expression ends, all operators in the stack are popped off the stack and added to the postfix expression.
postfix expression
Evaluation process
1||| Scan each item of the expression sequentially, and then perform the following operations according to its type
If the item is an operand, push it onto the stack
If the item is operator <op>, the two operands Y and X are continuously ejected from the stack to form the operation instruction
2||| When all terms of the expression have been scanned and processed, the final calculation result is stored on the top of the stack.
Application of stack in recursion
Recursion: applying itself to the definition of a function, procedure, or data structure
Recursive body: recursive expression
Recursive exit: boundary conditions
Essence: Convert the original problem into a smaller problem with the same properties
queue
Application of queue in hierarchical traversal
process
1||| The root node joins the queue.
2||| If the queue is empty (all nodes have been processed), the traversal ends; otherwise, operation ③ is repeated.
3||| The first node in the queue is dequeued and accessed. If it has a left child, add the left child to the team; if it has a right child, add the right child to the team and return to ②.
Application of queues in computer systems
Resolve speed mismatch between host and external device
Set the print buffer, the host will write the data to be output to the buffer in sequence, and the printer will use this to take the data out of the buffer.
Solve resource competition problems caused by multiple users
Arrays and special matrices
array
Definition: A finite sequence composed of n data elements of the same type, which is a generalization of linear tables
storage structure
one-dimensional array
Multidimensional Arrays
row precedence
Column first
Compressed storage of special matrices
definition
Compressed storage: means that only one storage space is allocated for multiple elements with the same value, and no storage space is allocated for zero elements.
Special matrix: refers to a matrix that has many identical matrix elements or zero elements and has a certain regularity in its distribution.
Classification
Symmetric matrix
Store the symmetric matrix A[1...n][1...n] in the one-dimensional array B[(n(n 1)/2], and only store the elements of the lower triangular part (including the diagonal)
triangular matrix
Lower triangular matrix (all elements in the upper triangular area are the same constant)
Similar to a symmetric matrix, after storing the elements in the lower triangular area and the main diagonal, store the constants in the upper triangular area again, and compress the matrix A[1...n][1...n] into a one-dimensional array. B[(n(n 1)/2 1]
upper triangular matrix
Similar to lower triangular matrix
Tridiagonal matrix (banded matrix)
Definition: For any element aij, when |i-j|>1, there is aij=0
The corresponding storage subscript of aij is k=2i j-3
sparse matrix
Definition: The number t of non-zero elements in the matrix is very small relative to the number s of matrix elements, that is, s>>t
Store triples (row index, column index, value) (can be stored in array or cross-linked list)
Matrix can be used as the storage structure of the graph