In this program, I need to implement some linked listoperations : i need to crea
ID: 3610329 • Letter: I
Question
In this program, I need to implement some linked listoperations :
i need to create a singly linked list where each node containsfollowing information:
1. ID as int . ( ID is : any9 digits)
2. Name as string or char[ ]
When the program is run, the following screen will be output andwaiting for the user input.
1. List all the values in thelist
2. Add a new value at the end ofthe list
0. Exit the program
The user can display all the values or add a new value at theend of the list by choosing corresponding option. The ID and Namewill be displayed if the user chooses “1”. When a useradds a new value, he needs to input the “Name”. ID willbe assigned automatically by the last value’s ID +1. When anew item is added, the old last item’s next value will pointto the new item.
1. List all the values in thelist
2. Add a new value at the end ofthe list
0. Exit the program
Explanation / Answer
//Hope this will helpyou.. //Don't forget to rateit. #include<iostream>using namespace std;
struct list{
int id;
char name[100];
struct list *next;
}*first=NULL,*last,*ptr;
int main(){
char ch;
do{
cout<<"1.List all values 2.Insert 3.exit ";
cin>>ch;
switch(ch){
case '1':
ptr =first;
while(ptr != NULL){
cout<<"Id = "<<ptr->id<<endl<<"Name ="<<ptr->name<<endl<<endl;
ptr =ptr->next;
}
break;
case '2':
if(first == NULL){
first = new struct list;
cin>>first->id;
cout<<"Enter name:";do{
cin.getline(first->name,100);
}while(first->name[0] == '');
last =first;
first->next = NULL;
}else
{
ptr = new struct list;
cin>>ptr->id;
cout<<"Enter name:";
do{
cin.getline(ptr->name,100);
}while(ptr->name[0] == '');
last ->next = ptr;
last = ptr;
ptr->next = NULL;
}
break;
}
}while(ch != '3');
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.