Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please explain why? The following statements pertain to the code snippet on the right. Circle all that apply. a) This code segment will not compile.

Please explain why?image text in transcribed

The following statements pertain to the code snippet on the right. Circle all that apply. a) This code segment will not compile. b) This code segment will not properly initialize the head and tail. c) This code segment will not properly initialize count. d) The head and tail ListNodes should not be dynami- cally allocated. e) "List::" should not precede the method name. List :: List() { ListNode *head = new ListNode(); ListNode *tail = new ListNode (); head->next = tail; tail->previous = head; count = 0; The following statements pertain to the code snippet on the right. Circle all that apply. a) This code segment will not compile. b) This code segment will always segfault. c) This code segment will exhibit undefined behavior, but is likely to work correctly because of how memory deal- location works. d) "pos.current" should be changed to pos->current, and pos.moveForward() should be changed to pos->moveForward(). e) The line "delete pos;" needs to be added once we're done using pos to avoid memory leaks. void List :: makeEmpty() { ListItr pos (head); pos. moveForward(); while (!pos. is PastEnd()) { pos. current->next->previous = head; delete head->next; pos. moveForward(); head->next = pos. current; The statements in this method are out of order! Re-order lines 1-4 to fix the method by writing the labels 1-4 in the correct order. (There may be more than one correct order. In that case, only write one solution). Please write your answer as a list of numbers (e.g., 1-2-3-4). void List :: insert After (int x, ListItr position) { ListNode *insert = new ListNode(); insert ->value = x; (1) insert->previous = position.current; (2) position.current->next = insert; (3) insert->next->previous = insert; (4) insert ->next = position.current->next; count++; The following statements pertain to the code snippet on the right. Circle all that apply. a) This code segment will not compile. b) This code segment will not properly initialize the head and tail. c) This code segment will not properly initialize count. d) The head and tail ListNodes should not be dynami- cally allocated. e) "List::" should not precede the method name. List :: List() { ListNode *head = new ListNode(); ListNode *tail = new ListNode (); head->next = tail; tail->previous = head; count = 0; The following statements pertain to the code snippet on the right. Circle all that apply. a) This code segment will not compile. b) This code segment will always segfault. c) This code segment will exhibit undefined behavior, but is likely to work correctly because of how memory deal- location works. d) "pos.current" should be changed to pos->current, and pos.moveForward() should be changed to pos->moveForward(). e) The line "delete pos;" needs to be added once we're done using pos to avoid memory leaks. void List :: makeEmpty() { ListItr pos (head); pos. moveForward(); while (!pos. is PastEnd()) { pos. current->next->previous = head; delete head->next; pos. moveForward(); head->next = pos. current; The statements in this method are out of order! Re-order lines 1-4 to fix the method by writing the labels 1-4 in the correct order. (There may be more than one correct order. In that case, only write one solution). Please write your answer as a list of numbers (e.g., 1-2-3-4). void List :: insert After (int x, ListItr position) { ListNode *insert = new ListNode(); insert ->value = x; (1) insert->previous = position.current; (2) position.current->next = insert; (3) insert->next->previous = insert; (4) insert ->next = position.current->next; count++

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logistics Lifeline Supply Chain Strategies

Authors: Ehsan Sheroy

1st Edition

7419377502, 978-7419377503

More Books

Students also viewed these Databases questions