Description

Sample Data Structures Questions Chapter 5 Linked ListsData 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: const node* link( ) const { return 22. What are the steps to removing the node after *p? Use one short English sentence for each step. // Precondition: head_ptr is the head pointer of a linked list. Write a few lines of C++ code that will print all the double numbers on the list. 16. 14. 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). // of 42 in the data field of a node on the linked list. 3. 18. Implement the following function as a new function for the linked list toolkit. // The list itself is unchanged.) 6. 21. // Postcondition: The return value is the number of occurrences 10. 2. 4. 15. Suppose we are using the usual node definition (with member functions called data and link). If there is no next node. }. 11. Suppose we are using the usual node definition (with member functions called data and link). list. 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. { return 1. (Use the usual node definition with member variables called data and link. // Precondition: head_ptr is the head pointer of a linked // The list might be empty or it might be non-empty. Implement the following function as a new function for the linked list toolkit. bool all_42s(const node* head_ptr). size_t count_42s(const node* head_ptr). // Postcondition: The return value is true if every node in . // Postcondition: The return value is true if the list has at least 17. 7.) 19. What is the one statement that can be used to insert a new node at the head of a linked list. // The list might be empty or it might be non-empty. 8. bool has_42(const node* head_ptr). node* link_field. the list. } node* link( ) link_field. then your statement should set locate_ptr to NULL. 20. (Use the usual node definition with member variables called data and link. // Precondition: head_ptr is the head pointer of a linked // The list might be empty or it might be non-empty. Assume that the list's head_pointer is called head_ptr and the that the data for the new node is called entry. Write a statement that will make locate_ptr point to the next node in the list (if there is one). Suppose that p is a pointer to a node in a linked list. } private: value_type data_field. }. 5.) 13. // one occurrence of the number 42 in the data part of a node.link_field. 9. and that locate_ptr is pointing to a node in a linked list. 12. and *p is not the tail node. // None of the nodes on any lists are changed. the function returns 1. int sum(const node* head_ptr). // Postcondition: The return value is the product of all the data components 36. // is a non-NULL pointer to some node on some linked list. 37. const node* p).23. Implement the following function as a new function for the linked list toolkit. // is a non-NULL pointer to some node on some linked list. list bool is_on(const node* head_ptr.) 45. 41. The data field is an int. The data field is an int. 25. The data in the new node is taken from the 43. 34. // Precondition: head_ptr is the head pointer of a linked // The list might be empty or it might be non-empty. Implement the following function as a new function for the linked list toolkit. // Precondition: head_ptr is the head pointer of a linked // (which might be empty. 31.) 32. // Precondition: head_ptr is the head pointer of a linked // (which might be empty. NOTE: If the list is empty. 44. 29. // Postcondition: The return value is the sum of all the data components 30. list. NOTE: If the list is empty. // of all the nodes. // then the function returns true.) 26. 24. Implement the following function as a new function for the linked list toolkit. 52. The pointer p 57. (Use the usual node definition with member variables called data and link. // of the list. 39. (Use the usual node definition with member variables called data and link. const node::value_type& entry). (Use the usual node definition with member variables called data and link. 27.) 54. 47. 56. // appears somewhere in a data field of a node in head_ptr's 51. // Postcondition: The return value is true if the data in *p 50. or might be non-empty). 35. 53. NOTE: If the list is empty. . 55. Implement the following function as a new function for the linked list toolkit. // Precondition: head_ptr is the head pointer of a non-empty 40. (Use the usual node definition with member variables called data and link. 28. 49. Otherwise the return value is false. int product(const node* head_ptr). // Precondition: head_ptr is the head pointer of a linked // The list might be empty or it might be non-empty. // linked list. 46. // of all the nodes. (Use the usual node definition with member variables called data and link. list bool data_is_on(const node* head_ptr. // linked list. 33.) 38. list. // Postcondition: A new node has been added at the tail end 42. // parameter called entry. // list contains 42 in the data part. the function returns 0. const node* p). void list_tail_insert(node* head_ptr. Implement the following function as a new function for the linked list toolkit. The pointer p 48. or might be non-empty). Write one clear sentence to tell me when the expression (a_ptr==b_ptr) will be true. 70. Do not call any of the other toolkit functions from within your implementation. q. 65. and z so that: x points to the first node of the copy. The new node contains 0. Your code may NOT contain any loops. Implement the following function as a new function for the linked list toolkit. q points to the 8th node.3 The Bag ADT with a Linked List node of the copy. but it can use the toolkit functions. // Postcondition: A new node has been added to the list after 67. // Postcondition: The return value is true if p actually points to 59. // false.58. and the insert function for the bag that is implemented using a linked list. and Short Answers Section 5. 66. Tell me about one of the sequence operations that is more efficient because the class keeps a tail pointer as a private member variable. 72. void list_insert_zero(node* previous_ptr). You code should set THREE new pointers called x. // p might point to the head node of this list. // or the third node. . 69. (Use the usual node definition with member variables called data and link. The pointer p points to the first node. 63. 68. y. and so on. None of the nodes on any lists are changed. For example. 61. Write a few lines of code that will make a new copy of the list. or the second node. 60. 71. // Precondition: previous_ptr is a pointer to a node on a linked list. Otherwise the return value is 62. // one of the nodes in the head_ptr's linked list. Suppose that p. Suppose that a_ptr and b_ptr are node pointers.) 64. // the node that previous_ptr points to. 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 r are all pointers to nodes in a linked list with 15 nodes. y points to the 8th node of the copy.4 The Sequence ADT with a Linked List the erase_one function for the bag that is implemented using a linked list. and z points to the last Short Answers Section 5. Provide a specific example showing why the operation would be less efficient without the tail pointer. and r points to the last node. 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. 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. } private: int data_field. . dnode* init_fore = NULL. and the insert function for the sequence that is implemented using a linked list. } dnode* fore( ) { return link_fore. dnode *link_fore. Linked Lists vs. 74. } Short Answer Section 5. link_fore = init_fore. } // Const member functions to retrieve current int data( ) const { return data_field.73. 75. } void set_fore(dnode* new_fore) { link_fore = new_fore. and the remove function for the sequence that is implemented using a linked list. 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. }. Provide a specific example showing why the operation would be harder to program without the precursor. dnode* init_back = NULL ) { data_field = init_data. dnode *link_back. } void set_back(dnode* new_back) { link_back = new_back. class dnode { public: // CONSTRUCTOR: Creates a dnode containing a specified initial values. dnode( int init_data = 0. Tell me about one of the sequence operations that is easier to program because the class keeps a precursor (rather than just a cursor). link_back = init_back. } // Two slightly different member functions to retrieve each link: const dnode* fore( ) const { return link_fore.5 Dynamic Arrays vs. Doubly Linked data: Lists // Member functions to set the fields: void set_data(int new_data) { data_field = new_data. } const dnode* back( ) const { return link_back. } dnode* back( ) { return link_back. It is NOT a member function). // It is neither the first nor the last dnode. 103. // POSTCONDITION: The dnode *p has been removed from the list. // of the dnodes in the linked list. // It is neither the first nor the last dnode. you may declare other dnode * variables to use in your algorithm. It is NOT a member function. Implement the following function using the dnode class that is shown above. int sum_data(const dnode* head_ptr). dnode *z. 86. Implement the following function using the dnode class that is shown above. 101. 77. 83. 97. 92. After executing part of the program. // PRECONDITION: p is a non-NULL pointer to a dnode in a linked list. // POSTCONDITION: The return value is the sum of all the data fields 82. 89. It is 95. Implement the following function using the dnode class that is shown above. dnode* find_data(dnode* head_ptr. Describe a situation where storing items in an array is clearly better than storing items on a linked list. void insert_dnode(dnode* p. If there is no 88. If necessary. Write one sentence to describe a situation when a doubly linked list is appropriate. 94. // POSTCONDITION: The return value is a pointer to the first dnode in 87. 102. the pointer b is a head pointer for a nonempty linked list. int target) 85. h should be the head pointer of the new list. 78. It is NOT a member function. 80.76. 79. // PRECONDITION: head_pointer is a non-NULL head pointer for a linked list. 98. Implement the following function using the dnode class that is shown above. Suppose that a program has declared these three variables using the dnode NOT a member function. class shown above. dnode *h. Your function should not cause a heap leak. dnode *b. // the linked list that contains the target as its data. . // PRECONDITION: p is a non-NULL pointer to a dnode in a linked list. // linked list after *p. and z should be the tail pointer of the new list. then the return value is NULL. Write some code that can be placed in the program at this point to make a complete copy of b's linked list. 100. 90. int new_data) 96. // POSTCONDITION: A new dnode with new_data has been inserted into the 99. 93. At the end of your code. 84. void delete_dnode(dnode* p) 91. // PRECONDITION: head_pointer is a non-NULL head pointer for a linked list. 81. // such dnode. b should be unchanged. // Postcondition: The function f has done some computation // the linked list. Suppose that f is a function with a prototype like this: 6. 4. The results are unpredictable. Suppose that p is a pointer variable that contains the NULL pointer. the other returns a pointer to the next node.104. recent_ptr = recent_ptr->fore( ). the other returns the backward link.cs. The set_recent function is a const member function. Why does our node class have two versions of the link member function? o A. with What is the best data type for head_ptr in this function? . o D. 3. (cursor == NULL) o B. A. Suppose cursor points to a node in a linked list (using Multiple Choice the node definition with member functions called data Sections 5. Explain why the compiler permits set_recent to change the recent_ptr member variable. o D.colorado. Suppose cursor points to a node in a linked list (using the node definition with member functions called data and link). 2. cursor += link( ). A run-time error always occurs when *p is evaluated. o C. What statement changes cursor so that it Linked List Fundamentals points to the next node? o A. but the list itself is unchanged. o C. 9. B. cursor = cursor->link( ). (cursor->data( ) == NULL) o D. (cursor->data( ) == 0. (cursor->link( ) == NULL) o C. 8. None of the above. o C. This question is appropriate if you have implemented the polynomial with a linked list from www.1-5.0) o E. Multiple Choice 1. A run-time error always occurs when the program finishes. In my dynamic polynomial. One returns the data. 5. o D.2 and link). cursor = link( ). Describe the effect of this statement in one clear English sentence. o B. o B. One returns the forward link. A syntax error always occurs at compilation time. o B. the other is private. // Precondition: head_ptr is a head pointer for a linked list. What Boolean expression will be true when cursor points to the tail node of the list? o A. cursor++. One is public.html. the other with a regular pointer. void f(________ head_ptr). which means that normally it is not permitted to change any member variables. What happens if your program tries to read or write *p? o A.edu/~main/projects/chap05d. the set_recent function includes this line: 105. 7. One is to use with a const pointer. list. Suppose that you create a bag. Each of the three numbers will be selected about 100 times. node*& 15. 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). Which of these situations is most likely to occur if you run your program 300 times (from the beginning): o A. . erase_one o D. 16. of node. const node* 10. node* D. Suppose that the bag is implemented with a linked list. 12. (B). and (C) have a constant worst-case time 17. (B). const node C. node B. The list_length() function works only for lists of integers. (B). insert the numbers 1. o C. The list_length() function is private. and (C) have a constant worst-case time o F. node B. Why not just make a call to the list toolkit function list_length()? o A.3 The Bag ADT with with a Linked List o D. count o C.A. 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. ALL of (A). o B. Suppose that f is a function with a prototype like this: o o o o 11. 13. and (C) have a constant worst-case time o E. node* Multiple Choice Section 5. and the list might now have a new head What is the best data type for head_ptr in this function? o o o A. and 3. TWO of (A). node& C. The list_length() function results in an infinite loop for circular lists. 2. None of (A). and then use the grab function to select an item. insert o B. The list_length() function is O(n) and the alternative is O(1). void f(________ head_ptr). 14. o D. // Precondition: head_ptr is a head pointer for a linked // Postcondition: The function f has done some manipulation // the linked list. Which of these operations are likely to have a constant worst-case time? o A. What is the expression for generating a pseudorandom number in the range 1. ALL of (A). the other two won't be selected at all. o E. One of the numbers will be selected about 200 times. another number will be selected about 66 times. but not insert o B. rand() % N.insert(42). 19. sequence x. Neither attach nor insert are constant time 21. with cursor at 43 // Inserts 41 into the sequence x // Inserts 42.N? o A. rand() / (N + 1). 25. insert is constant time.10 o B. o D. Both attach and insert are constant time o D. remove_current o D. What is the output of these statements. rand() % (N + 1).insert(41). Suppose that the sequence is implemented with a linked list. insert o B. (rand( ) % 21) . x. rand() / N. attach is constant time. (rand( ) % 20) . (rand( ) % 21) .h? o A. x.4 The List ADT with a Linked List D. and (C) have a constant worst-case time o E.11 20. 41 . so that x is now 42. using your sequence ADT implemented as a linked list with Item defined as integer: o o 23. o B.B. o C. which operations are constant time operations in the worst case? o A. the remaining number will be selected the rest of the time.11 E. 24. and (C) have a constant worst-case time o F. (B). (B). Which expression computes a pseudorandom integer between -10 and 10 using rand() from stdlib. 43.. None of (A). Which of these operations are likely to have a constant worst-case time? o A.10 o C.10 o Multiple Choice Section 5. and (C) have a constant worst-case time 22. y = x. (rand( ) % 22) . TWO of (A). size o C. (rand( ) % 20) . sequence y. For the linked-list version of the sequence ADT. (rand() % N) + 1. 18. One of the numbers will be selected 300 times. but not attach o C. x. (B). cursor at front 27..attach(43). o C. 26. 28. 41 with // Attaches 43 so that x is now 42. E.29. Lists implemented with an array. y size is 3 and y current item is 43.advance( ). cout << "y size is " << y. None of the above. B. Linked Lists vs. D. Doubly-linked or singly-linked lists are equally best o o Data Structures and Other Objects Using C++ Michael Main (main@colorado. y size is 2 and y current item is 43. Multiple Choice Section 5. y size is 2 and y current item is 43. 32. Singly-linked lists.current( ) << endl.5 Dynamic Arrays vs. E. cout << " and y current item is " << y. o D. None of the above. Doubly-linked lists. Suppose that you forgot to override the assignment operator in your sequence ADT implemented as a linked list.edu) and Walter Savitch ([email protected] Copyright © 2000 Addison-Wesley Computer and Engineering Publishing Group . 33.edu/~main/questions/chap05q. y size is 2 and y current item is 41. Doubly Linked Lists D. C. y size is 3 and y current item is 41.size( ). y size is 3 and y current item is 41. y size is 3 and y current item is 43.cs.edu) Thank you for visiting http://www. 31. y size is 2 and y current item is 41. o C. o B. o B. o C. 30. A. What is the most likely output from the previous question? o A. o o o o o y.colorado. What kind of list is best to answer questions such as "What is the item at position n?" o A.
Copyright © 2024 DOKUMEN.SITE Inc.