[Data Structure] - Stack, Queue and String Short Answer Question Templates

1. Stack

(1) Basic concepts of stack

1. Briefly describe the characteristics of the stack.

: The stack is a linear list with restricted access points. Insertion or deletion operations are only allowed at one end. The stack follows the principle of first in, last out (FILO), that is, the last element is taken out first.

(2) Application of stack

1. What are the application scenarios of the stack? (Try to give at least three examples)

: The stack has the following application scenarios:
① Recursion and function calls;
② Expression evaluation (for example, suffix expressions, parentheses Matching, etc.);
③ Base conversion (for example, converting decimal numbers to binary numbers, etc.);
④ Maze solving;
⑤Caching mechanism;
⑥Use stack to perform pre-, mid- and post-order traversal of binary trees;
⑦Use stack to simulate queue.

2. What are the operating principles of the stack? Give an application example of two stacks.

: The operating principle of the stack is first in, last out, for example, using the stack to perform pre-, mid-, and post-order traversal of a binary tree, and using the stack to simulate a queue, etc.

3. Briefly describe the basic idea of ​​using two stacks to simulate a queue.

: First, define two stacks S1 and S2, push all elements into stack S1 in sequence, then pop them out of the stack and into stack S2, and then pop them out of stack S2.

(3) Code implementation of stack

1. Write the structure of the sequential stack of the sequential storage structure, the key code of stack empty, stack full, push and pop.

: ① Sequential stack structure:

#define MaxSize 20	//可自行设置
typedef struct {
    
    
	int data[MaxSize];	//存放栈中元素 ,使用数组
	int top;	//栈顶指针 ,记录栈顶元素的位置 
} SqStack;	//顺序栈的类型定义 

②The condition for judging whether the sequence stack is an empty stack is S.top==-1.
③The condition for judging whether the sequential stack is full is S.top==MaxSize-1.
④Insert an element into the sequential stack, that is, push it into the stack,

++S.top;	//top指针始终指向栈顶,新的元素进栈,所以指针先加1
S.data[S.top]=x;	//将进栈元素的值传入并入栈

These two lines of code can also be directly replaced with one line of code: S.data[++S.top]=x.
⑤ Delete an element from the sequence stack, that is, pop it out of the stack,

x=S.data[S.top];	//出栈
S.top--;	//指针减1

These two lines of code can also be directly replaced with one line of code: x=S.data[S.top–].

(4) Recursive algorithm

1. What are the two parts of the recursive algorithm? Briefly describe the steps of recursion.

: A recursive algorithm must include a termination condition and a recursive part. When the recursive conditions are not met, the recursion ends and returns, otherwise the recursive operation continues.

2. Briefly describe the advantages and disadvantages of recursive algorithms.

: The code of the recursive algorithm is concise and clear, and its readability is good, but its time complexity and space complexity are large, and overflow may occur when calling the stack.

(5) The difference between stack and queue

1. Briefly describe the similarities and differences between stacks and queues.

: ① Difference: The operation rules are different. The stack follows the principle of first in, last out, while the queue follows the principle of first in, first out. The stack only allows insertion and deletion operations at one end, while the queue only allows insertion and deletion at one end. Insert and delete on the other end. In addition, the two purposes are different. The stack is used for subroutine calling and protecting the scene, while the queue is used for multi-channel job processing, instruction registration and other operations;
②The same point: they are all operated by A limited linear table with the same logical structure and the same storage representation (sequential storage structure and chained storage structure).

2. Queue

(1) Basic concept of queue

1. What does the "first in, first out" feature of the queue mean?

: The queue follows the principle of first in, first out, and its characteristic is that the last element inserted into the queue is always the last to be deleted.

(2) Application of queue

1. What are the applications of queues?

: The application scenarios of the queue are as follows:
(1) Buffer (such as the print data buffer between the computer and the printer);
( 2) Page replacement algorithm;
(3) Breadth-first search of graphs and hierarchical traversal of trees all rely on the basic idea of ​​​​queues.

(3) Basic concepts of circular queue

1. How to solve the problem of "false overflow" of the queue? And write the relevant code.

: Connect the one-dimensional array of the storage queue end to end to form a ring to form a circular queue. When the head pointer and the tail pointer of the queue are increased by 1, the remainder operation is performed to prevent "false overflow" of the queue. The entry point is for the rear pointer of the queue, that is, Q.rear=(Q.rear+1)%MAXSIZE, and the exit pointer is for the head pointer of the queue, that is, Q.front=(Q.front+1)%MAXSIZE.

2. What is a circular queue? Give the calculation formula for the number of elements in the circular queue (assume the maximum length is N, the head pointer FRONT, and the tail pointer REAR).

: Circular queue is to connect the one-dimensional arrays in the sequential queue end to end to form a ring, that is, it is logically regarded as a ring and connected, so that the head pointer and the tail pointer of the queue move along the ring; calculation of the number of elements in the circular queue The formula is (REAR-FRONT+N)%N.

Three, string

(1) Basic concepts of string

1. Noun explanation: string.

: String is a linear structure. It is a special linear table, a limited sequence composed of zero or more characters. The data elements of the string must be characters, which usually appear in the form of strings.

2. Describe the difference between the following concepts: space string and empty string.

: The empty string is a string that does not contain any characters, the length is 0, and the ASCII code is 0, while the space string is a character, which is a string composed of spaces, the length is equal to the number of spaces, and its ASCII code is 32.

(2) KMP algorithm

1. What improvements does the KMP algorithm have compared to the Brute algorithm (a naive string matching algorithm)?

: The time complexity of the general string matching algorithm is O(mn), while the time complexity of the KMP algorithm is O(m+n). Its main string pointer does not need to be traced back. When the length of the main string is large, partial matching and one-time matching are required. When it cannot be loaded into the memory, its advantages are more prominent than ordinary matching.

Guess you like

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