Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures -1 st exam- 授課教師 : 李錫智 教授. 1.[10] Suppose we have the ADT Bag which has the following operations: bool isEmpty(): tests whether Bag is.

Similar presentations


Presentation on theme: "Data Structures -1 st exam- 授課教師 : 李錫智 教授. 1.[10] Suppose we have the ADT Bag which has the following operations: bool isEmpty(): tests whether Bag is."— Presentation transcript:

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

2 1.[10] 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 isFull(): tests whether Bag is full. bool contains(anEntry): tests whether Bag contains the object anEntry. void clear(): removes all entries from Bag.

3 In this ADT bag, duplicate entries are stored. That is, no checking on duplication is done for the add operation. Now, suppose we want to create another ADT, Bag-A, in which duplicate entries are not allowed. That is, only one copy of an entry is stored in Bag-A. Suppose we have: bool add-A(newEntry): adds the object newEntry into this Bag-A. Please describe how to apply the operations of Bag to perform add-A.

4 Ans: 在每次加入 entry 時,先執行 isFull() ,如果已 經滿了,則不再執行任何動作,如果還沒滿, 再執行 contains(anEntry) ,如果 Bag-A 已有此 entry ,則不加入此 entry ,如果沒有,則再執 行 add(newEntry).

5 2.[10]Suppose we have the ADT Poly which has the following operations: int degree(): returns the degree of a polynomial. float coefficient(power): returns the coefficient of the x power term. void changeCoefficient(newCoefficient, power): replaces the coefficient of the x power term with newCoefficient.

6 Using these operations, please describe how to create the inverse of a given polynomial. Note that Polynomial A is the inverse of polynomial B if and only if A+ B = 0. Ans: 先執行 degree() ,取的多項式的最高次方,之後 再從次方為 0 開始,依序執行 coefficient(power) , 取得每個次方項的係數,再將取得的係數取負號, 然後執行 changecoefficient(newcoefficient,power) , power=0~degree()

7 3.[10] We want to compute the sum of the 100 integers 1, 2, …, 100. (a)Please describe how to do it iteratively. (b)Please describe how to do it recursively. (c)How many additions are performed in (b)? Prove it.

8 Ans: (a) sum=0; For(i=1;i<101;i++) sum=sum+i; (b) n=100 int add(n) { If(n==1) return 1; else return n+add(n-1); }

9 (c) 99 次 假設 s(n) 為執行的次數 s(1)=0,s(2)=1,s(n)=n-1 令 n=k+1 則 s(k+1)=k

10 4.[10] Suppose we have the following program: void display(n){ if (n > 0) { if (n/2 > 0) display(n/2); Send out n%2 onto the screen; } Please trace the execution of display(13), indicating the calling and returning of each display function.

11 Ans: display(13)->display(6)->display(3)->display(1)-> send 1 onto the screen-> send 1 onto the screen-> send 0 onto the screen-> send 1 onto the screen

12 5.[10] Consider the language: = abb | a bb Write all strings that are in this language and that contain 10 or fewer characters. Ans: abb,aabbbb,aaabbbbbb

13 6.[10] Consider the language: = | | = X = Y (a)Is the string XXXXYY in this language? Prove it. (b)Write all three-character strings that contain more Y’s than X’s.

14 Ans: (a) No, 字串不能 Y 結尾 (b) YYX

15 7.[10] 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. (a)Write a recursive definition of c(n). (b)Express c(n) in terms of n. Prove its correctness.

16 Ans: (a) (b)

17 8.[10] Suppose we have an array, for implementing the ADT Bag, shown below: itemCount: 6 maxSize: 8 array:

18 (a)Add 90 to this array. Show the resulting array. (b)Remove 70 from this array. Show the resulting array. (c)Clear this array. Show the resulting array. Ans: (a) itemCount:7 maxSize:8

19 (b) itemCount:5 maxSize:8 (c) itemCount:0 maxSize:8

20 9.[10] Suppose we have a link shown below: header: 500 link: 60|700 (500), 40|300 (700), 70|900 (300), 30|400 (900), 10|0 (400) where x|y (z) indicates the datum stored is x, the address of the next element is y, and the address of the element itself is z. The address stored in header points to the first element 60|700 (500).

21 Ans: (a) (b) (c)

22 10.[10] Suppose a grammar for the prefix expressions is defined to be: = | = + | – = a | b | c (a)Is "+ – ab + – ab – bc + ab" a legal string in the language? Why or why not? (b)What is the result of "– + – ab + bc – ab" if a = 3, b = 4, c = 5?

23 Ans: (a) No , + – ab + – ab – bc + ab => =>

24 (b) (3-4)+(4+5)-(3-4)=9


Download ppt "Data Structures -1 st exam- 授課教師 : 李錫智 教授. 1.[10] Suppose we have the ADT Bag which has the following operations: bool isEmpty(): tests whether Bag is."

Similar presentations


Ads by Google