Note: If you have already answered this question, Don\'t asnwer it again Write a
ID: 3599105 • 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:
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
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
string name;
struct node *next;
}*start;
/*
* Class Declaration
*/
class single_llist
{
public:
node* create_node(string);
void insert_begin();
void insert_last();
void delete_pos();
void display();
single_llist()
{
start = NULL;
}
};
main()
{
int choice, nodes, element, position, i;
single_llist sl;
start = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Operations on singly linked list"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert student at beginning"<<endl;
cout<<"2.Insert sudent at last"<<endl;
cout<<"3.Display Linked List"<<endl;
cout<<"4.Search Element"<<endl;
cout<<"5.Delete a student by name"<<endl;
cout<<"6.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
sl.insert_begin();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at Last: "<<endl;
sl.insert_last();
cout<<endl;
break;
case 3:
cout<<"Display elements of link list"<<endl;
sl.display();
cout<<endl;
break;
/* case 4:
cout<<"Search Search a student by name: "<<endl;
sl.search();
cout<<endl;
break;
case 5:
cout<<"Delete a particular node: "<<endl;
sl.delete_pos();
break;
case 6:
cout<<"Exiting..."<<endl;
exit(1);
break; */
default:
cout<<"Wrong choice"<<endl;
}
}
}
node *single_llist::create_node(string value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->name = value;
temp->next = NULL;
return temp;
}
}
void single_llist::insert_begin()
{
string name;
cout<<"Enter the student name: ";
cin>>name;
struct node *temp, *p;
temp = create_node(name);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
void single_llist::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"*******Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->name<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
void single_llist::insert_last()
{
string name;
cout<<"Enter student name to add : ";
cin>>name;
struct node *temp, *s;
temp = create_node(name);
s = start;
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at last"<<endl;
}
void single_llist::delete_pos()
{
int pos, i, counter = 0;
string name;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}else
{
cout<<"Enter the name to be deleted: ";
cin>>name;
struct node *s, *ptr;
s = start;
while(s != 0)
{
if (start->name == name)
{
break;
}
else
{
while (s != NULL)
{
s = s->next;
}
if (s == 0)
{
cout<<"student not found : ";
}else
{
if(s == start)
s=s->next;
free(s);}
}//while
}//parent if
}
}//method
--------------------------------------------------------------------------------------------
output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.