Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following...
-
Upload
denis-melton -
Category
Documents
-
view
257 -
download
0
description
Transcript of Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following...
Data Structures-2nd exam-
授課教師 : 李錫智 教授
1
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
Ans:•true•item•true•true•false
3
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
Ans:將字串裡的字元一一放進 stack ,如果放進的是$ 則開始 peek ,第一次 peek 的字元與剩下還沒放進 stack 的第一個字元比較,如果相同,則開始 pop ,接著第二次 peek ,如果 peek 的字元與剩下的第二個字元一樣,則 pop ,依此類推,如果 peek 的字元與相對比較的字元不一樣,或者剩下的字元已經比完但是 stack 裡還沒空,或者stack 空了,但是剩下的字元還沒比完,上述 3個情況都不屬於 language L 。
5
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
Ans:
7
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
Ans:•0•false•false•a•k
9
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
Ans:
11
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
Ans:
13
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
Ans:
15
16
17
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
Ans:
19
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
Ans:
21
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