Presentation is loading. Please wait.

Presentation is loading. Please wait.

DATA STRUCTURES 1st exam solution. Question 1(1/3) Suppose we have the ADT Bag which has the following operations: bool isEmpty(): tests whether Bag is.

Similar presentations


Presentation on theme: "DATA STRUCTURES 1st exam solution. Question 1(1/3) Suppose we have the ADT Bag which has the following operations: bool isEmpty(): tests whether Bag is."— Presentation transcript:

1 DATA STRUCTURES 1st exam solution

2 Question 1(1/3) Suppose we have the ADT Bag which has the following operations: bool isEmpty(): tests whether Bag is empty. bool add(newEntry): adds the object newEntry into Bag. bool remove(anEntry): remove the first occurrence of anEntry from Bag. bool isFull(): tests whether Bag is full. bool contains(anEntry): tests whether Bag contains the object anEntry. void clear(): removes all objects from Bag. Note that add(newEntry) just stores without checking duplication. That is, no checking on duplication is done for this operation. Also, remove(anEntry) removes only the first occurrence of anEntry from Bag.

3 Question 1(2/3) A) Suppose we want to create another operation, add-U(newEntry), which won’t allow duplicates to be stored. That is, at most one occurrence of an entry is stored in Bag. Please describe how to apply the functions isEmpty, add, remove, isFull, contains, or clear to implement add-U. ANS:

4 Question 1(3/3) B) Suppose we want to create another operation, remove-A(anEntry), which will remove all the occurrences of anEntry from Bag. Please describe how to apply the functions isEmpty, add, remove, isFull, contains, or clear to implement remove-A. ANS:

5 Question 2 (1/4) Suppose we use an array A of size 8 and an integer variable itemCount, recording the number of objects currently stored in A, to implement the Bag structure. Note that objects are stored in no particular order and objects may be duplicated. The objects we are concerned with are integers. Please answer the following questions:

6 Question 2 (2/4) A) Suppose we add five integers 10, 5, 5, 15, and 20 successively in the Bag. Please show the content of itemCount and A after each addition. ANS: 1,A[10] ;itemCount=1 2,A[10,5] ;itemCount=2 3,A[10,5,5] ;itemCount=3 4,A[10,5,5,15] ;itemCount=4 5,A[10,5,5,15,20] ;itemCount=5

7 Question 2 (3/4) B) Suppose we delete the first 5 from the Bag as fast as possible. How do you do it? Please describe. Also, please show the content of itemCount and A after execution. ANS: 從陣列循序搜尋找到第一個 5 後,以最後的 元素覆蓋,之後 itemcount=itemcount-1; A[10,20,5,15];itemCount=4

8 Question 2 (4/4) C) We want to see whether the Bag is empty. How do you do it? Please describe. ANS: 看 itemCount 的值是否等於 0 D) We want to clear the Bag, i.e., making the Bag empty. How do you do it? Please describe. ANS: 設定 itemCount=0;

9 Question 3(1/4) Suppose we use a link-based structure and a pointer variable headPtr, pointing to the first object of the chain, to implement the Bag structure. Note that objects are stored in no particular order and objects may be duplicated. We would like to store integers in the Bag. Please answer the following questions:

10 Question 3 (2/4) A) Suppose we add five integers 10, 5, 5, 15, and 20 successively in the Bag. Note that we do the additions as fast as possible. Please draw the headPtr and the chain after each addition. ANS: 10 5 5 15 20 10 5 5 15 10 5 5 5

11 Question 3 (3/4) B) Suppose we delete the first 5 from the Bag. How do you do it? Please describe. Also, please draw the headPtr and the chain after execution. ANS: 將指向第一個 5 的指標,改為指向第一個 5 的 下一個元素 20 515 10

12 Question 3 (4/4) C) We want to see whether the Bag is empty. How do you do it? Please describe. ANS: 看 headPtr 是否為 NULL D) We want to clear the Bag, i.e., making the Bag empty. How do you do it? Please describe. ANS: 1. 由起始點逐點回收記憶體 2. 設定 headPtr=NULL

13 Question 4(1/3) Suppose we want to write a function sum(x1, x2), where x1 and x2 are integers and x2 ≧ x1, which returns the sum x1+(x1+1)+(x1+2)+…+(x2-1)+x2. Please answer the following questions:

14 Question 4(2/3) A) Please describe how to implement sum(x1, x2) iteratively.

15 Question 4(3/3) B) Please describe how to implement sum(x1,x2) recursively.

16 Question 5 (1/4) Suppose we have the following function: void showOff(s:string) { cout << “Enter showOff with string: “ << s << endl; if(s is not an empty string) { showOff(s minus its first character); cout << “About to show the first character of string: “ << s << endl; } cout << “Leave showOff with string: “ << s << endl; }

17 Question 5 (2/4) Note that cout will print out the subsequent strings or variables onto the sreen., and endl is the end-of-line indicator. For example, if s stores “NSYSU”, then cout << “Leave showOff with string: “ << s << endl;” will print out “Leave showOff with string: NSYSU” on the screen and the cursor will be positioned at the beginning of the next line. Now we call showOff(“Hello”). Please answer the following questions:

18 Question 5 (3/4) A) How many calls to showOff will have been performed before showOff(“Hello”) finishes? ANS: 6 次

19 Question 5 (4/4) B.) What are the lines of print-out on the screen produced by showOff(“Hello”) eventually? ANS:

20 Question 6 (1/3) Consider the following function: Integer tryTry(n: non-negative integer){ If(n==0 or n==1) return 1; else return tryTry(n-2)+tryTry(n-1)+2; } Now we call tryTry(5). Please answer the following questions:

21 Question 6 (2/3) A) How many calls to tryTry will have been performedbefore tryTry(5) finishes? ANS: 15 次 B) How many calls to tryTry(2) will have been performed before tryTry(5) finishes? ANS: 3 次

22 Question 6 (3/3) C) What is the returned value of tryTry(5)? ANS: 22 D) How many additions have been performed before tryTry(5) finishes? ANS: 14 次

23 Question 7(1/1) Consider the language: = | | = X = Y A) Is the string XXXXYY in this language? Prove it by showing how it is derived. ANS: 此單詞不存在此語言,因無法以 Y 結尾 B) Write all five-character strings that contain more Y’s than X’s in this language. ANS: YYYYX 、 YYYXX

24 Question 8(1/2) Consider the language: = | a a | b b | c c | … | y y | z z = a | b | c | d |….| x | y | z. Let c(n) be the number of such strings of length n.

25 Solution of Q8(2/2)

26 Question 9(1/5) Please answer the following questions: A) If we implement the Bag with the array- based structure without the itemCount variable, can it work? Please explain clearly, otherwise you won’t get any points for your answer. ANS: No ,無法得知矩陣是否還有空間或是 否有資料。

27 Question 9(2/5) B) What advantages can we have if implementing the Bag with the array-based structure? Please explain clearly, otherwise you won’t get any points for your answer. ANS: 1. 節點儲存於連續記憶體,可以快速訪問節點。

28 Question 9(3/5) C) What disadvantages can we have if implementing the Bag with the array-based structure? Please explain clearly, otherwise you won’t get any points for your answer. ANS: 1. 儲存空間固定,無法彈性變動

29 Question 9(4/5) D) What disadvantages can we have if implementing the Bag with the link-based structure? Please explain clearly, otherwise you won’t get any points for your answer. ANS: 1. 節點只能循序走訪,較花費時間。 2. 需多花費記憶體空間儲存指標。

30 Question 9(5/5) E) If we implement the Bag with the link-based structure with the itemCount variable, what advantages can we get? Please explain clearly, otherwise you won’t get any points for your answer. ANS: 可以快速得知資料總節點數。

31 Question 10(1/5) Please answer the following questions: A) Given an ordinary algebraic expression (x+y)*(z- w)/(y-z)+x, please show the corresponding postfix expression for it. Note that w, x, y, and z are operands. ANS: xy+zw-*yz-/x+

32 Question 10(2/5) B) Given an ordinary algebraic expression (x+y)*(z- w)/(y-z)+x, please show the corresponding prefix expression for it. Note that w, x, y, and z are operands. ANS: +/*+xy-zw-yzx

33 Question 10(3/5) C. Given an expression xy+zw*-z+, where w, x, y, and z are operands, please prove whether or not it is a legal postfix expression by showing how it is derived from the following rules for : = | = a|b|c|…|x|y|z = +|-|*|/

34 Question 10(4/5) ANS: 1. = = x = = y 2. = =xy+ 3. = = z = = w 4. = =zw* 5. = =(xy+)(zw*)- 6. = =(xy+zw*-)z+

35 Question 10(5/5) D) Please convert the postfix expression, xyz-w/+y*, where w, x, y, and z are operands, to an ordinary algebraic expression. ANS: (x+(y-z)/w)*y E) Please convert the prefix expression, /*+xy-zwx, where w, x, y, and z are operands, to an ordinary algebraic expression. ANS: (x+y)*(z-w)/x


Download ppt "DATA STRUCTURES 1st exam solution. Question 1(1/3) Suppose we have the ADT Bag which has the following operations: bool isEmpty(): tests whether Bag is."

Similar presentations


Ads by Google