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
- I have a few questions I need help with Statement: When we build a nearest neighbor model, we shall not remove the redundant dummies when coding a categorical variable. True or False Statement: One reason why a neural network model often requires a significant number of data observations to train is that it often has a significant number of model parameters to estimate even if there are only a few predictors. True or False. Which of the following statements about confusion matrix is wrong A) Confusion matrix is a performance measure for probability prediction techniques B) Confusion matrix is derived based on classification rules with cut-off value 0.5 C) Confusion matrix is derived based on training partition to measure a model’s predictive performance D) None of the abovearrow_forwardStudent ID is 24241357arrow_forwardWhich of the following methods help when a model suffers from high variance? a. Increase training data. b. Increase model size. c. Decrease the amount of regularization. d. Perform feature selection.arrow_forward
- 57 Formula 1 point Use shift folding, length 3, on the following value to calculate the Hash Value. 114184121 Type your answer...arrow_forwardWrite a program that reads a list of 10 integers, and outputs those integers in reverse. For coding simplicity, follow each output integer by a space, including the last one. Then, output a newline. Ex: If the input is: 2 4 6 8 10 12 14 16 18 20 the output is: 20 18 16 14 12 10 8 642 To achieve the above result, first read the integers into an array. Then output the array in reverse. 623802 1031906 nx3zmv7.arrow_forward6.3B-2. Multiple Access protocols (2). Consider the figure below, which shows the arrival of 6 messages for transmission at different multiple access nodes at times t=0.1, 0.8, 1.35, 2.6, 3.9, 4.2. Each transmission requires exactly one time unit. 1 2 3 4 t=0.0 t=1.0 t=2.0 t=3.0 5 6 t=4.0 t=5.0 For the slotted ALOHA protocol, indicate which packets are successfully transmitted. You can assume that if a packet experiences a collision, a node will not attempt a retransmission of that packet until sometime after t=5. 1 2 3 4 5 Karrow_forward
- Problem of checking Compile errors Runtime errors ======== } ng; } You have the following IQueue interface. Implement a Queue class derived from IQueue. You can use STL containers discussed in class, such as vector, queue, stack, deque, map. #include using namespace std; class IQueue { public: }; virtual void Enqueue(int val) virtual int Dequeue() = 0; virtual int Size() const = 0; int main() = 0; { Queue q; ===== } cout << q.Size() << endl; q. Enqueue(10); q.Enqueue(20); q. Enqueue(30); cout << q.Size() << endl; cout << q.Dequeue() << endl; cout << q.Size() << endl; cout << q.Dequeue() << endl; cout << q.Size() << endl; cout << q.Dequeue() << endl; cout << q.Size() << endl; ==== ====arrow_forwardlogicarrow_forwardQ1: For the Figure Below if the input to the first tank is step with magnitude 2 find 1. What type of relation between (tanks 1 and 2) and tank 3 2. Initial real value of H2, if the steady state value is 10 3. Final Value of H3 4. H1 at t=1.5 5. For the system tank1 and tank 2 only which case is applied to them (overdamping, underdamped or critically damping) A₁=1 A₂=1 Tank 1 R₁ = 2 * Tank 2 R₁₂=2 A3=0.5 hy R₁=4 Tank 3arrow_forward
- Please original work Talk about the most common challenges encountered in a data warehouse What are some creative ways to overcome those challenges What is for one real world example where your method would be effective Please cite in text references and add weblinksarrow_forwardWhat is the differences between mobile website navigation and traditional website navigation?arrow_forwardPoint 10:26 Explain P 10:26 10:25arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning