1 / 22

Data Structures -1 st exam-

Data Structures -1 st exam-. 授課教師 : 李錫智 教授. 1.[10 ] Suppose we have the following program: 1: int *p = new int ; 2: int *q = new int ; 3: *p = 20; 4: q = p; 5: int *r = q; 6: *r = *p - *q; 7: q = new int ; 8: *q = 30;

akina
Download Presentation

Data Structures -1 st exam-

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 1.[10] Suppose we have the following program: 1: int *p = new int; 2: int *q = new int; 3: *p = 20; 4: q = p; 5: int *r = q; 6: *r = *p - *q; 7: q = new int; 8: *q = 30; Note that ‘new’ dynamically allocates memory in C++, similar to ‘alloc’ in C. a. If we print out *p, and *q immediately after Line 3, what values will we have? b. If we print out *p, *q, and *r immediately after Line 5, what values will we have? c. If we print out *p, *q, and *r immediately after Line 6, what values will we have? d. If we print out *p, *q, and *r immediately after Line 8, what values will we have?

  3. Ans: a. *p=20 *q=Unknow b. *p=20 *q=20 *r=20 c. *p=20*q=20 *r=0 d. *p=20 *q=30 *r=0

  4. 2.[10] Suppose we have the following program: 1: int *p = new int; 2: int *q = new int; 3. int r; 4. p = &r; 5: *p = 20; 6: p = new int; 7: *p = 40; 8: q = p; 9: q = new int; 10: *q = 30; Note that ‘new’ dynamically allocates memory in C++, similar to ‘alloc’ in C. a. If we print out *p, *q, and r immediately after Line 4, what values will we have? b. If we print out *p, *q, and r immediately after Line 5, what values will we have? c. If we print out *p, *q, and *r immediately after Line 8, what values will we have? d. If we print out *p, *q, and *r immediately after Line 10, what values will we have?

  5. Ans: a. *p=Unknow *q=Unknow r=Unknow b. *p=20 *q=Unknow r=20 c. *p=40 *q=40 r=20 d. *p=40 *q=30 r=20

  6. 3.[10] The ADT list has the following operations: isEmpty(): returns true if the list is empty, false otherwise remove(int index): deletes the element at position `index’ from the list getlength(): returns the number of elements that are in the list a. Consider the following loop: while (!isEmpty()) remove(1); What is the function of this code? b. Consider the following loop: for (intpos = getLength(); pos >= 1; pos = pos - 1) remove(1); What is the function of this code? c. Consider the following loop: for (intpos = getLength(); pos >= 1; pos = pos - 1) remove(pos); What is the function of this code? d. Consider the following loop: for (intpos = 1; pos <= getLength(); pos = pos + 1) remove(pos); What is the function of this code?

  7. Ans: a. 從第一個節點開始將整個list刪除 b. 從第一個節點開始將整個list刪除c. 從最後一個節點開始將整個list刪除Ans: a. 從第一個節點開始將整個list刪除 b. 從第一個節點開始將整個list刪除c. 從最後一個節點開始將整個list刪除 d. 刪除奇數點,但程式執行到 POS>Length時會出現錯誤。

  8. 4.[10] Suppose we have a linked list, for storing integers, with the following:list: a variable, contains the address of the first node 1st node: containing integer 37, and the address of this node is 400 2nd node: containing integer 87, and the address of this node is 1000 3rd node: containing integer 57, and the address of this node is 600 4th and last node: containing integer 77, and the address of this node is 1200 Please answer the following questions INDEPENDENTLY. Note that you MUST show the content, i.e., the integer and the pointer, in each node and in the list variable. a. Show the resulting list after inserting an integer 17 as the third node. Assuming the address of the new node for storing 17 is 700. b. Show the resulting list after deleting the second node. c. Show the resulting list after inserting an integer 17 as the last node. Assuming the address of the new node for storing 17 is 700. d. Show the resulting list after deleting the first node.

  9. Initial • inserting an integer 17 as the third node (address=700) • delete the second node

  10. Initial: c. inserting an integer 17(address=700) as the last node d. delete the first node.

  11. 5. [10] Suppose we use a circular linked list for storing integers, with the variable list containing the address of the last node. Redo Problem 4. Please answer the four questions INDEPENDENTLY. Note that you MUST show the content, i.e., the integer and the pointers, in each node and in the head. You don’t need to consider the dummy head node.

  12. Initial: • inserting an integer 17(address=700) as the third node b. deleting the second node

  13. Initial: c. inserting an integer 17 as the last node. d. deleting the first node

  14. 6.[10] Suppose we use a doubly circular linked list for storing integers, with the variable list containing the address of the last node. Redo Problem 4. Please answer the four questions INDEPENDENTLY. Note that you MUST show the content, i.e., the integer and the pointers, in each node and in the head. You don’t need to consider the dummy head node.

  15. Initial: • inserting an integer 17(address=700) as the third node b. deleting the second node

  16. Initial: c. inserting an integer 17 as the last node. d. deleting the first node

  17. 7.[10] Assume that the head variable contains the pointer to the first node of a list. Each node in the list is declared to be data type of Node which contains two fields, item and next. In the item field, an integer is stored. In the next field, the pointer to the next node is stored. Consider the following code: for (Node *cur = head; cur != NULL; cur = cur  next) Print out cur  item; What does the code do? Please describe briefly and clearly. Ans: 從第一個節點開始,依序將節點的item印出

  18. 8.[10] Assume that the head variable contains the pointer to the first node of a sorted list. Each node in the list is declared to be data type of Node which contains two fields, item and next. In the item field, an integer is stored. In the next field, the pointer to the next node is stored. Let value be a variable contains an integer. Consider the following code: for (Node *prev = NULL, Node *cur = head; (cur != NULL) && (value > cur  item); prev = cur, cur = cur  next); What does the code do? Please describe briefly and clearly. Ans:找出此sorted list中,比value大的第一個數,cur指向大於value的第一個數,prev指向小於value的最大數;若找不到則cur=null,prev為最後一個節點

  19. 9.[10] The ADT sorted list has the following operations: sortedIsEmpty(): returns true if the list is empty, false otherwise sortedRemove(int index): deletes the element at position `index’ from the list sortedGetlength(): returns the number of elements that are in the list locatePosition(Type item): returns the position of item in the list. If the item is not in the list, returns NULL Please describe how you delete a specified value from the sorted list. Ans: if( sortedIsEmpty() == false && locatePosition(Type item) != NULL ) sortedRemove( locatePosition(Type item) )

  20. [10] Please answer the following questions a. Suppose you are given n numbers and asked to find the product of these n numbers. Please describe how you get the answer both iteratively and recursively. b. Consider the following program: int gaga(int a) { if (a < 1) print out “I hate you!” and stops; if (a=1) return 2; if (a = 2) return 3; return (gaga(a-1) + gaga(a-2)); } What is the returned value of gaga(5)?

  21. a. itrtatively: inti=0; intans=1; for ( i ; i<=n-1 ; i++) ans=ans*a[i]; recursively: int rec(n) { if( n == 0) return a[0]; else return rec(n-1)*a[n]; }

  22. gaga(5) = gaga(4)+gaga(3) = gaga(3)+gaga(2)+gaga(1)+gaga(2) = gaga(2)+gaga(1)+gaga(2)+gaga(1)+gaga(2) = 3 + 2 + 3 + 2 + 3 = 13

More Related