STARTING OUT WITH C++ MPL
STARTING OUT WITH C++ MPL
9th Edition
ISBN: 9780136673989
Author: GADDIS
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
What are the steps you will follow in order to check the database and fix any problems with it and normalize it?  Give two references with your answer.
What are the steps you will follow in order to check the database and fix any problems with it? Have in mind that you SHOULD normalize it as well. Consider that the database offline is not allowed since people are connected to it and personal data might be bridged and not secured. Provide three refernces with you answer.
Should software manufacturers should be tolerant of the practice of software piracy in third-world countries to allow these countries an opportunity to move more quickly into the information age? Why or why not?
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
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage