Note: If you have already answered this question, Don\'t asnwer it again. I\'m u
ID: 3600076 • Letter: N
Question
Note: If you have already answered this question, Don't asnwer it again. I'm using Microsoft visual studio.
Note: Please check your code before posting, I've had to post this question multiple times and all the replies have had errors
Write a C++ program to manage a list of students waiting to register for a course using a linked list. Operations should include adding a new student at the end of the list, adding a new student at the beginning of the list, removing a student from the beginning of the list, removing a student from the end of the list, and removing a student by name. Allow the user an option to exit.
Be sure to use good programming methodology and keep your project modular.
Function main() should be small
Functions/Methods created and called:
The program should be able to
add student at the end of the list
add student at the beginning of the list
remove student from the end of the list
remove student from the beginning of the list
remove student by name
display the list of students
exit
Explanation / Answer
#include <iostream>
#include <cstring>
using namespace std;
struct node
{
node *next;
char name[100];
};
void addAtEnd(node **head,char *a)
{
node *temp,*r;
if(*head==NULL)
{
temp=new node;
strcpy(temp->name,a);
temp->next=NULL;
*head=temp;
}
else
{
temp=*head;
while(temp->next!=NULL)
{
temp=temp->next;
}
r=new node;
strcpy(r->name,a);
r->next=NULL;
temp->next=r;
}
}
void display(node **head)
{
node *temp;
temp=*head;
while(temp!=NULL)
{
cout<<temp->name<<endl;
temp=temp->next;
}
}
void addAtBegining(node **head,char *a)
{
node *temp;
temp=new node;
strcpy(temp->name,a);
temp->next=*head;
*head=temp;
}
void deleteAtEnd(node **head)
{
node *temp1,*temp2;
temp1=*head;
temp2=temp1->next;
if(*head==NULL)
{
cout<<"The list is empty"<<endl;
}
else if(temp2==NULL)
{
*head=NULL;
delete temp1;
}
else
{
while(temp2->next!=NULL)
{
temp1=temp1->next;
temp2=temp2->next;
}
temp1->next=NULL;
delete temp2;
}
}
void deleteAtBegining(node **head)
{
node *temp;
temp=*head;
if(temp==NULL)
{
cout<<"The list is already empty"<<endl;
}
else
{
*head=temp->next;
delete temp;
}
}
void deleteByName(node **head,char *a)
{
node *temp1,*temp2;
temp1=*head;
if (temp1==NULL)
{
cout<<"The list is already empty."<<endl;
}
else if(strcmp(temp1->name,a)==0)
{
deleteAtBegining(head);
}
else
{
int f=0;
while(1)
{
if(temp==NULL)
{
f=1;
break;
}
else if(strcmp(temp->name,a)==0)
{
f=2;
break;
}
temp2=temp1;
temp1=temp->next;
}
if(f==2)
{
temp2->next=temp1->next;
delete temp1;
}
else
{
cout<<"The mentioned name is not found in the list."<<endl;
}
}
}
int main()
{
node *head;
head=NULL;
char a[100];
for(int i=0;i<5;i++)
{
cout<<"Enter the name : ";
cin>>a;
addAtEnd(&head,a);
}
cout<<"Enter the name : ";
cin>>a;
addAtBegining(&head,a);
display(&head);
deleteAtEnd(&head);
cout<<"After deleting:"<<endl<<endl;
display(&head);
deleteAtBegining(&head);
cout<<"After deleting:"<<endl<<endl;
display(&head);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.