Starting Out with C++: Early Objects (9th Edition)
Starting Out with C++: Early Objects (9th Edition)
9th Edition
ISBN: 9780134400242
Author: Tony Gaddis, Judy Walters, Godfrey Muganda
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 17, Problem 17RQE

A)

Explanation of Solution

Purpose of the given code:

The given code is trying to print the members of a linked list by traversing through the entire list by using a destructor. A destructor is called when the program ends or the destructor function calls.

Given Code:

//Definition of destructor

NumberList::printList()//Line 1

{//Line 2

//loop

//Error line3

while(head)//Line 3

{//Line 4

/*Print the data value of the node while traversing through the list*/

cout<<head->value; //Line5

/*the pointer is moved one position ahead till end of list */

head= head->next;//Line6

}//Line 7

}//Line 8

Error in the given code:

  • In “line 3”, use of the head pointer to walk down the list destroys the list.
    • This should be written as an “auxiliary pointer”. So, correct code is given below:

      ListNode *nodePtr = head

          while (nodePtr != null)

                    ;&#x...

B)

Explanation of Solution

Purpose of the given code:

The given code is trying to print the members of a linked list by traversing through the entire list by using a destructor. A destructor is called when the program ends or the destructor function calls.

Given Code:

//Definition of destructor

NumberList::PrintList()//Line 1

{//Line 2

//Declaration of structure pointer variables

//Line3

ListNode *p =head; /*the start or head of the list is stored in p */

//loop

     //Error Line4

while (p->next) //Line4

{//Line 5

/*Print the data value of node p while traversing through the linklist */

//Line6

cout<<p->value; /*print the individual  data values of each node */

//Line7

p=p->next; //the pointer is moved one position ahead till end of list

}//Line8

}//Line9

Error in the given code:

  • In “line 4”, eventually the pointer p becomes “NULL”, at which time the attempt to access p->NULL will result in an error.
    • This should be written by replacing the text p->next in the while loop with p.

while (p) //Line4

/*Here the loop will traverse till p exists or   till p is not NULL */

  • The function fails to declare a return type of void...

C)

Explanation of Solution

Purpose of the given code:

The given code is trying to print the members of a linked list by traversing through the entire list by using a destructor. A destructor is called when the program ends or the destructor function calls.

Given Code:

//Definition of destructor

NumberList::PrintList()//Line 1

{//Line 2

//Declaration of structure pointer variables

//Line3

ListNode *p =head; /*the start or head of the list is stored in p */

//loop

     // Line4

while (p) //Line4

{//Line 5

/*Print the data value of node p while traversing through the linklist */

//Line6

cout<<p->value; /*print the individual  data values of each node */

          //Error Line7

//Line7

p++//the pointer is incremented by one position

}//Line8

}//Line9

Error in the given code:

  • In “line 7”, the function uses p++ erroneously in place of p=p->next when attempting to move to the next node in the list. This is not possible as increment operator can work only on variables containing data values but here p is a node of a link list which contains an address value pointer pointing to the next list along with a data value.
    • This should be written by replacing the text p++ in the while loop body with p=p->next...

D)

Explanation of Solution

Purpose of the given code:

The given code is trying to destroy the members of a linked list by using a destructor. A destructor is called when the program ends or the destructor function calls.

Given Code:

//Definition of destructor

NumberList::~NumberList()//Line 1

{//Line 2

//Declaration of structure pointer variables

ListNode *nodePtr, *nextNode;//Line 3

//Storing "head" pointer into "nodePtr"

nodePtr = head;//Line 4

//loop

while (nodePtr != nullptr)//Line 5

{//Line 6

//Assign address of next into "nextNode"

nextNode = nodePtr->next;//Line 7

//Error

nodePtr->next=nullptr;//Line 8

//Assign nextNode into "nodePtr"

nodePtr = nextNode;//Line 9

}//Line 10

}//Line 11

Error in the given code:

In “line 8”, the address of “next” in “nodePtr” is assigned as “nullptr”.

  • This should be written as “delete nodePtr” to delete the value of node from the list, because, “delete” operator is used to free the memory space allocated by the list...

Blurred answer
Students have asked these similar questions
data structure-JAVA
struct insert_at_back_of_dll { // Function takes a constant Book as a parameter, inserts that book at the // back of a doubly linked list, and returns nothing. void operator()(const Book& book) { / // TO-DO (2) |||| // Write the lines of code to insert "book" at the back of "my_dll". // // // END-TO-DO (2) ||| } std::list& my_dll; };
struct remove_from_front_of_dll { // Function takes no parameters, removes the book at the front of a doubly // linked list, and returns nothing. void operator()(const Book& unused) { //// TO-DO (13) |||| // Write the lines of code to remove the book at the front of "my_dll", // // Remember, attempting to remove an element from an empty data structure is // a logic error. Include code to avoid that. ///// END-TO-DO (13) //// } std::list& my_dll; };
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education