22
Data Structures -1 st exam- 授授授授 : 授授授 授授

Data Structures -1 st exam-

  • Upload
    akina

  • View
    42

  • Download
    0

Embed Size (px)

DESCRIPTION

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; - PowerPoint PPT Presentation

Citation preview

Page 1: Data Structures -1 st   exam-

Data Structures-1st exam-

授課教師 : 李錫智 教授

Page 2: 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;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?

Page 3: Data Structures -1 st   exam-

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

Page 4: Data Structures -1 st   exam-

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?

Page 5: Data Structures -1 st   exam-

Ans: a. *p=Unknow *q=Unknow r=Unknow

b. *p=20 *q=Unknow r=20c. *p=40 *q=40 r=20

d. *p=40 *q=30 r=20

Page 6: Data Structures -1 st   exam-

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

Page 7: Data Structures -1 st   exam-

Ans: a. 從第一個節點開始將整個 list 刪除 b. 從第一個節點開始將整個 list 刪除c. 從最後一個節點開始將整個 list 刪除d. 刪除奇數點,但程式執行到

POS>Length 時會出現錯誤。

Page 8: Data Structures -1 st   exam-

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 4002nd node: containing integer 87, and the address of this node is 10003rd node: containing integer 57, and the address of this node is 6004th and last node: containing integer 77, and the address of this node is 1200Please 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.

Page 9: Data Structures -1 st   exam-

Initial

a. inserting an integer 17 as the third node (address=700)

b. delete the second node

Page 10: Data Structures -1 st   exam-

Initial:

c. inserting an integer 17(address=700) as the last node

d. delete the first node.

Page 11: Data Structures -1 st   exam-

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.

Page 12: Data Structures -1 st   exam-

Initial:

a. inserting an integer 17(address=700) as the

third node

b. deleting the second node

Page 13: Data Structures -1 st   exam-

Initial:

c. inserting an integer 17 as the last node.

d. deleting the first node

Page 14: Data Structures -1 st   exam-

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.

Page 15: Data Structures -1 st   exam-

Initial:

a. inserting an integer 17(address=700) as the

third node

b. deleting the second node

Page 16: Data Structures -1 st   exam-

Initial:

c. inserting an integer 17 as the last node.

d. deleting the first node

Page 17: Data Structures -1 st   exam-

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 印出

Page 18: Data Structures -1 st   exam-

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 為最後一個節點

Page 19: Data Structures -1 st   exam-

9.[10] The ADT sorted list has the following operations:sortedIsEmpty(): returns true if the list is empty, false otherwisesortedRemove(int index): deletes the element at position `index’ from the listsortedGetlength(): returns the number of elements that are in the listlocatePosition(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) )

Page 20: Data Structures -1 st   exam-

[10] Please answer the following questionsa. 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)?

Page 21: Data Structures -1 st   exam-

a. itrtatively:int i=0;int ans=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];

}

Page 22: Data Structures -1 st   exam-

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