Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 13, Problem 1P

The following program creates a linked list with three names:

  #include <iostream>

  #include <string>

  using namespace std;

  struct Node

  {

  string name;

  Node *link;

  };

  typedef Node* NodePtr;

  int main()

  {

  NodePtr listPtr, tempPtr;

  listPtr = new Node;

  listPtr–>name = “Emily”;

  tempPtr = new Node;

  tempPtr–>name = “James”;

  listPtr–>link = tempPtr;

  tempPtr–>link = new Node;

  tempPtr = tempPtr–>link;

  tempPtr–>name = “Joules”;

  tempPtr–>link = NULL;

  return 0;

  }

Add code to the main function that:

  1. a. Outputs in order all names in the list.
  2. b. Inserts the name “Joshua” in the list after “James” then outputs the modified list.
  3. c. Deletes the node with “Joules” then outputs the modified list,
  4. d. Deletes all nodes in the list.
Expert Solution & Answer
Check Mark
Program Plan Intro

Creation of program to produce a linked list and perform operations

Program Plan:

  • Define a structure “Node” with member variable and link to another node.
  • Define a method “displayList()” to display nodes present in the list.
    • Perform a loop operation until list reaches null.
    • Display name of node and move to next node.
    • Continue this process until list reaches null.
  • Define a method “insertNameAfter()” to insert a name after a particular name.
    • Perform a loop operation until list reaches null.
    • Compare each word with given name.
    • If required name is reached, insert new name after the given name.
    • Update the next node pointer.
  • Define a method “deleteNameList()” to delete a particular name in list.
    • Perform a loop operation until list reaches null.
    • Compare each word with given name.
    • If required name is reached, delete name.
    • Update the next node pointer.
  • Define a method “deleteAllNodes()” to delete all nodes in list.
    • Declare variables that are required for program.
    • Perform a loop operation until list reaches null.
    • Assign “head” to a temporary variable.
    • Move to next node.
    • Delete the “head” of linked list.
  • Define a main method to perform operations on list.
    • Declare variables that are required for program.
    • Define nodes and assign values.
    • Call method “displayList()” to display nodes in list.
    • Call method “insertNameAfter()” to insert a name after a particular name.
    • Call method “deleteNameList()” to delete a particular name in list.
    • Call method “deleteAllNodes()” to delete all nodes in list.
Program Description Answer

Program Description:

The following C++ program describes about creation of program to create a linked list and perform operations on list.

Explanation of Solution

Program:

//Include libraries

#include <iostream>

#include <string>

//Use namespace

using namespace std;

//Define a structure

struct Node

{

//Declare member variable

string name;

//Declare link

Node *link;

};

//Define instance

typedef Node* NodePtr;

//Define a method displayList()

void displayList(NodePtr head)

{

//Loop until empty

while(head!=NULL)

{

//Display value

cout<<head->name<<" ";

//Move to next node

head=head->link;

}

//New line

cout<<endl;

}

//Define method insertNameAfter()

void insertNameAfter(NodePtr head,string sName, string newName)

{

//Declare variable

NodePtr temp;

//Create instance of node

temp=new Node;

//Assign value

temp->name=newName;

//Assign null value

temp->link=NULL;

//Loop until it reaches null

while(head!=NULL)

{

//If condition satisfies

if(head->name.compare(sName)==0)

{

//Assign value

temp->link=head->link;

//Assign value

head->link=temp;

//Break

break;

}

//Assign value

head=head->link;

}

}

//Define method deleteNameList()

void deleteNameList(NodePtr head,string sName)

{

//Declare variable

NodePtr prev=NULL;

//Loop until it reaches null

while(head!=NULL)

{

//If condition satisfies

if(head->name.compare(sName)==0)

{

//Assign value

prev->link=head->link;

//Delete

delete head;        

//Break

break;

}

//Assign value

prev=head;

//Assign value

head=head->link;

}

}

//Define method deleteAllNodes()

void deleteAllNodes(NodePtr head)

{

//Declare variable

NodePtr temp;

//Loop

while(head)

{

//Assign value

temp=head;

//Move to next value

head=head->link;

//Delete node

delete temp;   

}

}

//Define main method

int main()

{

//Declare variables

NodePtr listPtr,tempPtr;

//Create new instance

listPtr =new Node;

//Assign value

listPtr->name="Emily";

//Create new node

tempPtr=new Node;

//Assign value

tempPtr->name="James";

//Assign value

listPtr->link=tempPtr;

//Create new node

tempPtr->link=new Node;

//Move to next value

tempPtr=tempPtr->link;

//Assign value

tempPtr->name="Joules";

//Assign null value

tempPtr->link=NULL;

//Display message

cout<<"All Names in the list are: "<<endl;

//Call method displayList()

displayList(listPtr);

//Call method insertNameAfter()

insertNameAfter(listPtr,"James","Joshua");

//Display message

cout<<"Output modified list after Insert Joshua are:"<<endl;

//Call method displayList()

displayList(listPtr);

//Call method deleteNameList()

deleteNameList(listPtr,"Joules");

//Display message

cout<<"Output modified list after deleting Joshua are:"<<endl;

//Call method displayList()

displayList(listPtr);

//Call method deleteAllNodes()

deleteAllNodes(listPtr);

//Pause console window

system("pause");

//Return

return 0;

}

Sample Output

All Names in the list are:

Emily James Joules

Output modified list after Insert Joshua are:

Emily James Joshua Joules

Output modified list after deleting Joshua are:

Emily James Joshua

Press any key to continue . . .

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
#ifndef LLCP_INT_H#define LLCP_INT_H #include <iostream> struct Node{   int data;   Node *link;};void DelOddCopEven(Node*& headPtr);int    FindListLength(Node* headPtr);bool   IsSortedUp(Node* headPtr);void   InsertAsHead(Node*& headPtr, int value);void   InsertAsTail(Node*& headPtr, int value);void   InsertSortedUp(Node*& headPtr, int value);bool   DelFirstTargetNode(Node*& headPtr, int target);bool   DelNodeBefore1stMatch(Node*& headPtr, int target);void   ShowAll(std::ostream& outs, Node* headPtr);void   FindMinMax(Node* headPtr, int& minValue, int& maxValue);double FindAverage(Node* headPtr);void   ListClear(Node*& headPtr, int noMsg = 0); // prototype of DelOddCopEven of Assignment 5 Part 1 #endif
#include using namespace std; struct ListNode { string data; ListNode *next; }; int main() { ListNode *p, *list; list = new ListNode; list->data = "New York"; p new ListNode; p->data = "Boston"; list->next = p; p->next = new ListNode; p->next->data = "Houston"; p->next->next = nullptr; // new code goes here Which of the following code correctly deletes the node with value "Boston" from the list when added at point of insertion indicated above? O list->next = p; delete p; O p = list->next; %3D list->next = p->next; delete p; p = list->next; list = p->next; delete p; O None of these O p = list->next; %3D list->next = p; %3D delete p;
struct node{ int a; struct node * nextptr; };   Write two functions. One for inserting new values to a link list that uses the given node structure. void insert(struct node **head, int value); Second function is called to count the number of even numbers in the link list. It returns an integer that represents the number of even numbers. int countEvenNumbers(struct node *head); Write a C program that reads a number of integers from the user and insert those integers into a link list (use insert function). Later pass the head pointer of this link list to a function called countEvenNumbers. This function counts and returns the number of even numbers in the list. The returned value will be printed on the screen. Note 1: Do not modify the function prototypes.   Sample Input1:          Sample Output1: 45 23 44 12 37 98 33 35 -1 3   Sample Input2:          Sample Output2: 11 33 44 21 22 99 123 122 124 77 -1 4

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Why were computer programming languages invented?

Starting Out With Visual Basic (8th Edition)

True or False: When you write a constructor for a class, it still has the default constructor that Java automat...

Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)

The statement or block that is repeated is known as the_______ of the loop.

Starting Out with C++: Early Objects (9th Edition)

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
Introduction to Linked List; Author: Neso Academy;https://www.youtube.com/watch?v=R9PTBwOzceo;License: Standard YouTube License, CC-BY
Linked list | Single, Double & Circular | Data Structures | Lec-23 | Bhanu Priya; Author: Education 4u;https://www.youtube.com/watch?v=IiL_wwFIuaA;License: Standard Youtube License