Data Structure Chapter 3 Stack and Queue Jobs (corrected)

True or False

1-1
If the input sequence of a stack is 1, 2, 3,..., N, and the first element of the output sequence is i, then the j-th output element is j−i−1.

T F

The stack is a first-in-last-out data structure, which means that if the stacking order is 123, the stacking order is 321. The input sequence of the stack in the title is 1, 2, 3,..., n, which is an increasing sequence with an arithmetic difference of 1, so the stacking order should be n, n-1..., 3, 2, 1, that is, etc. Decreasing sequence with a difference of 1. Then when the first element of the output sequence is i, use the arithmetic sequence formula an = a1+(n-1) d to know that the j-th element should be aj = i+(j-1) (-1) = i- j+1.

Insert picture description here
1-2
Run the following operations on a stack S: Push(S,1), Push(S,2), Pop(S), Push(S,3), Pop(S), Pop(S). The output sequence must be {1, 2, 3}.

Translation: Run the following operations on the stack: Push(S,1), Push(S,2), Pop(S), Push(S,3), Pop(S), Pop(S). The output sequence must be {1,2,3}.

T F

{ 2, 3, 1}

1-3
Given the input sequence onto a stack as {1, 2, 3, …, N}. If the first output is i, then the j-th output must be j−i−1.

Translation: The input sequence on a given stack is {1,2,3,...N}. If the first output is i, then the jth output must be j-i-1.

T F

Same as 1-1

1-4
If keys are pushed onto a stack in the order {1, 2, 3, 4, 5}, then it is impossible to obtain the output sequence {3, 4, 1, 2, 5}.

Translation: If you push the keys onto the stack in the order of {1,2,3,4,5}, it is impossible to get the output sequence {3,4,1,2,5}.

T F

1-5
If the input sequence of a stack is {1, 2, 3, 4, 5}, it is impossible to get a pop sequence like {3, 4, 1, 2, 5}.

T F

Same as 1-4

1-7
If keys are pushed onto a stack in the order abcde, then it’s impossible to obtain the output sequence cdabe.

Translation: If you press the keys into the stack in the "abcde" sequence, it is impossible to get the output sequence "cdabe".

T F

Same as 1-4

1-8
If keys are pushed onto a stack in the order abcde, then it’s impossible to obtain the output sequence cedab.

Translation: If you press the keys into the stack in the "abcde" sequence, it is impossible to get the output sequence "cedab".

T F

cedba

1-9
Given that the pushing sequence of a stack is { 1, 2, ⋯, n } and popping sequence is { x​1​​ ,x​2​​ ,⋯,x​n​​ }. If x​2​​ =n, we can obtain n−1 different possible popping sequences.

Translation: Suppose that the push sequence of the stack is {1,2,...n}, and the pop sequence is {x1, x2,...xn}. If x2 = n, we can get n−1 different possible retrieval sequences.

T F

Multiple choice

2-1
Assuming that 5 integers are pushed onto the stack in the order of 1, 2, 3, 4, and 5, and the stacking order is 3, 5, 4, 2, 1, then in order to obtain such an output, the stack size is at least for:

A:2
B:3
C:4
D:5

2-2
If elements a, b, c, d, e, and f are pushed onto the stack in turn, and alternate push and unstack operations are allowed, but three consecutive unstack operations are not allowed, what is the impossible unstack sequence?

A:b c a e f d
B:c b d a e f
C:d c e b f a
D:a f e d c b

fedc unstack 4 times in a row;

2-3
Suppose the input sequence of a stack is 1, 2, 3, 4, 5, then in the following sequence, which is the legal output sequence of the stack?

A:3 2 1 5 4
B:5 1 2 3 4
C:4 5 1 3 2
D:4 3 1 2 5

2-4
Let P stand for push and O stand for pop. When using the stack to solve the suffix expression 1 2 3 + * 4 –, the stack operation sequence is:

A:PPPOOPOO
B:PPOOPPOOPPOO
C:PPPOOPOOPPOO
D:PPPOOPOOPPOOPO

Postfix expression calculation

In order to explain the benefits of suffix expressions, let's first take a look at how the computer uses suffix expressions to calculate the final result 20.

Suffix expression: 9 3 1-3 * + 10 2 / +

Rule: traverse each number and symbol in the expression from left to right, push the stack when it encounters a number, and pop the two numbers at the top of the stack when it encounters a symbol, perform operations, and push the result of the operation until Finally get the result.

Here are the detailed steps:

  1. Initialize an empty stack. This 桟 is used to enter and exit the number to be calculated.

  2. The first three in the postfix expression are all numbers, so 9, 3, and 1 are pushed onto the stack.
    Insert picture description here

  3. Next is the minus sign "-", so pop 1 from the stack as the subtract, and pop 3 as the subtract, and calculate 3-1 to get 2, and then push 2 into the stack.

  4. Then the number 3 is pushed onto the stack.
    Insert picture description here

  5. The multiplication "*" follows, which means that 3 and 2 are popped from the stack, and 2 is multiplied by 3 to get 6, and 6 is pushed into the stack.

  6. The following is the addition "+", so find 6 and 9 to find, 9 and 6 are added together to get 15, and 15 is pushed into the stack.
    Insert picture description here

  7. Then the two numbers 10 and 2 are pushed onto the stack.

  8. Next is the symbol. Therefore, the top 2 and 10 are popped from the stack, 10 is divided by 2 to get 5, and 5 is pushed onto the stack.
    Insert picture description here

  9. The last one is the symbol "+", so 15 and 5 are found and added to get 20, and 20 is pushed onto the stack.

  10. The result is 20 pops and the stack becomes empty.
    Insert picture description here

2-5
Suppose the stacking order of a stack is 1, 2, 3, 4, 5. If the first popped element is 4, the last popped element must be:

A:1
B:3
C:5
D: 1 or 5

2-6
Delete a node from the chain stack whose top pointer is ST and use X to save the value of the deleted node, then execute:

A:X= ST->data;
B:X= ST; ST = ST->next;
C:X= ST->data; ST = ST->next;
D:ST = ST->next; X= ST->data;

Textbook P61

2-7
If a singly linked list with head and tail pointers is used to represent a stack, how should the top pointer top of the stack be set?

A: Set the head of the linked list to top
B: Set the end of the linked list to top
C: Any end can be used as top
D: The head and tail of the linked list are not suitable for top

Textbook P60: Since the main operation of the stack is to insert and delete at the top of the stack, obviously it is most convenient to use the head of the linked list as the top of the stack, and there is no need to add a head node for the convenience of operation like a singly linked list;

2-8
If the stack is stored in sequential storage, the two stacks share the space V[m]: top[i] represents the top of the i-th (i=1 or 2) stack; the bottom of stack 1 is at V[0] , The bottom of stack 2 is at V[m-1], the conditions for stack full are:

A:|top[2]-top[1]| == 0
B:top[1]+top[2] == m
C:top[1] == top[2]
D:top[1]+1 == top[2]

doubt?

2-9 In the
following description of the stack, what is wrong:

  1. The stack must be used when rewriting a recursive program in a non-recursive way
  2. When the function is called, the system needs to save the necessary information with the stack
  3. As long as the stacking order is determined, the stacking order can be determined
  4. The stack is a restricted linear table that allows operations on both ends

A: Only 1
B: Only 1, 2, 3
C: Only 1, 3, 4
D: Only 2, 3, 4

  1. The iterative realization of calculating Fibonacci sequence can be realized by only one loop. (doubt??
  2. It must be wrong, otherwise what are the multiple-choice questions above
  3. This limitation is manifested in: stack insertion and deletion operations are only allowed at the end of the table (the "stack top" in the stack), satisfying "FIFO: First In Last Out"; the queue is only allowed to insert data at the end of the table Element, delete the data element in the header to satisfy "First In First Out".
    Textbook P54: The stack is a linear table limited to insert or delete operations only at the end of the table (top of the stack);

2-10 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 one by one, and the printer takes out the data from the buffer one by one. What should be the logical structure of the buffer?

A: Stack
B: Queue
C: Tree
D: Graph

2-11
If an array of size 6 is used to implement a circular queue, and the current front and rear values ​​are 0 and 4 respectively. When two elements are removed from the queue and two more elements are added, what are the values ​​of front and rear respectively?

A: 2 and 0
B: 2 and 2
C: 2 and 4
D: 2 and 6

2-12
If the circular queue is represented by an array of size m, the head position of the queue is front, and the number of queue elements is size, then the position of the tail element rear is:

A:front+size
B:front+size-1
C:(front+size)%m
D:(front+size-1)%m

2-13
Whether the next element can be inserted in the circular sequence queue ().

A: It is related to the value of the team head pointer and the team tail pointer
B: Only related to the value of the tail pointer, not related to the value of the head pointer
C: Only related to the size of the array, not related to the value of the head and tail pointers of the team
D: Related to how many insertion operations have been performed

Textbook P71: The conditions for the empty and full teams in the circular queue are: the conditions for the empty and full
teams: Q.front == Q.rear and
the conditions for the full team: (Q.rear + 1)% MAXQSIZE == Q.front
(MAXQSIZE is The maximum possible length of the define queue at the beginning)

2-14
Existing queue Q and stack S. Initially, the elements in Q are {1, 2, 3, 4, 5, 6} (1 at the head of the queue), and S is empty. If the following three operations are allowed: (1) Dequeue and output the dequeued elements; (2) Dequeue and push the dequeued elements onto the stack; (3) Pop and output the dequeued elements, the output sequence that cannot be obtained is:

A:1, 2, 5, 6, 4, 3
B:2, 3, 4, 5, 6, 1
C:3, 4, 5, 6, 1, 2
D:6, 5, 4, 3, 2, 1

3, 4, 5, 6, 2, 1

Programming questions

7-1 Symbol matching (20 points)
Please write a program to check whether the following symbols in the C language source program are matched: / and /, (and), [and], {and}.

Input format:
Input is a C language source program. When there is only one period. and one carriage return in a certain line, it marks the end of input. No more than 100 symbols need to be checked in the program.

Output format:
First, if all symbols are paired correctly, output YES in the first line, otherwise output NO. Then point out the first unpaired symbol in the second line: if the left symbol is missing, output ?-right symbol; if the right symbol is missing, output left symbol -?.

Input example 1:

void test()
{
    
    
    int i, A[10];
    for (i=0; i<10; i++) /*/
        A[i] = i;
}
.

Output sample 1:

NO
/*-?

Input example 2:

void test()
{
    
    
    int i, A[10];
    for (i=0; i<10; i++) /**/
        A[i] = i;
}]
.

Output sample 2:

NO
?-]

Input sample 3:

void test()
{
    
    
    int i
    double A[10];
    for (i=0; i<10; i++) /**/
        A[i] = 0.1*i;
}
.

Output sample 3:

YES
#include <bits/stdc++.h>
#include <stdio.h>
#define OK          1
#define ERROR       0
#define MAXSIZE 10000
using namespace std;

typedef char SElemType,Status;
const int MAXN=1e5+10;

//顺序栈的表示 
typedef struct{
    
    
	SElemType *base;
	SElemType *top;
	int stacksize;
}SqStack;

//顺序栈初始化
Status InitStack(SqStack &S) {
    
    
	S.base = new SElemType[MAXSIZE];
	if(!S.base)		return OVERFLOW;
	S.top = S.base;
	S.stacksize = MAXSIZE;
	return OK;
} 

//判断顺序栈是否为空
bool StackEmpty(SqStack S) {
    
    
	if(S.top == S.base) 	return true;
	else 	return false;
} 

//顺序栈进栈
/**
* (1)判断是否栈满,若满则出错
* (2)元素e压入栈顶
* (3)栈顶指针加1 
*/ 
Status Push(SqStack &S,SElemType e) {
    
    
	if(S.top - S.base == S.stacksize)	//栈满
		return ERROR;
	*S.top++ = e;
	return OK; 
}

//顺序栈出栈
/**
* (1)判断是否栈空,若空则出错 
* (2)获取栈顶元素e 
* (3)栈顶指针减1 
*/ 
Status Pop(SqStack &S,SElemType &e) {
    
    
	if(S.top == S.base)		//栈空
		return ERROR;
	--S.top;
  e = *S.top;
	return OK; 
}

//取顺序栈栈顶元素
/**
* (1)判断是否空栈,若空则返回错误 
* (2)否则通过栈顶指针获取栈顶元素 
*/ 
Status GetTop(SqStack S,SElemType &e) {
    
    
	if(S.top == S.base)		return ERROR;	//栈空
	e = *(S.top - 1);
	return OK; 
}

//取顺序栈栈底元素
Status GetBase(SqStack &S,SElemType &e) {
    
    
	if(S.base == S.top)		return ERROR;	//栈空
	e = *S.base;
	return OK;  
} 

int main(){
    
    
	char s[MAXN * 10], sign[MAXN], temp, ERRORKIND = ' ';
	int count = 0;
	SqStack S;
	InitStack(S);
	while(cin>>s && s[0] != '.') {
    
    
		int len = strlen(s);
		for(int i = 0; i < len; ++i) {
    
    
			if(s[i] == '(' || s[i] == ')' || s[i] == '[' || s[i] == ']' || s[i] == '{' || s[i] == '}') {
    
    
				sign[count++] = s[i]; 
			}else if(s[i] == '/' && s[i + 1] == '*') {
    
    
				sign[count++] = '/';
				++i;
			}else if(s[i] == '*' && s[i + 1] == '/') {
    
    
				sign[count++] = '*';
				++i;
			}
		}
	}
	for(int i = 0; i < count; ++i) {
    
    
		if(sign[i] == '(' || sign[i] == '[' || sign[i] == '{' || sign[i] == '/') {
    
    
			Push(S, sign[i]);
		}else if(sign[i] == ')'){
    
    
			if(GetTop(S,temp) && temp == '(') {
    
    
				Pop(S,temp);
			}else if(!GetTop(S,temp)) {
    
    
				ERRORKIND = ')';
				break; 
			}else {
    
    
				GetTop(S,temp);
				ERRORKIND = temp;
				break;
			}
		}else if(sign[i] == ']') {
    
    
			if(GetTop(S,temp) && temp == '[') {
    
    
				Pop(S,temp);
			}else if(!GetTop(S,temp)) {
    
    
				ERRORKIND = ']';
				break; 
			}else {
    
    
				GetTop(S,temp);
				ERRORKIND = temp;
				break;
			}
		}else if(sign[i] == '}') {
    
    
			if(GetTop(S,temp) && temp == '{') {
    
    
				Pop(S,temp);
			}else if(!GetTop(S,temp)) {
    
    
				ERRORKIND = '}';
				break; 
			}else {
    
    
				GetTop(S,temp);
				ERRORKIND = temp;
				break;
			}
		}else if(sign[i] == '*') {
    
    
			if(GetTop(S,temp) && temp == '/') {
    
    
				Pop(S,temp);
			}else if(!GetTop(S,temp)) {
    
    
				ERRORKIND = '*';
				break; 
			}else {
    
    
				GetTop(S,temp);
				ERRORKIND = temp;
				break;
			}
		}
	}
	if(ERRORKIND == ' ') {
    
    
		if(StackEmpty(S)) {
    
    	
			cout << "YES" << endl;
			return 0;
		}else {
    
    
			GetBase(S,ERRORKIND);
		}
	}
	cout << "NO" << endl;
	if(ERRORKIND == '(' || ERRORKIND == '[' || ERRORKIND == '{') {
    
    
		cout << ERRORKIND << "-?" << endl;
	}else if(ERRORKIND == ')' || ERRORKIND == ']' || ERRORKIND == '}') {
    
    
		cout << "?-" << ERRORKIND << endl;
	}else if(ERRORKIND == '/') {
    
    
		cout << "/*-?" << endl;
	}else if(ERRORKIND == '*') {
    
    
		cout << "?-*/" << endl;
	}
}

7-2 Stack operation legality (20 points)

Suppose S and X represent the stacking and popping operations respectively. If an empty stack is operated according to a sequence consisting of only S and X, the corresponding operation can be done (if the stack is empty when there is no deletion) and the final state is also the stack empty, then the sequence is said to be a legal stack operation sequence. Please write a program, enter the S and X sequence, and judge whether the sequence is legal.

Input format: The
first line of input gives two positive integers N and M, where N is the number of sequences to be tested, and M (≤50) is the maximum capacity of the stack. Next N lines, each line gives a sequence consisting only of S and X. The sequence is guaranteed not to be empty and the length does not exceed 100.

Output format:
For each sequence, output YES in one line if the sequence is a legal stack operation sequence, or NO if it is not.

Input sample:

4 10
SSSXXSXXSX
SSSXXSXXS
SSSSSSSSSSXSSXXXXXXXXXXX
SSSXXSXXX

Sample output:

YES
NO
NO
NO

Guess you like

Origin blog.csdn.net/Jessieeeeeee/article/details/105751002