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
Expert Solution & Answer
Book Icon
Chapter 17.2, Problem 17.6CP

Explanation of Solution

Appending a node:

It is the process of adding a new node at the end of a linked list.

Inserting a node:

It is the process of adding a new node at a specific location in a list.

Member function for appending a node:

The following member function is used to append the received value into a list at end:

//Definition of appendNode() function

void IntList::appendNode(int num)

{

//Declare a structure pointer variables

ListNode *newNode, *dataPtr = nullptr;

//Assign a new node

newNode = new ListNode;

//Store the argument value into "newNode"

newNode->value = num;

//Assign the next pointer to be "NULL"

newNode->next = nullptr;

//Condition to check whether the list is empty or not

if (!head)

//If true, make newNode as the first node

head = newNode;

//else part

else

{

//Store head value into dataPtr

dataPtr = head;

//Loop to find the last node in the list

while (dataPtr->next)

//Assign last node to the structure variable

dataPtr = dataPtr->next;

//Insert newNode as the last node

dataPtr->next = newNode;

}

}

Explanation:

Consider the function named “appendNode()” to insert the received value “num” at end of the list.

  • Make a new node and assign a received value “num” into the node.
  • Using “if…else” condition check whether the list is empty or not.
    • If the “head” node is empty, assign a new node into “head” pointer.
    • Otherwise, make a loop to find a last node in the list and insert the value at end of the list.

Member function for inserting a node:

The following member function is used to insert the received value into a list at specific location:

//Definition of insertNode() function

void IntList::insertNode(int num)

{

/*Declare a structure pointer variables*/

ListNode *newNode, *dataPtr, *prev = nullptr;

//Assign a new node

newNode = new ListNode;

//Store the argument value into "newNode"

newNode->value ...

Blurred answer
Students have asked these similar questions
dont want code, please write done the step
What is the best way to code?
How do you do this? Please comment the explanation for each line of code.
Knowledge Booster
Background pattern image
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