data structures -1 st test- march 30, 2015 授課教授:李錫智
TRANSCRIPT
Data Structures-1st test-
March 30, 2015
授課教授:李錫智
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.
Solution of Q.1
Grocery
+getTotalItem(): integer
+isEmpty(): boolean
+stockItem(newEntry: ItemType): boolean
+sellItem(anEntry: ItemType): boolean
+findItem(anEntry: ItemType): ItemType
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.
Q.2-1
• We have to initialize the bag. Please show the content of itemcount and A in the initial state.
Solution of Q.2-1
itemcount = 0
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.
Solution of Q.2-2
• itemcount = 6
• A[] = {5, 10, 5, 5, 15, 10, , , , }
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.
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, , , , , }
Q.2-4
• We want to see whether the bag is empty. How do you do it? Please describe.
Solution of Q.2-4
isEmpty:
If itemcount == 0:
print “The bag is empty.”
Else:
print “There is something in bag.”
End
Q.2-5
• We want to clear the bag, i.e., making the bag empty. How do you do it? Please describe.
Solution of Q.2-5
Clear:
itemcount = 0
for i from 0 to 12:
A[i] = NULL
End
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.
Q.3-1
• We have to initialize the bag. Please show the content of headPtr and the chain in the initial state.
Solution of Q.3-1
headPtr nullptr
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.
Solution of Q.3-2
headPtr nextPtr5 nextPtr10 nextPtr5
nextPtr5 nextPtr15 nextPtr10 nullptr
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.
Solution of Q.3-3
headPtr nextPtr5 nextPtr10 nextPtr5
nextPtr5 nextPtr15 nextPtr10 nullptr
Q.3-4
• We want to see whether the bag is empty. How do you do it? Please describe.
Solution of Q.3-4
• Check whether the headPtr points to nullptr.
headPtr nullptr?
Q.3-5
• We want to clear the bag, i.e., making the bag empty. How do you do it? Please describe.
Solution of Q.3-5
• Find next node and free current node from headPtr until that the next node is nullptr.
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. }
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?
Solution of Q.4
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
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.
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. }
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
Solution of Q.5
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)
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
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. }
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]
Solution of Q.6
• 0, 7 06
• 1, 5 15
• 2, 3 24
• 3, 1 End
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