Data Structure Review - Chapter 3: Stack and Queue


Part One: Stack

1. Definition of stack

A linear list that only allows insertion or deletion operations at one end, four words:"Last in, first out"
Insert elements into the stack It is calledpush, and deleting elements from the stack is calledpop .
Schematic diagram of loading and unloading the stack

2. Stack operation

empty boardcondition EnteringOperation Full stackStatus OutputOperation
top = -1 ① top = top+1;
② A[top] = a;
top = Maxsize-1 ① e = A[top];
② top = top-1;
  • "Overflow" phenomenon - when the stack is full, another push to the stack operation occurs The phenomenon of space overflow;
  • "Underflow" phenomenon - when the stack is empty, it will occur if the pop operation is performed again The phenomenon of space overflow.

Part 1 Exercises

  1. If it is known that the push sequence of a stack is 1, 2, 3..., n, its output sequence is p1, p2, p3..., pn, if p1=n, then pi is ( C).
    A.i
    B.n=i
    C.n-i+1 (Algebraic method yields p2 is n-1 and p3 is n-2, then the induction method yields n-i+1)
    D. Uncertain

  2. elements a1, a2, a3, a4 enter the sequence stack in sequence, then the following impossible pop-out sequence is (D).
    A.a4, a3, a2, a1 (all pushed into the stack and then popped out one by one)
    B.a3, a2 , a4, a1 (First push a1-a3 onto the stack, then pop a3 and a2 off the stack, then push a4 onto the stack, and finally pop off the stack in sequence)
    C.a3, a4, a2, a1 (First push a1-a3 onto the stack, then pop a3 off the stack, then push a4 onto the stack, and finally pop off the stack in sequence)
    D. a3, a1 , a4, a2 (Obviously, a1 must be popped after a2 is popped)

  3. Suppose there is a sequential stack S, elements s1, s2, s3, s4, s5, s6 are pushed into the stack in sequence. If the order of the 6 elements being popped out of the stack is s2, s3, s4, s6, s5, s1, then the stack The capacity should be at least (3). -Fill in the blanks
    [Analysis]: The stack can store up to s6, s5, and s1 at the same time


Part 2: Shared Stack

1. Definition of shared stack

The shared stack is also calleddual stack:
The two stacks jointly create a storage space, so that The bottom of one stack is the beginning of the space (subscript 0), and the bottom of the other stack is the end of the space (subscript Maxsize-1). When elements are pushed onto the stack, they are extended from both ends to the middle, so that the remaining space can be used by either stack.
Dual stack structure diagram

2. Operation of shared stack

  • 栈空: top1 = -1; top2 = Maxsize;
  • The stack is full; when top1 = top2-1 (top1+1 = top2 is also acceptable);
  • Operation of stack 1:
    • Entering port: ① top1 = top1+1(moving 1 column); ② A[top1] = val;
    • Output: ① e = A[top1]; ② top1 = top1-1 (1 bottom shift);
  • Operation of stack 2:
    • Enter: ① top2 = top2-1 (move to 2 pages); ② A[top2] = val;
    • Output: ① e = A[top2]; ② top2 = top2+1 (2 bottom movement);

Part 2 Exercises

  1. In order to reduce the possibility of stack overflow, two stacks can share a continuous storage space, and the stack bottoms of the two stacks are set at both ends of this space, so that only when (< a i=1>A) may produceoverflow. A. The tops of the two stacks meet at a certain position in the stack space B. The top of one of the stacks reaches the center point of the stack space a> (an impossible situation, or it is already overflowing at this time) (Because the other stack is not empty, the stack can reach at most The previous position of the bottom, similar to C) D. Neither stack is empty, and the top of one stack reaches the bottom of the other stack C. The tops of the two stacks reach the center point of the stack space at the same time



  2. If the stack is stored in sequential storage, the two stacks now share space V[1,...,m], top[1], top[2] represent the tops of the first and second stacks respectively. The bottom of 1 is at V[1], and the bottom of stack 2 is at V[m]. The condition for the stack to be full is (B).
    A. ∣ | top[2]-top[1] ∣ | = 0
    B.top[1]+1 = top[2]栈栈满的定义)
    C.top[1]+top[2] = m
    D.top[1] = top[2]


Part 3: Chain Stack

1. Definition of chain stack

The stacks and shared stacks mentioned above are all implemented using sequential storage. If The stack implemented by chain storage is called a chain stack.
In fact, the stack implemented by the linked list imposes a "last in, first out" constraint on the linked list. Its structure is the same as the general one. The linked lists are exactly the same.
Chain stack structure diagram

2. Operation of chain stack

It should be noted that in the previous sequential stack, top refers to the array subscript, while in the chain stack, top refers to the pointer.

  • Stack empty: top = null;
  • Stack full: Doesn’t exist!
  • Push onto the stack: (actually the head insertion method of the linked list)
    ① p->next = top;
    ② top = p;
  • 出栈:
    ① e = top->data;(p = top;)
    ② top = top->next;(free(p);)

Part 3 Exercises

  1. Insert an x ​​node into a link stack whose top pointer is top, then execute (C).
    A.top->next=x
    B.x->next=top->next; top->next=x
    C.x->next=top;top=x
    D.x->next=top;top=top->next

  2. If a linked list is used as the storage structure of the stack, during the stack operation (D).
    A. It is necessary to determine whether the stack is full. (The chain stack does not become full unless the physical storage space is seriously insufficient; and the stack is full during the push operation. Judgment)
    B. Do not make any judgment on the stack (Not making judgment is not robust enough)
    C. Judge the stack elements Type (meaningless)
    D. It is necessary to determine whether the stack is empty


Summary of Parts One to Three

1. Comparison between sequence stack and chain stack

(1) Time performance comparison
The time complexity of the basic operation algorithms of sequential stack and chain stack is O(1)< /span> There is no stack full problem. The stack will only be full when there is no available space in the memory, but each element requires a pointer field, resulting in structural overhead. Chain stack must determine a fixed length, so there are limitations on the number of stored elements and problems of wasted space. the sequence stack Initially
(2) Space performance comparison.

  • General conclusion: When the number of elementsof the stack changes greatly during use (that is, it is difficult to determine the fixed length), use a chain Stack is better; otherwise, sequential stack should be used.
  • Classic example: Compared with the sequential stack, the chain stack has an obvious advantage (A).
    A. The stack is usually not full.
    B. The stack is usually empty.
    C. Insertion The operation is easier to implement
    D. The deletion operation is easier to implement

2. Application of stack

  • Application of stack: mainly in bracket matching, expression evaluation, and recursionPrefix expressions and Suffix expression.
  • Classic example: infix expression A − ( B + C / D ) ∗ E A-(B+C/D)*E A(B+C/D)E's subsequent form is (D).
    A. A B − C + D / E ∗ AB-C+D/E* ABC+D/E
    B. A B C + D / − E ∗ ABC+D/-E* ABC+D/E
    C. A B C D / E ∗ + − ABCD/E*+- ABCD/E+
    D. A B C D / + E ∗ − ABCD/+E*- ABCD/+AND

[Analysis]Infix expression is our common operation symbol in the middle of the formula< /span> a>: operator symbol is at the front or back end of the expression , that is, the arithmetic expression in which the Postfix expression and prefix expressions’s arithmetic expression, the more difficult thing is how to convert it into

Add parentheses: Enclose each operation in parentheses according to the operator priority. Original parentheses>multiplication and division>addition Subtract, that is, the above formula becomes: ( A − ( ( B + ( C / D ) ) ∗ E ) ) (A-((B+(C/D))*E)) (A((B+(C/D))E));
transfer code − ( A ∗ ( + ( B / ( C D ) ) E ) ) - (A*(+(B/(CD))E)) :Transfer to the parentheses before or after the parentheses, immediately change the order:(A(+(B/ (CD))E ( A ( ( B ( C D ) / ) + E ) ∗ ) − (A((B(CD )/)+E)*)- or))(A((B(CD)/)+E))− a i=9> − A ∗ + B / C D E -A*+B/CDE
A+B/CD E, a certain expression: A B C D / + E ∗ − ABCD/+E*- ABCD/+AND

3. Exercises related to stack application

  1. When changing a recursive algorithm to the corresponding non-recursive algorithm, you usually need to use (A)
    A. Stack (Stack is usually used, but other methods can also be used)
    B. Queue
    C. Circular queue< /span>
    D. Priority queue

  2. Arithmetic expression A + B ∗ C − D / E A+B*C-D/E A+BCD/E 4>D). A. − A + B ∗ C / D E -A+B*C/DE
    A+BC/DE
    B. − A + B ∗ C D / E -A+B*CD/E A+BCD/E
    C. − + ∗ A B C / D E -+*ABC/DE +ABC/D E
    D. − + A ∗ B C / D E -+A*BC/DE +ABC/DE
    【分析】① ( ( A + ( B ∗ C ) ) − ( D / E ) ) ((A+(B*C))-(D/E)) ((A+(BC))(D/E)) − ( + ( A ∗ ( B C ) ) / ( D E ) ) -(+(A*(BC))/(DE)) ;② (+(A(BC))/( − + A ∗ B C / D E -+A*BC/DE ;③ ))ED+ABC/DE

  3. Middle table expression A ∗ ( B + C ) / ( D − E + F ) A*(B+C)/(D-E+F) A(B+C)/(DAND+F)after displaying (C ).
    A. A ∗ B + C / D − E + F A*B+C/D-E+F AB+C/DAND+F
    B. A B + C + D / E − F + AB+C+D/E-F+ AB+C+D/EF+
    C. A B C + ∗ D E − F + / ABC+*DE-F+/ ABC+DEF+/
    D. A B C D E F ∗ + / − + ABCDEF*+/-+ ABCDEF+/
    【分析】① ( ( A ∗ ( B + C ) ) / ( ( D − E ) + F ) ) ((A*(B+C))/((D-E)+F)) ((A(B+C))/((DE)+F));② ( ( A ( B C ) + ) ∗ ( ( D E ) − F ) + ) / ((A(BC)+)*((DE)-F)+)/ ((A(BC))+((DE)F)+)/;③ A B C + ∗ D E − F + / ABC+*DE-F+/ ABC+DEF+/


Part 4: Queue

1. Definition of queue

only allows insertion at one end of the table and deletion at the other end of the table. Four words: "First in, first out"
Inserting elements into the queue is called enqueue< a i=4>, deleting an element from the queue is calleddequeuing.
Queue structure diagram
[Note] A simple queue may have false overflow or false full a> problem, that is: when the head element of a full queue is dequeued, although there is a vacancy in front of the head pointer, the entry must be from the end of the queue, and at this time the tail pointer has reached At the end, the queue cannot be moved back (the queue is judged to be full), resulting in the inability to join the queue.
Schematic diagram of false full situation

2. Circular queue

In order to solve the problem of false overflow or false full, the sequential queue is generally composed of circular queue. The circular queue connects the head and tail of the queue. Of course, this is just a logical structure, but it is still Sequential storage, the implementation method is that when the queue tail pointer reaches the maximum position of the queue, it will return to the starting position of the queue if it moves forward.

3. Operation of circular queue

vacancycondition EntryOperation Full teamStatus outputoperation
front = rear ① queue[rear] = A;
② rear = (rear+1)%Maxsize;
(rear+1)%Maxsize = front ① e = queue[front];
② front = (front+1)%Maxsize;
  • In the above operation, the remainder of Maxsize is used to return +1 to the head of the queue when it reaches the end of the queue.

Part 4 Exercises

  1. Assume that the elements of the circular queue are stored in the array A[m], and its head and tail pointers are front and rear respectively, then the number of elements in the current queue is (A[Analysis] Because rear cannot exceed front, so between the two The maximum difference is m; add m to the difference between the two. If the difference is positive, the remainder operation will eliminate the added m; if the difference is negative, the remainder operation will not take effect. This formula is equivalent to ( m + r e a r ) − f r o n t (m+rear)-front D.(rear-front)%m C.(front- rear+m)%m B.rear-front+1
    A.(rear-front+m)%m).



    (m+rear)front

  2. Assume that the initial state of stack S and queue Q is empty. Elements a, b, c, d, e, and f pass through stack S in sequence. After one element is popped out of the stack, it enters queue Q. If 6 elements are popped out of the queue The sequence is a, c, f, e, d, b, then the capacity of stack S should be at least (C).
    A.6
    B.5
    C.4
    D.3
    [Analysis] When the stack contains the most b, d, e, f

  3. If an array of size 6 is used to implement a circular queue, and the current values ​​of rear and front are 0 and 3 respectively, when an element is deleted from the queue and two more elements are added, the values ​​of rear and front The values ​​are (B).
    A.1 and 5
    B.2 and 4
    C.4 and 2
    D.5 and 1


Part 5: Deque

1. Definition of double-ended queue

Refers to a queue that allows both ends to perform enqueue and dequeue operations (both ends are not restricted)

  • Output-restricted deque: allows insertions and deletions at one end, but only insertions atthe other end The deque of is called an output-limited deque.
  • Input-restricted deque: allows insertions and deletions at one end, but only deletions atthe other end The deque of is called an input-restricted deque.

2. Operation of double-ended queue

The same as a normal queue, both ends can be enqueued and dequeued without restrictions.

  • Please note that if both ends are enqueued, the element enqueued first will be surrounded in the middle of the double-ended queue. At this time, the queue is dequeued from a certain end and becomes the entry part of the exit end"last in, first out". The part of entering the queue at the entrance end is "first in, first out". (Output limited)
  • Similarly, if you only enter the queue from one end and dequeue from both ends, it becomesthe part that exits the queue at the entrance end "Last in, first out", and the part that exits the queue at the exit end is "first in, first out". (Input restricted)

[Classic example] If 1234 is used as the input sequence of a double-ended queue, the output sequence that can neither be obtained by the input-limited double-ended queue nor the output-limited double-ended queue is (< /span> D.1234 C.4132 B.4231 A.4213). B




Problem solving process


Part Six: Chain Team

1. Definition of chain team

is actually a singly linked list with both a head pointer and a tail pointer, which is equivalent to us applying a "first in first out" to the linked list< a i=2> constraint.
Schematic diagram of the structure of the chain team

2. Operation of chain team

It should be noted that in the previous sequential storage queue, front and rear refer to the array subscript, while in the chain queue, both are pointers.

  • 空队:front = null;rear = null;
  • Full team: It doesn’t exist!
  • Enter the queue: (actually the tail insertion method of the linked list)
    ① rear->next = p;
    ② rear = rear->next;
  • 出队:
    ① e = front->data;(p = front;)
    ② front = front->next;(free(p);)

Part 6 Exercises

  1. If a singly linked list is used to represent the queue, (B) should be used.
    A. Acyclic linked list with tail pointer (Going back to the head of the queue to implement the dequeue operation is too time-consuming ×) (Going to the end of the queue to join the queue is too time-consuming ×) D. Circular linked list with head pointer (Similarly Nodes need to be moved to the end of the queue one by one, unless it is a double circular linked list ×)


  2. The condition for determining that "the chain queue of the leading node is empty" is (A).
    A.Q.front == NULL (The head pointer is used to dequeue, generally pointing to the head element of the team)
    B.Q.rear == NULL (the tail pointer is used for enqueueing and originally points to null)
    D.Q.front == Q.rear (possibly It contains one node)
    D.Q.front != Q.rear


Summary of Parts Four to Six

1. Comparison between circular queue and chain queue

(1) Time performance comparison
The algorithms for basic operations of circular queues and chain queues both require constant timeO(1),

  • Circular queueThe space is applied for in advance and will not be released during use.
  • Chained queueThere will also be some time overhead every time you apply for and release a node,

Ifentry and exit frequently, there is still a slight difference between the two - best Selecting circular queue can save the time overhead of applying for and releasing nodes.

(2) Space performance comparison
The circular queue must have a fixed length (sequential storage), so there are problems with the number of storage elements and wasted space.

2. Queue application

  • Applications of queues: Mainly in hierarchical traversal, in addition queues can be used in computer systems to solve the problemThe speed mismatch between the host and external devicesproblems andresource competitionproblems caused by multiple users.
  • Classic example: When solving the problem of speed mismatch between the computer host and the printer, a print data buffer is usually set up, so that the host writes the data to be output into the buffer in sequence, and the printer reads from the buffer. Take out the data and print it. The buffer should be a (B) structure.
    A. Stack
    B. Queue
    C. Array
    D. Linear table< /span>

Summary of stacks and queues

  • The stack accounts for a large part of the data structure, while the queue accounts for a large part of the operating system.
    Comparison between stack and queue

Guess you like

Origin blog.csdn.net/qq_50571974/article/details/126614669