Note: If you have already answered this question, Don\'t asnwer it again Write a
ID: 3599683 • Letter: N
Question
Note: If you have already answered this question, Don't asnwer it again
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.
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
/*
* C++ Program to Implement Singly Linked List
*/
#include <iostream>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int studentId;
string name;
struct node *next;
}*start;
/*
* Class Declaration
*/
class single_llist
{
public:
node* create_node(int,string);
void insert_begin(int studentId,string name);
void insert_last(int studentId,string name);
void delete_begin();
void delete_last();
void delete_pos(int pos);
void delete_by_name(string name);
void display();
single_llist()
{
start = NULL;
}
};
/*
* Main :contains menu
*/
main()
{
int choice, nodes, element, position, i;
single_llist sl;
start = NULL;
int studentId;
// int choice;
string name; // asume name has a string
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Operations on singly linked list"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.add student at the end of the list"<<endl;
cout<<"2.add student at the beginning of the list"<<endl;
cout<<"3.remove student from the end of the list"<<endl;
cout<<"4.remove student from the beginning of the list"<<endl;
cout<<"5.remove student by name"<<endl;
cout<<"6.display the list of students"<<endl;
cout<<"7.Exit "<<endl;
while (1)
{
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"add student at the beginning of the list: "<<endl;
cin>>studentId>>name;
sl.insert_begin(studentId,name);
cout<<endl;
break;
case 2:
cout<<"add student at the end of the list: "<<endl;
cin>>studentId>>name;
sl.insert_last(studentId,name);
cout<<endl;
break;
case 3:
cout<<"remove student from the end of the list:"<<endl;
sl.delete_last();
cout<<endl;
break;
case 4:
cout<<"remove student from the beginning of the list: "<<endl;
sl.delete_begin();
cout<<endl;
break;
case 5:
cout<<"remove student by name: "<<endl;
cin>>name;
sl.delete_by_name(name);
break;
case 6:
cout<<"display the list of students"<<endl;
sl.display();
cout<<endl;
break;
case 7:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}
/*
* Creating Node
*/
node *single_llist::create_node(int studentId,string name)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->studentId = studentId;
temp->name=name;
temp->next = NULL;
return temp;
}
}
/*
* Inserting student in beginning
*/
void single_llist::insert_begin(int studentId,string name)
{
struct node *temp, *p;
temp = create_node(studentId,name);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Student Inserted at beginning of list"<<endl;
}
/*
* Inserting student at last
*/
void single_llist::insert_last(int studentId,string name)
{
struct node *temp, *s;
temp = create_node(studentId,name);
s = start;
if(s==NULL)
{
start=temp;
start->next=NULL;
return;
}
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Student Inserted at last of list"<<endl;
}
/*
* Delete begin
*/
void single_llist::delete_begin()
{
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
if(start!=NULL)
{
struct node *temp=start->next,*s=start;
free(s);
start=temp;
}
}
/*
* Delete end
*/
void single_llist::delete_last()
{
//first check if firstNode is NULL or last node.
if(start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
else if(start->next == NULL){
start=NULL;
free(start);
}
else{
struct node *one = start;
struct node *two = start->next;
while(two->next != NULL){
two = two->next;
one = one->next;
}
one->next = NULL;
free(two);
}
}
/*
* Delete Student by name here find pos then delete student because name may be same for many student
*/
void single_llist::delete_by_name(string name)
{
struct node *s=start;
int i=1;
while(s!=NULL)
{
if(s->name==name)
{
delete_pos(i);
i=1;
s=start;
}
else
{
s=s->next;
i++;
}
}
}
/*
* Delete Student by pos
*/
void single_llist::delete_pos(int pos)
{
int i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
//cout<<"Enter the position of value to be deleted: ";
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
}
}
/*
* Display Elements of a link list
*/
void single_llist::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"student list "<<endl;
while (temp != NULL)
{
cout<<temp->studentId<<" "<<temp->name<<endl;
temp = temp->next;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.