[Data Structure]——Exercises related to stack and queue

Question Type 1 (Basic Concepts of Stacks and Queues)

1. Both stack and queue are ().
A. Linear structure of sequential storage
B. Nonlinear structure of chain storage
C. Linear structure that limits access points
D. Nonlinear structure that limits access points

Analysis: (C)
It is a linear list that only allows insertion or deletion operations at one end. It is a special linear list. It has the same logical structure as the queue and both belong to it. The difference lies in the different processing of the elements. The stack 线性结构follows The principle is 先进后出(FILO)that the last element is taken out first, and it is a linear structure with restricted access points.

队列Like the stack, it is also a special linear list with limited operations. It has the same logical structure as the stack and belongs to both. The difference is that the 线性结构elements are processed differently. The queue only allows insertion at one end and only allows insertion at the other end. Deletion is performed at one end. The queue follows the principle 先进先出(FIFO)that the element that enters the queue first leaves first. It is also a linear structure with restricted access points, which is the same as queuing in daily life.

2. What stacks and queues have in common is ().
A. Both are first in , first out
B. Both are first in last out
C. Only insertion and deletion of elements are allowed at the endpoints
D. There is no common ground

Analysis: (C)
What stacks and queues have in common is that they are special linear tables that only allow insertion or deletion operations at one end. The main difference is that the restrictions on insertion and deletion operations are different.

Question Type 2 (Synthesis of Stack and Queue)

1. Assume that the initial state of the stack S and the queue Q is empty. The elements a, b, c, d, e, and f pass through the stack S in sequence. After an element is popped off the stack, it enters the queue Q. If the sequence of 6 elements leaving the queue is a, c, f, e, d, b, then the capacity of stack S should be at least ().
A, 3
B, 4
C, 5
D, 6

Analysis: (B)
The stack is first in, last out, and the queue is first in, first out.
If the capacity is 3, to meet the condition, first element a passes through the stack S, and then enters the queue Q, and the
dequeue is {a}; after b and c pass through the stack S, c is first dequeued and passed through the queue, and the dequeue is obtained as
{a, c}; if the third dequeue element is f at this time, then the stack should be {b, d, e, f} from bottom to top, so the capacity of stack S should be at least 4.

Question Type 3 (Shared Stack)

1. The benefits of using a shared stack are ().
A. Reduce access time and reduce the possibility of overflow
B. Save storage space and reduce the possibility of overflow
C. Reduce access time and reduce the possibility of underflow
D. Save storage space and reduce the possibility of underflow

Analysis: (B)
Using a shared stack can make more efficient use of storage space and reduce the possibility of overflow. By letting two sequential stacks share a one-dimensional array space, the bottoms of the two sequential stacks are set at both ends of the array space. , the top pointers of the two stacks point to the top element of the stack, as shown below:
Insert image description here
When top1=-1, the sequential stack 1 is empty, when top2=MaxSize, the sequential stack 2 is empty, and when the two stack top pointers Adjacent, that is, when top2-top1=1, the shared stack is full at this time.

Question Type 4 (empty and full judgment of circular queue)

1. Assume that the maximum capacity of the circular queue is m, the head pointer of the queue is front, and the tail pointer of the queue is rear, then the condition for the queue to be full is ().
A. (rear+1)%m == front
B. rear == front
C. rear+1 == front
D. (rear-1)%m == front

Analysis: (A)
Let MaxSize be the maximum capacity of the circular queue, (Q.rear+1)%MaxSize == Q.front, that is, when the remainder of the queue tail pointer plus MaxSize is equal to the head pointer, the queue is full at this time, as follows:
Insert image description here
The current team head pointer Q.front=1, Q.rear=0, that is, (Q.rear+1)%MaxSize=(0+1)%7=1%7=1, which is equal to Q.front=1, so this When the queue is full, the queue head pointer is at the next position of the queue tail pointer.

Question Type 5 (Circular linked list represents queue)

1. Use a circular singly linked list to represent the queue. Let the length of the queue be n. If only the tail pointer is set, the time complexity of dequeuing and entering the queue is () respectively.
A.O(1),O(1)
B.O(1),O(n)
C.O(n),O(1)
D.O(n),O(n)

Analysis: (A)
The circular singly linked list means that the queue is equivalent to a ring. Since only the tail pointer is set, the queue is dequeued directly. The time complexity of dequeuing is O(1); because the head and tail of the queue are connected, the next result of the tail pointer is The point is the head of the queue, and the time complexity of joining the queue is also O(1).

2. Use a circular singly linked list to represent the queue. Let the length of the queue be n. If only the head pointer is set, the time complexity of dequeuing and entering the queue is () respectively.
A. O(1),O(1)
B. O(n),O(n)
C. O(1),O(n)
D. O(n),O(1)

Analysis: (D)
A circular singly linked list means that the queue is equivalent to a ring. Since only the head pointer is set, when dequeuing, the pointer needs to go from the head to the end of the queue and pass through n nodes to reach the end of the queue. Therefore, the time complexity of dequeuing is O(n ); since there is a head pointer when joining the queue, you can directly join the queue, and the time complexity of dequeuing is O(1).

Question Type 6 (Storage of Circular Queue)

1. It is known that the length of the array storing the circular queue is 21. If the values ​​of the head pointer and tail pointer of the current queue are 9 and 3 respectively, then the current length of the queue is ().
A, 6
B, 12
C, 15
D, 18

Analysis: (C)
The number of data elements can be obtained by subtracting the head pointer from the tail pointer plus the value of MaxSize and the modulus of MaxSize. That is, (Q.rear-Q.front+MaxSize)%MaxSizethe code is as follows:

//循环队列的数据元素个数
bool NumQueue(SqQueue Q){
    
    
	if(Q.front==Q.rear)	//若队列为空,则报错 
		return false;
	int num=(Q.rear-Q.front+MaxSize)%MaxSize;
	printf("当前循环队列的数据元素个数为:%d\n",num);
}

In the question, Q.front=9, Q.rear=3, MaxSize=21, so (Q.rear-Q.front+MaxSize)%MaxSize=(3-9+21)%21=15%21=15.

Question Type 7 (entry and dequeue of circular queue)

1. The circular queue is stored in the array A[0,...,m], and front and rear are used to represent the head and tail of the queue respectively. The operation when entering the queue is ().
A. rear=rear+1
B. rear=(rear+1) mod (m-1)
C. rear=(rear+1) mod m
D. rear=(rear+1) mod (m+1)

Analysis: (D)
The enqueue operation is for Q.rear. The enqueue code is implemented through remainder operation. The tail pointer of the queue is increased by 1, that is, Q.rear=(Q.rear+1)%MaxSizeno matter what the previous (Q.rear+1) is, it is the same as MaxSize (for example, MaxSize=5) The remainder results can only be 0, 1, 2, 3, 4, that is, each movement of the tail pointer Q.rear increases by 1.
Insert image description here
So in the question, the operation when joining the queue is rear=(rear+1) mod (m+1).

2. The circular queue is stored in the array A[0,...,m], and front and rear are used to represent the head and tail of the queue respectively. The operation when dequeuing is ().
A. front=front+1
B. front=(front+1) mod (m-1)
C. front=(front+1) mod m
D. front=(front+1) mod (m+1)

Analysis: (D)
The dequeue code is still implemented through the remainder operation. The queue head pointer is added by 1. That is, Q.front=(Q.front+1)%MaxSizewe know that no matter what the previous (Q.front+1) is, it will be the remainder of MaxSize (for example, MaxSize=5). The only possible values ​​are 0, 1, 2, 3, and 4, that is, each movement of the head pointer Q.front increases by 1.
Insert image description here
So in the question, the operation when dequeuing is front=(front+1) mod (m+1).

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 added to the queue, two elements are deleted, and three elements are added, then one element is deleted. At this time, the values ​​of rear and front are () respectively.
A, 1 and 3
B, 0 and 4
C, 4 and 0
D, 3 and 1

Analysis: (C)
The initial values ​​of rear and front are 0 and 3 respectively. Add one element and target the rear pointer, so rear=1; delete two elements and target the front pointer, so front=1; add three more elements. , targeting the rear pointer, so rear=4; deleting another element targets the front pointer, so front=0. So rear=4, front=0.

Question type 8 (chained storage structure represents circular queue)

1. () is required when inserting and deleting operations using a queue with a chained storage structure.
A. Only the head pointer is modified
. B. Only the tail pointer is modified.
C. Both the head and tail pointers are modified.
D. Both the head and tail pointers may be modified.

Analysis: (D)
When inserting an element into the chained queue, if the queue is not empty, only the tail pointer needs to be modified; when the queue is empty, both the head and tail pointers need to be modified.

When an element is deleted from the chained queue, if the queue is not empty, it only needs to be deleted from the head, that is, the head pointer is modified; and if there is only one element in the queue, the tail pointer also needs to be modified, that is, rear=front.
Insert image description here

Question Type 9 (Application of Stack and Queue)

1. When executing a function, its local variables are generally stored using ().
A. Tree structure
B. Static linked list
C. Stack structure
D. Queue structure

Analysis:(C)

2. (Multiple choice) Stack applications include the following ().
A. Bracket matching
B. Expression evaluation
C. Page replacement algorithm
D. Recursion
E. Base conversion
F. Caching mechanism

Analysis: ( A、B、D、E、F)
Except for the page replacement algorithm, which is a queue application, the others are all stack applications.

3. In order to solve the problem of speed mismatch between the computer host and the printer, a print data buffer is usually set up. The host writes the data to be output into the buffer in sequence, and the printer takes out the data from the buffer in sequence. The logical structure of this buffer should be ().
A. Stack
B. Queue
C. Tree
D. Graph

Analysis: (B)
Since the host will write the data to be output into the buffer in sequence, the order needs to be guaranteed. At the same time, in order to output the data in order, the first-in-first-out feature, that is, the queue, should be used.

Guess you like

Origin blog.csdn.net/qq_43085848/article/details/132181428