The following
#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:
- a. Outputs in order all names in the list.
- b. Inserts the name “Joshua” in the list after “James” then outputs the modified list.
- c. Deletes the node with “Joules” then outputs the modified list,
- d. Deletes all nodes in the list.
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:
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;
}
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?
Chapter 13 Solutions
Problem Solving with C++ (10th Edition)
Additional Engineering Textbook Solutions
Web Development and Design Foundations with HTML5 (8th Edition)
Mechanics of Materials (10th Edition)
INTERNATIONAL EDITION---Engineering Mechanics: Statics, 14th edition (SI unit)
Introduction To Programming Using Visual Basic (11th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Modern Database Management
- Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsinx Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.arrow_forwardQuestion: Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsinx Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.arrow_forward23:12 Chegg content://org.teleg + 5G 5G 80% New question A feed of 60 mol% methanol in water at 1 atm is to be separated by dislation into a liquid distilate containing 98 mol% methanol and a bottom containing 96 mol% water. Enthalpy and equilibrium data for the mixture at 1 atm are given in Table Q2 below. Ask an expert (a) Devise a procedure, using the enthalpy-concentration diagram, to determine the minimum number of equilibrium trays for the condition of total reflux and the required separation. Show individual equilibrium trays using the the lines. Comment on why the value is Independent of the food condition. Recent My stuff Mol% MeOH, Saturated vapour Table Q2 Methanol-water vapour liquid equilibrium and enthalpy data for 1 atm Enthalpy above C˚C Equilibrium dala Mol% MeOH in Saturated liquid TC kJ mol T. "Chk kot) Liquid T, "C 0.0 100.0 48.195 100.0 7.536 0.0 0.0 100.0 5.0 90.9 47,730 928 7,141 2.0 13.4 96.4 Perks 10.0 97.7 47,311 87.7 8,862 4.0 23.0 93.5 16.0 96.2 46,892 84.4…arrow_forward
- You are working with a database table that contains customer data. The table includes columns about customer location such as city, state, and country. You want to retrieve the first 3 letters of each country name. You decide to use the SUBSTR function to retrieve the first 3 letters of each country name, and use the AS command to store the result in a new column called new_country. You write the SQL query below. Add a statement to your SQL query that will retrieve the first 3 letters of each country name and store the result in a new column as new_country.arrow_forwardWe are considering the RSA encryption scheme. The involved numbers are small, so the communication is insecure. Alice's public key (n,public_key) is (247,7). A code breaker manages to factories 247 = 13 x 19 Determine Alice's secret key. To solve the problem, you need not use the extended Euclid algorithm, but you may assume that her private key is one of the following numbers 31,35,55,59,77,89.arrow_forwardConsider the following Turing Machine (TM). Does the TM halt if it begins on the empty tape? If it halts, after how many steps? Does the TM halt if it begins on a tape that contains a single letter A followed by blanks? Justify your answer.arrow_forward
- Pllleasassseee ssiiirrrr soolveee thissssss questionnnnnnnarrow_forward4. def modify_data(x, my_list): X = X + 1 my_list.append(x) print(f"Inside the function: x = {x}, my_list = {my_list}") num = 5 numbers = [1, 2, 3] modify_data(num, numbers) print(f"Outside the function: num = {num}, my_list = {numbers}") Classe Classe that lin Thus, A pro is ref inter Ever dict The The output: Inside the function:? Outside the function:?arrow_forwardpython Tasks 5 • Task 1: Building a Library Management system. Write a Book class and a function to filter books by publication year. • Task 2: Create a Person class with name and age attributes, and calculate the average age of a list of people Task 3: Building a Movie Collection system. Each movie has a title, a genre, and a rating. Write a function to filter movies based on a minimum rating. ⚫ Task 4: Find Young Animals. Create an Animal class with name, species, and age attributes, and track the animals' ages to know which ones are still young. • Task 5(homework): In a store's inventory system, you want to apply discounts to products and filter those with prices above a specified amount. 27/04/1446arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageNew Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT