22
Data Structures -2 nd exam- 授授授授 : 授授授 授授 1

Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

Embed Size (px)

DESCRIPTION

Ans: true item true false 3

Citation preview

Page 1: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

Data Structures-2nd exam-

授課教師 : 李錫智 教授

1

Page 2: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

1. [10] Please answer the following questions about stack:What is the response of the statement

“(new stack()).isEmpty()”?What is the response of the statement

“((new stack()).push(item)).peek()”?What is the response of the following statement:

“(((new stack()).push(item)).peek()).pop()”?What is the response of the following statement:“((((new stack()).push(item)).peek()).pop()).push(item)”?What is the response of the following statement:

“((((new stack()).push(item)).peek()).pop()).pop()”?2

Page 3: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

Ans:•true•item•true•true•false

3

Page 4: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

2. [10] Suppose we have a language L = { s$s' : s is a possibly empty string of characters other than $, s' = reverse(s)}. Please describe how to use a stack to determine whether the following string belongs to L:“abcd$dcba”“tak$tak”“pk$kpw”“hike$ek”“$xy”

4

Page 5: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

Ans:將字串裡的字元一一放進 stack ,如果放進的是$ 則開始 peek ,第一次 peek 的字元與剩下還沒放進 stack 的第一個字元比較,如果相同,則開始 pop ,接著第二次 peek ,如果 peek 的字元與剩下的第二個字元一樣,則 pop ,依此類推,如果 peek 的字元與相對比較的字元不一樣,或者剩下的字元已經比完但是 stack 裡還沒空,或者stack 空了,但是剩下的字元還沒比完,上述 3個情況都不屬於 language L 。

5

Page 6: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

3. [10] Suppose we have a stack, called aStack, implemented by array, as shown below (the left end is the bottom of the stack and index 0):

p, q, r and top = 2. Note that the capacity of the array

is 10.After executing the following statement:

“(((aStack.pop()).push(x)).push(w)).peek()” Draw the resulting stack [3]. What is the returned value [2]?After executing the following statement:

“(((((aStack.peek()).push(w)).push(a)).pop()).pop()).peek()” Draw the resulting stack [3]. What is the returned value [2]?

6

Page 7: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

Ans:

7

Page 8: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

4. [10] Please answer the following questions about list:What is the response of the statement

“(new List()).getLength()”?What is the response of the statement

“(new List()).remove(1)”?What is the response of the following statement:

“((((new List()).insert(1,a)).insert(1,b)).remove(2)).isEmpty()”?What is the response of the following statement:

“((((new List()).insert(1,a)).insert(1,b)).insert(2,c)).getEntry(3)”?What is the response of the following statement:

“(((((new List()).insert(1,a)).insert(1,b)).setEntry(2,k)).insert(2,c)).getEnt

ry(3)”?8

Page 9: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

Ans:•0•false•false•a•k

9

Page 10: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

5. [10] Suppose we have a list, called aList, implemented by array, as shown below (the left end is position 1 and index 0):

w, x, y Note that the capacity of the array is 10.

After executing the following statement:“(((aList.insert(1,a)).insert(1,b)).insert(2,c)).getEntry(3)”

Draw the resulting list [3]. What is the returned value [2]?After executing the following statement:

“((((aList.insert(1,a)).insert(1,b)).setEntry(2,k)).insert(2,c)).getEntry (3)”

Draw the resulting list [3]. What is the returned value [2]?10

Page 11: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

Ans:

11

Page 12: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

6. [10] Given an array:37, 13, 29, 14, 10

Please sort it in ascending order using Selection Sort. Draw the array after each swap.

12

Page 13: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

Ans:

13

Page 14: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

7. [10] Given an array:37, 13, 29, 14, 10

Please sort it in ascending order using Bubble Sort. Draw the array after each pass.

14

Page 15: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

Ans:

15

Page 16: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

16

Page 17: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

17

Page 18: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

8. [10] Given an array:37, 13, 29, 14, 10

Please sort it in ascending order using Insertion Sort. Draw the array after each insertion.

18

Page 19: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

Ans:

19

Page 20: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

9. [10] Given an array:2, 6, -2, 5, 4, 0, -5, 7, 1, 8, 3

and indexFromLeft = 0 and indexFromRight = 9. Please partition it using the rightmost element as the pivot. Show the array, indexFromLeft, and indexFromRight after each swap.

20

Page 21: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

Ans:

21

Page 22: Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?

10.[10] What are the best case and the worst case in efficiency, expressed in big O, of the sorting methods Selection Sort [2], Bubble Sort [2], Insertion Sort [2], Quick Sort [2], and Merge Sort [2].

Ans:

22