Starting out With C++, Early Objects - Access
Starting out With C++, Early Objects - Access
8th Edition
ISBN: 9780133452259
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
Linked list. Complete the function that takes as a parameter the head of a linked list and prints the linked list in reverse order. If the linked list had the contents: of,the,and,on,a,an,ok. Then the correct output would be: ok,an,a,on,and,the,of. Given: struct node {    char word[31];     struct node *prev, next; };   void print_reverse(struct node *head) {     }
Java - Write a non-member method for a "enqueue" that utilizes a doubly linked list.  There are three variables available (can be sent) to the method; a “front” reference, a “rear” reference, and a data element (string) (you decide what you need).  Empty case is when front and rear are NULL.  The node is defined as follows: class node{   node after;   node before   string data }
Every time you write a non-const member function for a linked list, you should always think about if that function is preserving your class invariants. Group of answer choices A. True B. False
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