list

14
Sample Data Structures Questions Chapter 5 Linked Lists Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch Second Edition ISBN 0-201-70297-5, Softcover, 816 pages, 2000 The Purpose of These Questions These are typical exam questions from Chapter 5 of the textbook. These exact questions might not be on your exam, but if you research and find the right answers to these questions, that should be good preparation for a real exam. (It's also possible that some of this material was not covered in your class.) There are 29 short answer questions and 16 multiple choice questions in this file. Short Answers Short Answers Sections 5.1- 5.2 Linked List Fundamentals The node definition: class node { // TYPEDEF typedef double value_type; // CONSTRUCTOR node( const value_type& init_data = value_type( ), node* init_link = NULL ) { data_field = init_data; link_field = init_link; } // Member functions to set the data and link fields: void set_data(const value_type& new_data)

Upload: raffi-sk

Post on 08-Apr-2016

423 views

Category:

Documents


12 download

DESCRIPTION

12

TRANSCRIPT

Page 1: List

Sample Data Structures Questions Chapter 5

Linked Lists

Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch Second Edition ISBN 0-201-70297-5, Softcover, 816 pages, 2000

The Purpose of These Questions

These are typical exam questions from Chapter 5 of the textbook. These exact questions might not be on your exam, but if you research and find the right answers to these questions, that should be good preparation for a real exam. (It's also possible that some of this material was not covered in your class.) There are 29 short answer questions and 16 multiple choice questions in this file.

Short Answers

Short Answers Sections 5.1-5.2 Linked List Fundamentals

The node definition: class node{ // TYPEDEF typedef double value_type;

// CONSTRUCTOR node( const value_type& init_data = value_type( ), node* init_link = NULL ) { data_field = init_data; link_field = init_link; }

// Member functions to set the data and link fields: void set_data(const value_type& new_data) { data_field = new_data; } void set_link(node* new_link) { link_field = new_link; }

// Constant member function to retrieve the current data:

value_type data( ) const { return data_field; }

// Two slightly different member functions to retreive

// the current link:

Page 2: List

const node* link( ) const { return link_field; } node* link( ) { return link_field; }

private:value_type data_field;node* link_field;

};};

1. What is the one statement that can be used to insert a new node at the head of a linked list. Assume that the list's head_pointer is called head_ptr and the that the data for the new node is called entry.

2. Suppose that p is a pointer to a node in a linked list, and *p is not the tail node. What are the steps to removing the node after *p? Use one short English sentence for each step.

3. Suppose we are using the usual node definition (with member functions called data and link). Your program is using a node* variable called head_ptr to point to the first node of a linked list (or head_ptr == NULL for the empty list). Write a few lines of C++ code that will print all the double numbers on the list.

4. Suppose we are using the usual node definition (with member functions called data and link), and that locate_ptr is pointing to a node in a linked list. Write a statement that will make locate_ptr point to the next node in the list (if there is one). If there is no next node, then your statement should set locate_ptr to NULL.

5. Implement the following function as a new function for the linked list toolkit. (Use the usual node definition with member variables called data and link.)

6. size_t count_42s(const node* head_ptr);7. // Precondition: head_ptr is the head pointer of a linked

list.8. // The list might be empty or it might be non-empty.9. // Postcondition: The return value is the number of

occurrences10. // of 42 in the data field of a node on the linked list.11. // The list itself is unchanged.12. Implement the following function as a new function for the linked list toolkit.

(Use the usual node definition with member variables called data and link.) 13. bool has_42(const node* head_ptr);14. // Precondition: head_ptr is the head pointer of a linked

list.15. // The list might be empty or it might be non-empty.16. // Postcondition: The return value is true if the list has

at least17. // one occurrence of the number 42 in the data part of a

node.18. Implement the following function as a new function for the linked list toolkit.

(Use the usual node definition with member variables called data and link.) 19. bool all_42s(const node* head_ptr);20. // Precondition: head_ptr is the head pointer of a linked

list.21. // The list might be empty or it might be non-empty.22. // Postcondition: The return value is true if every node in

the

Page 3: List

23. // list contains 42 in the data part. NOTE: If the list is empty,

24. // then the function returns true.25. Implement the following function as a new function for the linked list toolkit.

(Use the usual node definition with member variables called data and link. The data field is an int.)

26. int sum(const node* head_ptr);27. // Precondition: head_ptr is the head pointer of a linked

list.28. // The list might be empty or it might be non-empty.29. // Postcondition: The return value is the sum of all the

data components30. // of all the nodes. NOTE: If the list is empty, the

function returns 0.31. Implement the following function as a new function for the linked list toolkit.

(Use the usual node definition with member variables called data and link. The data field is an int.)

32. int product(const node* head_ptr);33. // Precondition: head_ptr is the head pointer of a linked

list.34. // The list might be empty or it might be non-empty.35. // Postcondition: The return value is the product of all the

data components36. // of all the nodes. NOTE: If the list is empty, the

function returns 1.37. Implement the following function as a new function for the linked list toolkit.

(Use the usual node definition with member variables called data and link.) 38. void list_tail_insert(node* head_ptr, const

node::value_type& entry);39. // Precondition: head_ptr is the head pointer of a non-empty40. // linked list.41. // Postcondition: A new node has been added at the tail end42. // of the list. The data in the new node is taken from the43. // parameter called entry.44. Implement the following function as a new function for the linked list toolkit.

(Use the usual node definition with member variables called data and link.) 45. bool data_is_on(const node* head_ptr, const node* p);46. // Precondition: head_ptr is the head pointer of a linked

list47. // (which might be empty, or might be non-empty). The

pointer p48. // is a non-NULL pointer to some node on some linked list.49. // Postcondition: The return value is true if the data in *p50. // appears somewhere in a data field of a node in head_ptr's51. // linked list. Otherwise the return value is false.52. // None of the nodes on any lists are changed.53. Implement the following function as a new function for the linked list toolkit.

(Use the usual node definition with member variables called data and link.) 54. bool is_on(const node* head_ptr, const node* p);55. // Precondition: head_ptr is the head pointer of a linked

list56. // (which might be empty, or might be non-empty). The

pointer p57. // is a non-NULL pointer to some node on some linked list.

Page 4: List

58. // Postcondition: The return value is true if p actually points to

59. // one of the nodes in the head_ptr's linked list. For example,

60. // p might point to the head node of this list, or the second node,

61. // or the third node, and so on. Otherwise the return value is

62. // false. None of the nodes on any lists are changed.63. Implement the following function as a new function for the linked list toolkit. Do

not call any of the other toolkit functions from within your implementation. (Use the usual node definition with member variables called data and link.)

64. void list_insert_zero(node* previous_ptr);65. // Precondition: previous_ptr is a pointer to a node on a

linked list.66. // Postcondition: A new node has been added to the list

after67. // the node that previous_ptr points to. The new node

contains 0.68. Suppose that p, q, and r are all pointers to nodes in a linked list with 15 nodes.

The pointer p points to the first node, q points to the 8th node, and r points to the last node. Write a few lines of code that will make a new copy of the list. You code should set THREE new pointers called x, y, and z so that: x points to the first node of the copy, y points to the 8th node of the copy, and z points to the last node of the copy. Your code may NOT contain any loops, but it can use the toolkit functions.

69. Suppose that a_ptr and b_ptr are node pointers. Write one clear sentence to tell me when the expression (a_ptr==b_ptr) will be true.

70. Compare the worst-case big-O time analysis for these two functions: The insert function for the bag that is implemented using a fixed-sized array, and the insert function for the bag that is implemented using a linked list.

71. Compare the worst-case big-O time analysis for these two functions: The erase_one function for the bag that is implemented using a fixed-sized array, and the erase_one function for the bag that is implemented using a linked list.

72. Tell me about one of the sequence operations that is more efficient because the class keeps a tail pointer as a private member variable. Provide a specific example showing why the operation would be less efficient without the tail pointer.

73. Tell me about one of the sequence operations that is easier to program because the class keeps a precursor (rather than just a cursor). Provide a specific example showing why the operation would be harder to program without the precursor.

74. Compare the worst-case big-O time analysis for these two functions: The insert function for the sequence that is implemented using a fixed-sized array, and the insert function for the sequence that is implemented using a linked list.

75. Compare the worst-case big-O time analysis for these two functions: The remove function for the sequence that is implemented using a fixed-sized array, and the remove function for the sequence that is implemented using a linked list.

Short Answers Section 5.3

The Bag ADT with a Linked List

Short Answers Section 5.4

The Sequence ADT with a Linked List

Page 5: List

Short AnswerSection 5.5 Dynamic Arrays vs. Linked Lists vs.

Doubly Linked Lists

class dnode { public:

// CONSTRUCTOR: Creates a dnode containing a specified initial values.

dnode( int init_data = 0, dnode* init_fore = NULL, dnode* init_back = NULL ) {

data_field = init_data;link_fore = init_fore;link_back = init_back;

}

// Member functions to set the fields: void set_data(int new_data)

{ data_field = new_data; } void set_fore(dnode* new_fore)

{ link_fore = new_fore; } void set_back(dnode* new_back)

{ link_back = new_back; }

// Const member functions to retrieve current data:

int data( ) const { return data_field; }

// Two slightly different member functions to retrieve each link:

const dnode* fore( ) const { return link_fore; } dnode* fore( ) { return link_fore; }

const dnode* back( ) const { return link_back; } dnode* back( ) { return link_back; }

private:int data_field;dnode *link_fore;dnode *link_back;

};

76. Write one sentence to describe a situation when a doubly linked list is appropriate. 77. Describe a situation where storing items in an array is clearly better than storing

items on a linked list. 78. Implement the following function using the dnode class that is shown above. It is

NOT a member function). 79. int sum_data(const dnode* head_ptr);80. // PRECONDITION: head_pointer is a non-NULL head pointer for a

linked list.81. // POSTCONDITION: The return value is the sum of all the data

fields82. // of the dnodes in the linked list.

Page 6: List

83. Implement the following function using the dnode class that is shown above. It is NOT a member function.

84. dnode* find_data(dnode* head_ptr, int target)85. // PRECONDITION: head_pointer is a non-NULL head pointer for a

linked list.86. // POSTCONDITION: The return value is a pointer to the first

dnode in87. // the linked list that contains the target as its data. If

there is no88. // such dnode, then the return value is NULL.89. Implement the following function using the dnode class that is shown above. It is

NOT a member function. Your function should not cause a heap leak. 90. void delete_dnode(dnode* p)91. // PRECONDITION: p is a non-NULL pointer to a dnode in a linked

list.92. // It is neither the first nor the last dnode.93. // POSTCONDITION: The dnode *p has been removed from the list.94. Implement the following function using the dnode class that is shown above. It is

NOT a member function. 95. void insert_dnode(dnode* p, int new_data)96. // PRECONDITION: p is a non-NULL pointer to a dnode in a linked

list.97. // It is neither the first nor the last dnode.98. // POSTCONDITION: A new dnode with new_data has been inserted

into the99. // linked list after *p.100. Suppose that a program has declared these three variables using the dnode

class shown above. 101. dnode *b;102. dnode *h;103. dnode *z;

After executing part of the program, the pointer b is a head pointer for a non-empty linked list. Write some code that can be placed in the program at this point to make a complete copy of b's linked list. At the end of your code, b should be unchanged, h should be the head pointer of the new list, and z should be the tail pointer of the new list. If necessary, you may declare other dnode * variables to use in your algorithm.

104. This question is appropriate if you have implemented the polynomial with a linked list from www.cs.colorado.edu/~main/projects/chap05d.html. In my dynamic polynomial, the set_recent function includes this line:

105. recent_ptr = recent_ptr->fore( );

A. Describe the effect of this statement in one clear English sentence. B. The set_recent function is a const member function, which means that normally it is not permitted to change any member variables. Explain why the compiler permits set_recent to change the recent_ptr member variable.

Multiple Choice

Page 7: List

1. Suppose cursor points to a node in a linked list (using the node definition with member functions called data and link). What statement changes cursor so that it points to the next node?

o A. cursor++; o B. cursor = link( ); o C. cursor += link( ); o D. cursor = cursor->link( );

2. Suppose cursor points to a node in a linked list (using the node definition with member functions called data and link). What Boolean expression will be true when cursor points to the tail node of the list?

o A. (cursor == NULL) o B. (cursor->link( ) == NULL) o C. (cursor->data( ) == NULL) o D. (cursor->data( ) == 0.0) o E. None of the above.

3. Why does our node class have two versions of the link member function? o A. One is public, the other is private. o B. One is to use with a const pointer, the other with a regular pointer. o C. One returns the forward link, the other returns the backward link. o D. One returns the data, the other returns a pointer to the next node.

4. Suppose that p is a pointer variable that contains the NULL pointer. What happens if your program tries to read or write *p?

o A. A syntax error always occurs at compilation time. o B. A run-time error always occurs when *p is evaluated. o C. A run-time error always occurs when the program finishes. o D. The results are unpredictable.

5. Suppose that f is a function with a prototype like this: 6. void f(________ head_ptr);7. // Precondition: head_ptr is a head pointer for a linked list.8. // Postcondition: The function f has done some computation

with9. // the linked list, but the list itself is unchanged.

What is the best data type for head_ptr in this function?

o A. node o B. const node o C. node* o D. const node*

10. Suppose that f is a function with a prototype like this: 11. void f(________ head_ptr);12. // Precondition: head_ptr is a head pointer for a linked

list.13. // Postcondition: The function f has done some manipulation

of14. // the linked list, and the list might now have a new head

node.

Multiple Choice Sections 5.1-5.2

Linked List Fundamentals

Page 8: List

What is the best data type for head_ptr in this function?

o A. node o B. node& o C. node* o D. node*&

15. In the linked list version of the bag class a member variable many_nodes is used to keep track of how long the linked list is. Why not just make a call to the list toolkit function list_length()?

o A. The list_length() function is O(n) and the alternative is O(1). o B. The list_length() function is private. o C. The list_length() function results in an infinite loop for circular lists. o D. The list_length() function works only for lists of integers.

16. Suppose that the bag is implemented with a linked list. Which of these operations are likely to have a constant worst-case time?

o A. insert o B. count o C. erase_one o D. None of (A), (B), and (C) have a constant worst-case time o E. TWO of (A), (B), and (C) have a constant worst-case time o F. ALL of (A), (B), and (C) have a constant worst-case time

17. The bag class in Chapter 5 has a new grab member function that returns a randomly selected item from a bag (using a pseudorandom number generator). Suppose that you create a bag, insert the numbers 1, 2, and 3, and then use the grab function to select an item. Which of these situations is most likely to occur if you run your program 300 times (from the beginning):

o A. Each of the three numbers will be selected about 100 times. o B. One of the numbers will be selected about 200 times; another number

will be selected about 66 times; the remaining number will be selected the rest of the time.

o C. One of the numbers will be selected 300 times; the other two won't be selected at all.

18. What is the expression for generating a pseudorandom number in the range 1...N? o A. rand() % N; o B. rand() / N; o C. rand() % (N + 1); o D. rand() / (N + 1); o E. (rand() % N) + 1;

19. Which expression computes a pseudorandom integer between -10 and 10 using rand() from stdlib.h?

o A. (rand( ) % 20) - 10 o B. (rand( ) % 21) - 10 o C. (rand( ) % 22) - 10 o D. (rand( ) % 20) - 11 o E. (rand( ) % 21) - 11

Multiple ChoiceSection 5.3

The Bag ADT with with a Linked List

Page 9: List

20. For the linked-list version of the sequence ADT, which operations are constant time operations in the worst case?

o A. attach is constant time, but not insert o B. insert is constant time, but not attach o C. Both attach and insert are constant time o D. Neither attach nor insert are constant time

21. Suppose that the sequence is implemented with a linked list. Which of these operations are likely to have a constant worst-case time?

o A. insert o B. size o C. remove_current o D. None of (A), (B), and (C) have a constant worst-case time o E. TWO of (A), (B), and (C) have a constant worst-case time o F. ALL of (A), (B), and (C) have a constant worst-case time

22. What is the output of these statements, using your sequence ADT implemented as a linked list with Item defined as integer:

23. sequence x;24. sequence y;25. x.insert(41); // Inserts 41 into the sequence x26. x.insert(42); // Inserts 42, so that x is now 42, 41 with

cursor at front27. y = x;28. x.attach(43); // Attaches 43 so that x is now 42, 43, 41

with cursor at 4329. y.advance( );30. cout << "y size is " << y.size( );31. cout << " and y current item is " << y.current( ) << endl;

o A. y size is 2 and y current item is 41. o B. y size is 2 and y current item is 43. o C. y size is 3 and y current item is 41. o D. y size is 3 and y current item is 43. o E. None of the above.

32. Suppose that you forgot to override the assignment operator in your sequence ADT implemented as a linked list. What is the most likely output from the previous question?

o A. y size is 2 and y current item is 41. o B. y size is 2 and y current item is 43. o C. y size is 3 and y current item is 41. o D. y size is 3 and y current item is 43. o E. None of the above.

33. What kind of list is best to answer questions such as "What is the item at position n?"

o A. Lists implemented with an array. o B. Doubly-linked lists. o C. Singly-linked lists. o D. Doubly-linked or singly-linked lists are equally best

Multiple ChoiceSection 5.4

The List ADT with a Linked List

Multiple ChoiceSection 5.5

Dynamic Arrays vs. Linked Lists vs.

Doubly Linked Lists

Page 10: List

Data Structures and Other Objects Using C++

Michael Main ([email protected]) and Walter Savitch ([email protected])

Thank you for visiting http://www.cs.colorado.edu/~main/questions/chap05q.html Copyright © 2000 Addison-Wesley Computer and Engineering Publishing Group