Presentation is loading. Please wait.

Presentation is loading. Please wait.

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()”?

Similar presentations


Presentation on theme: "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()”?"— Presentation transcript:

1 Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1

2 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

3 Ans: true item true false 3

4 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

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

6 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

7 Ans: 7

8 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)).getEntr y(3)”? 8

9 Ans: 0 false a k 9

10 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

11 Ans: 11

12 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

13 Ans: 13

14 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

15 Ans: 15

16 16

17 17

18 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

19 Ans: 19

20 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

21 Ans: 21

22 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


Download ppt "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()”?"

Similar presentations


Ads by Google