Write a menu based program in C to perform the various opeartions over a single linked list .
-
Append/create a list
-
Display a list
-
Calculate the length of the lits
-
Insert a node at begin
-
Insert a node before a node
-
Insert a node after a node
-
Delete a node from the beginning
-
Delete a node from the end
-
Delete a node from a specified location
-
Exit
using namespace std;
struct student
{
int roll,sub_1,sub_2;
student* next;
};
typedef struct student node;
node* create_ll(int);
void options();
void list_node(node*);
node* insert_node(node*);
void fill_node(node*,int,int,int,node*);
void search_node(node*);
void count_node(node*);
node* delete_node(node*);
int main(){
node* header;
header=create_ll(30);
cout<<"Linked ist of 30 nodes containing roll number,marks in 2 subjects is created\n";
options();
char choice;
do{
cout<<"Enter any valid choice (or o to view options): ";
fflush(stdin);
cin>>choice;
switch(choice){
case 'l':list_node(header);break;
case 'i':header=insert_node(header);break;
case 's':search_node(header);break;
case 'c':count_node(header);break;
case 'o':options();break;
case 'd':header=delete_node(header);break;
}
}while(choice!='e');
return 0;
}
void fill_node(node* ptr,int r,int s1,int s2,node* next){
ptr->roll = r;
ptr->sub_1 = s1;
ptr->sub_2 = s2;
ptr->next = next;
}
node* create_ll(int n){
node* head;
node* ptr;
for(int i=0;i<n;i++){
if(i==0){
head=new node;
ptr = head;
fill_node(ptr,i+1,rand()%70+20,rand()%70+20,new node);
}
else{
ptr = ptr->next;
fill_node(ptr,i+1,rand()%70+20,rand()%70+20,new node);
}
}
delete(ptr->next);
ptr->next=NULL;
return head;
}
void options(){
cout<<"Available options: \n";
cout<<"list information of all node - l\n";
cout<<"Search for a node based on roll no. - s\n";
cout<<"Delete a record - d\n";
cout<<"Insert a new record - i\n";
cout<<"Count the no. of nodes - c\n";
cout<<"exit - e\n";
}
void list_node(node* head){
node* ptr=head;
cout<<"roll\ts1\ts2\n";
while(ptr!=NULL){
cout<<ptr->roll<<"\t"<<ptr->sub_1<<"\t"<<ptr->sub_2<<"\n";
ptr=ptr->next;
}
}
node* insert_node(node* head){
cout<<"where do you wish to insert new node?\n";
cout<<"beginning -b\nend -e\n after certain roll no. -r\n";
char c;
int r,s1,s2,nex;
cin>>c;
if(c=='r'){
cout<<"enter the roll no. after which new node is to be inserted: ";
cin>>nex;
}
cout<<"Enter roll no.,marks in two subjects(like 10 83 87) of new node:\n";
cin>>r>>s1>>s2;
if(c=='b'){
node* newhead=new node;
fill_node(newhead,r,s1,s2,head);
return newhead;
}
else if(c=='e'){
node* ptr=head;
while(ptr!=NULL){
ptr=ptr->next;
}
ptr=new node;
fill_node(ptr,r,s1,s2,NULL);
}
else{
node* ptr=head;
while(ptr!=NULL && ptr->roll!=nex){
ptr=ptr->next;
}
if(ptr==NULL)cout<<"NO record found with roll no. "<<nex<<"\n";
else{
node* ptrsnext=ptr->next;
ptr->next =new node;
fill_node(ptr->next,r,s1,s2,ptrsnext);
}
}
return head;
}
void search_node(node *head){
node*ptr = head;
cout<<"Enter the roll no. of student: ";
int r;
cin>>r;
while(ptr!=NULL && ptr->roll!=r){
ptr=ptr->next;
}
if(ptr==NULL)cout<<"NO record found with roll no. "<<r<<"\n";
else{
cout<<"record found\n";
cout<<"marks in subjects are "<<ptr->sub_1<<" and "<<ptr->sub_2<<"\n";
}
}
void count_node(node* head){
int i=0;
node* ptr=head;
while(ptr!=NULL){
i++;
ptr=ptr->next;
}
cout<<"No. of nodes = "<<i<<"\n";
}
node* delete_node(node * head){
cout<<"which node do you wish to delete?\n";
cout<<"first node -f\nlast node -l\nof a particular roll no. -r\n";
char c;
cin>>c;
if (c=='f')
{
node *ptr=head;
head = head->next;
delete(ptr);
}
else if(c=='l')
{
node* ptr=head;
node *ptr2=ptr->next;
while(ptr2->next!=NULL){
ptr=ptr->next;
ptr2=ptr2->next;
}
ptr->next=NULL;
delete ptr2;
}
else if(c=='r')
{
int r;
cout<<"enter the roll number of node: ";
cin>>r;
node* ptr=head;
node *ptr2=ptr->next;
while(ptr2!=NULL&& ptr2->roll!=r){
ptr=ptr->next;
ptr2=ptr2->next;
}
if(ptr2==NULL){
cout<<"no record has"<<r<<" roll number\n";
}
ptr->next=ptr2->next;
delete ptr2;
}
cout <<"deleted successfully\n";
return head;
}