【Data Structure】——Introduction to Data Structure Exercises

Question 1

1. The time complexity of the following program segment is ().
A, O(nlog 2 n)
B, O(n 2 )
C, O(n)
D, O(log 2 n)

count=0;
for(k=1;k<=n;k*=2)
	for(j=1;j<=n;j++)
		count++;

Analysis: (A)
It is determined by the for loop, assuming that the outermost m loop b loop ends, then there will be 2 m + k = n, which can be obtained: m = log 2 n,
in addition, the inner for loop n times, so the program segment Time complexity:
O(n)=n×log 2 n=nlog 2 n.

Question 2

2. The time complexity of the following program segment is ().
A, O(logn)
B, O(n 1/2 )
C, O(n)
D, O(n 2 )

x=0;
while(n>=(x+1)*(x+1))
	x=x+1;

Analysis: (B)
Execute the while loop for k times, add 1 to x for each loop, and finally x=k,
so n≥k 2 , get O(n 1/2 ).

Question 3

3. The time complexity of the following program segment is ().
A. O(nlog 3 n)
B. O(log 3 n)
C. O(n 2 )
D. O(n 3 )

i=1;
while(i<=n)
	i=i*3;

Analysis: (B)
the number of cycles is i 3 =n, and O(log 3 n) can be obtained.

Question 4

4. The time complexity of the following program segment is ().
A, O(log 2 n)
B, O(n)
C, O(n 2 )
D, O(nlog 2 n)

int fact(int n){
    
    
	if(n<=1)
		return 1;
	return n*fact(n-1)
}

Analysis: (B)
Since it is n times factorial, that is O(n), a total of n recursive calls to the fact() function are executed, so it is O(n).

Question 5

5. Each storage node contains only one data element, and the storage node is stored in a continuous storage space. In addition, there is a set of tables indicating the storage location of the node. This storage method is ().
A, order
B, chain
C, index
D, hash

Analysis: (C)
Sequential storage is reflected by the adjacency of storage units; chained storage does not require logically adjacent elements to be physically adjacent, and the logical relationship between elements is reflected by pointers indicating the storage addresses of elements; indexes are stored in storage At the same time as the data element, an additional index table needs to be established, and the index items are in the form of keywords and addresses; hash storage directly calculates its storage address according to the keyword of the data element, also known as hash storage (Hash).

Question 6

6. The storage space occupied by the storage structure of chained storage ().
A. Divided into two parts, one part stores the value of the nodes, and the other part stores the pointers indicating the relationship between nodes
B. Only one part stores the values ​​of nodes
C. Only one part stores the pointers indicating the relationship between nodes
D. Divided into two parts Part, one part stores the value of the node, and the other part stores the single element occupied by the node

Parse:(A)

Question 7

7. (Multiple choice) Which of the following belongs to the logical structure is ().
A. Sequence table
B. Hash table
C. Ordered table
D. Singly linked list
E. Stack

Analysis: (C、E)
An ordered table is a linear table with ordered keywords. It is a linear structure and can be stored sequentially and chained, so it belongs to a logical structure. The stack is also a linear structure (logical structure), and both sequential storage and chain storage can be used in the storage structure.

Question 8

8. The two-dimensional array A[m][n] is stored in row-major order, and each element occupies l storage units. The storage address of element A[0][0] is b, then the storage address of element A[i][j] (0≤i≤m-1, 0≤j≤n-1) is ().
A, b + ( i * n + j ) * l
B, b + i * j * l
C, b + ( i + j ) * l
D, b + ( ( i - 1 ) * n + ( j - 1 ) ) * l

Analysis: (A)
There are a total of i rows of elements in front of the element A[i][j], and each row has n elements. A[i][j] is the j+1th element of this row, and the j elements are located in front of it. So there are i * n + j elements in total, and each element occupies l storage units, multiplied by l, plus the storage address of element A[0][0], that is, the storage address of element A[i][j] is b + ( i * n + j ) * l.

Question 9

9. Among the following data structures, () is a non-linear data structure.
A, tree
B, graph
C, string
D, queue
E, stack

Analysis: (AB)
The logical structure is divided into linear structure and nonlinear structure, as follows:
linear structure is a one-to-one relationship, there are linear tables, stacks, queues, strings, etc.; nonlinear structure is a many-to-many relationship, there are two-dimensional arrays ( multidimensional arrays), generalized tables, trees, graphs, etc.

Question 10

10. (Judgment) Different sets of operations can be defined on a given logical structure and its storage representation, so that different data structures can be obtained.

Analysis: (×)
For example, the logical structure of the stack and the queue are the same (linear structure, both are linear tables with restricted operations), and the storage structure is also the same (sequence and chain), but because of their different sets of operations, they become different data structure.

Guess you like

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