Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
9th Edition
ISBN: 9780134498379
Author: Tony Gaddis
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 18.2, Problem 18.6CP

Explanation of Solution

Appending a node:

It is the process of adding a new node at the end of a linked 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.

Inserting a node:

It is the process of adding a new node at a specific location in a 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
what's the error or missing?
Python: numpy def serial_numbers(num_players):"""QUESTION 2- You are going to assign each player a serial number in the game.- In order to make the players feel that the game is very popular with a large player base,you don't want the serial numbers to be consecutive. - Instead, the serial numbers of the players must be equally spaced, starting from 1 and going all the way up to 100 (inclusive).- Given the number of players in the system, return a 1D numpy array of the serial numbers for the players.- THIS MUST BE DONE IN ONE LINEArgs:num_players (int)Returns:np.array>> serial_numbers(10)array([1. 12. 23. 34. 45. 56. 67. 78. 89. 100.])>> serial_numbers(12)array([1. 10. 19. 28. 37. 46. 55. 64. 73. 82. 91. 100.])""" # print(serial_numbers(10)) # print(serial_numbers(12))
Please help, what's wrong with this code
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage