Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures -1 st test- March 30, 2015 授課教授:李錫智.

Similar presentations


Presentation on theme: "Data Structures -1 st test- March 30, 2015 授課教授:李錫智."— Presentation transcript:

1 Data Structures -1 st test- March 30, 2015 授課教授:李錫智

2 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.

3 Solution of Q.1 Grocery +getTotalItem(): integer +isEmpty(): boolean +stockItem(newEntry: ItemType): boolean +sellItem(anEntry: ItemType): boolean +findItem(anEntry: ItemType): ItemType

4 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.

5 Q.2-1 We have to initialize the bag. Please show the content of itemcount and A in the initial state.

6 Solution of Q.2-1 itemcount = 0

7 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.

8 Solution of Q.2-2 itemcount = 6 A[] = {5, 10, 5, 5, 15, 10,,,, }

9 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.

10 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,,,,, }

11 Q.2-4 We want to see whether the bag is empty. How do you do it? Please describe.

12 Solution of Q.2-4 isEmpty: If itemcount == 0: print “The bag is empty.” Else: print “There is something in bag.” End

13 Q.2-5 We want to clear the bag, i.e., making the bag empty. How do you do it? Please describe.

14 Solution of Q.2-5 Clear: itemcount = 0 for i from 0 to 12: A[i] = NULL End

15 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.

16 Q.3-1 We have to initialize the bag. Please show the content of headPtr and the chain in the initial state.

17 Solution of Q.3-1 headPtr nullptr

18 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.

19 Solution of Q.3-2 headPtr nextPtr5 10 nextPtr5 5 15nextPtr10 nullptr

20 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.

21 Solution of Q.3-3 headPtr nextPtr5 10 nextPtr5 5 15nextPtr10 nullptr

22 Q.3-4 We want to see whether the bag is empty. How do you do it? Please describe.

23 Solution of Q.3-4 Check whether the headPtr points to nullptr. headPtr nullptr ?

24 Q.3-5 We want to clear the bag, i.e., making the bag empty. How do you do it? Please describe.

25 Solution of Q.3-5 Find next node and free current node from headPtr until that the next node is nullptr.

26 Question 4 Suppose we have the following recursive program: Let’s execute the function call kkk(5,3).

27 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?

28 Solution of Q.4

29 Q.4-3 What messages are shown on the screen? Please show them in correct order.

30 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.

31 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].

32 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

33 Solution of Q.5

34 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)

35 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

36 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].

37 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]

38 Solution of Q.6 0, 7  0  6 1, 5  1  5 2, 3  2  4 3, 1  End

39 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


Download ppt "Data Structures -1 st test- March 30, 2015 授課教授:李錫智."

Similar presentations


Ads by Google