![Page 1: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/1.jpg)
Data Structures-1st test-
March 30, 2015
授課教授:李錫智
![Page 2: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/2.jpg)
Question 1
• Suppose you want to open a grocery store. Please design an abstract data type (ADT) for your store. Please give at least five operations.
![Page 3: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/3.jpg)
Solution of Q.1
Grocery
+getTotalItem(): integer
+isEmpty(): boolean
+stockItem(newEntry: ItemType): boolean
+sellItem(anEntry: ItemType): boolean
+findItem(anEntry: ItemType): ItemType
![Page 4: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/4.jpg)
Question 2
• Suppose we use an integer variable itemcount and an array A of size 12 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 a bag.
![Page 5: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/5.jpg)
Q.2-1
• We have to initialize the bag. Please show the content of itemcount and A in the initial state.
![Page 6: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/6.jpg)
Solution of Q.2-1
itemcount = 0
![Page 7: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/7.jpg)
Q.2-2
• Suppose we add six integers 5, 10, 5, 5, 15, and 10 successively in the bag. Please show the content of itemcount and A after these additions.
![Page 8: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/8.jpg)
Solution of Q.2-2
• itemcount = 6
• A[] = {5, 10, 5, 5, 15, 10, , , , }
![Page 9: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/9.jpg)
Q.2-3
• We want to delete the second 5 from the bag. How do you do it? Please describe. Also, please show the content of itemcount and A after execution.
![Page 10: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/10.jpg)
Solution of Q.2-3
Q.2-3:
num5count = 0
For i from 0 to 12:
If A[i] == 5:
If num5count == 1:
For j from i+1 to 12:
Copy A[j] to A[j-1]
itemcount = itemcount – 1
Else:
num5count = num5count + 1
End
itemcount = 5
A[] = { 5, 10, 5, 15, 10, , , , , }
![Page 11: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/11.jpg)
Q.2-4
• We want to see whether the bag is empty. How do you do it? Please describe.
![Page 12: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/12.jpg)
Solution of Q.2-4
isEmpty:
If itemcount == 0:
print “The bag is empty.”
Else:
print “There is something in bag.”
End
![Page 13: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/13.jpg)
Q.2-5
• We want to clear the bag, i.e., making the bag empty. How do you do it? Please describe.
![Page 14: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/14.jpg)
Solution of Q.2-5
Clear:
itemcount = 0
for i from 0 to 12:
A[i] = NULL
End
![Page 15: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/15.jpg)
Question 3
• Suppose we use a pointer variable headPtr and a chain to implement the bag structure. We would like to store integers in a bag.
![Page 16: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/16.jpg)
Q.3-1
• We have to initialize the bag. Please show the content of headPtr and the chain in the initial state.
![Page 17: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/17.jpg)
Solution of Q.3-1
headPtr nullptr
![Page 18: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/18.jpg)
Q.3-2
• Suppose we add six integers 5, 10, 5, 5, 15, and 10 successively in the bag. Please show the headPtr and the chain after these additions.
![Page 19: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/19.jpg)
Solution of Q.3-2
headPtr nextPtr5 nextPtr10 nextPtr5
nextPtr5 nextPtr15 nextPtr10 nullptr
![Page 20: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/20.jpg)
Q.3-3
• We want to delete the second 5 from the bag. How do you do it? Please describe. Also, please show headPtr and the chain after execution.
![Page 21: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/21.jpg)
Solution of Q.3-3
headPtr nextPtr5 nextPtr10 nextPtr5
nextPtr5 nextPtr15 nextPtr10 nullptr
![Page 22: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/22.jpg)
Q.3-4
• We want to see whether the bag is empty. How do you do it? Please describe.
![Page 23: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/23.jpg)
Solution of Q.3-4
• Check whether the headPtr points to nullptr.
headPtr nullptr?
![Page 24: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/24.jpg)
Q.3-5
• We want to clear the bag, i.e., making the bag empty. How do you do it? Please describe.
![Page 25: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/25.jpg)
Solution of Q.3-5
• Find next node and free current node from headPtr until that the next node is nullptr.
![Page 26: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/26.jpg)
Question 4
• Suppose we have the following recursive program:
• Let’s execute the function call kkk(5,3).
1. int kkk(n, m)2. {3. cout<<"About to compute for n, m = "<< n <<" , "<< m <<endl;4. if ((n==m) or (m==0))5. return 1;6. else7. return kkk(n-1,m)+kkk(n-1,m-1);8. }
![Page 27: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/27.jpg)
Q.4
• How many times kkk(2,1) is executed before termination? 3
• How many times kkk(1,1) is executed before termination? 3
• What messages are shown on the screen? Please show them in correct order.
• What is the final return value? 10
• Is this function efficient? Why or why not?
![Page 28: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/28.jpg)
Solution of Q.4
![Page 29: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/29.jpg)
Q.4-3
• What messages are shown on the screen? Please show them in correct order.
About to compute for n, m = 5,3About to compute for n, m = 4,3 About to compute for n, m = 3,3 About to compute for n, m = 3,2 About to compute for n, m = 2,2 About to compute for n, m = 2,1 About to compute for n, m = 1,1 About to compute for n, m = 1,0 About to compute for n, m = 4,2 About to compute for n, m = 3,2 About to compute for n, m = 2,2 About to compute for n, m = 2,1 About to compute for n, m = 1,1 About to compute for n, m = 1,0About to compute for n, m = 3,1About to compute for n, m = 2,1About to compute for n, m = 1,1About to compute for n, m = 1,0About to compute for n, m = 2,0
![Page 30: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/30.jpg)
Q.4-5
• Is this function efficient? Why or why not?
• Usually, recursion is not efficient in terms of stack and system overhead. Think about a recursion to calculate the factorial of a number. You will have n-1 recursion calls.
![Page 31: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/31.jpg)
Question 5
• Suppose we have a program:
• where max takes the larger of its two arguments. Let’s execute the function call aaa(A,0,6) with A = [20, 80, 30, 40, 10, 70, 50].
1. int aaa(A,i,n)2. {3. cout<<"About to compute for i, n = "<<i<<" , "<< n <<endl;4. if (n==i)5. return A[n];6. else7. return max(aaa(A,i,(n+i)/2), aaa(A,(n+i)/2+1,n));8. }
![Page 32: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/32.jpg)
Q.5
• Please list all the base cases encountered during execution and show what values of n and i in each case.
• What messages are shown on the screen? Please show them in correct order.
• How many times the function max is executed? 6 Please show its two arguments each time.
• What is the final return value? 80
![Page 33: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/33.jpg)
Solution of Q.5
![Page 34: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/34.jpg)
Q.5-1
• Please list all the base cases encountered during execution and show what values of n and i in each case.
• aaa(A, 0, 0)、 aaa(A, 1, 1)、 aaa(A, 2, 2)
aaa(A, 3, 3)、 aaa(A, 4, 4)、 aaa(A, 5, 5)
![Page 35: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/35.jpg)
Q.5-2
• About to compute for i, n = 0 , 6
• About to compute for i, n = 0 , 3
• About to compute for i, n = 0 , 1
• About to compute for i, n = 0 , 0
• About to compute for i, n = 1 , 1
• About to compute for i, n = 2 , 3
• About to compute for i, n = 2 , 2
• About to compute for i, n = 3 , 3
• About to compute for i, n = 4 , 6
• About to compute for i, n = 4 , 5
• About to compute for i, n = 4 , 4
• About to compute for i, n = 5 , 5
• About to compute for i, n = 6 , 6
![Page 36: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/36.jpg)
Question 6
• Suppose we have a recursive program:
• where Swap exchanges the two values involved. Let’s execute the function call qqq(A,0,7) with A = [20, 80, 30, 40, 10, 70, 50].
1. void qqq(A,i,n) 2. {3. cout<<"About to compute for i, n = "<<i <<" , "<< n <<endl;4. if (n>1)5. cout<<"To swap the indices "<<i<<" and "<< i+n-1<<endl;6. Swap A[i] and A[i+n-1];7. qqq(A,i+1,n-2);8. cout<<"End computing for i, n = "<<i<<", "<<n<<endl;9. }
![Page 37: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/37.jpg)
Q.6
• How many times the function qqq is called before termination, including qqq(A,0,7) itself? 4
• What messages are shown on the screen? Please show them in correct order.
• How many times the Swap operation is performed? 3
• What is the content of A after execution?
• [50, 70, 10, 40, 30, 80, 20]
![Page 38: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/38.jpg)
Solution of Q.6
• 0, 7 06
• 1, 5 15
• 2, 3 24
• 3, 1 End
![Page 39: Data Structures -1 st test- March 30, 2015 授課教授:李錫智](https://reader035.vdocuments.net/reader035/viewer/2022081419/56649ebb5503460f94bc3d49/html5/thumbnails/39.jpg)
Q.6-2
• What messages are shown on the screen? Please show them in correct order.
• About to compute for i, n = 0 , 7
To swap the indices 0 and 6
About to compute for i, n = 1 , 5
To swap the indices 1 and 5
About to compute for i, n = 2 , 3
To swap the indices 2 and 4
About to compute for i, n = 3 , 1
End computing for i, n = 3 , 1