Menu Driven Program For All Operations
#include <stdio.h>
#include <iostream>
#include <limits.h>
#include <stdlib.h>
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct header
{
float avg;
int max1,min1,max2,min2,totalnode;
struct node *next;
};
void headerfunc(struct header *);
void insertf(struct header *);
void deletef(struct header *);
void bubble_sort(struct header *);
void display(struct header *);
void avg(struct header *);
void max_min(struct header *);
void max2_min2(struct header *);
void disp_odd(struct header *);
void disp_even(struct header *);
struct node * new_node(int data);
int main(void)
{
struct header *head=new header;
head->avg=0;
head->max1=0;
head->max2=0;
head->min1=0;
head->min2=0;
head->totalnode=0;
head->next=NULL;
int c=0;
while(true)
{
cout<<"\n\nPress 1 to insert in Linked list";
cout<<"\nPress 2 to delete in Linked list";
cout<<"\nPress 3 for sorting the Linked list(Bubble Sort)";
cout<<"\nPress 4 to display the Linked list";
cout<<"\nPress 5 to show the average of the Linked list";
cout<<"\nPress 6 to show the maximum and minimum element of the Linked list";
cout<<"\nPress 7 to to show the second maximum and second minimum element of the Linked list";
cout<<"\nPress 8 to show the odd node elements";
cout<<"\nPress 9 to show the even node elements";
cout<<"\nPress 0 to exit : ";
cin>>c;
cout<<"\n";
switch(c)
{
case 0:
exit(0);
case 1: insertf(head);
break;
case 2: deletef(head);
break;
case 3: bubble_sort(head);
break;
case 4: display(head);
break;
case 5: cout<<"\nAverage of Linked List is : "<<head->avg;
break;
case 6: cout<<"\nMaximum element of Linked List is : "<<head->max1;
cout<<"\nMinimum element of Linked List is : "<<head->min1;
break;
case 7: cout<<"\nSecond maximum element of Linked List is : "<<head->max2;
cout<<"\nSecond minimum element of Linked List is : "<<head->min2;
break;
case 8: disp_odd(head);
break;
case 9: disp_even(head);
break;
default:cout<<"\n****Wrong input****";
cout<<"\n Enter again\n";
}
}
return 0;
}
struct node * new_node(int data)
{
struct node *nnd=new node;
nnd->data=data;
nnd->next=NULL;
return nnd;
}
void headerfunc(struct header * head)
{
struct node * ptr=head->next;
int i=0;
while(ptr!=NULL)
{
++i;
ptr=ptr->next;
}
head->totalnode=i;
avg(head);
max_min(head);
max2_min2(head);
}
void insertf(struct header * head)
{
int ins=0;
cout<<"\nInsertion in the end.";
cout<<"\nEnter element to be inserted(-1 to exit) : ";
cin>>ins;
while(ins!=-1)
{
struct node *nnd=new_node(ins);
if(head->next==NULL)
head->next=nnd;
else
{
struct node *ptr=head->next;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=nnd;
}
cout<<"\nEnter element to be inserted(-1 to exit) : ";
cin>>ins;
}
headerfunc(head);
display(head);
}
void deletef(struct header * head)
{
struct node *ptr=head->next,*preptr=head->next;
int del=0;
cout<<"\nEnter element to be deleted : ";
cin>>del;
while(ptr->data!=del)
{
preptr=ptr;
ptr=ptr->next;
}
preptr->next=ptr->next;
delete ptr;
headerfunc(head);
display(head);
}
void bubble_sort(struct header * head)
{
struct node * ptr=head->next;
int ar[head->totalnode],i=0;
while(ptr!=NULL)
{
ar[i]=ptr->data;
++i;
ptr=ptr->next;
}
sort(ar,ar+head->totalnode);
ptr=head->next;
i=0;
while(ptr!=NULL)
{
ptr->data=ar[i];
++i;
ptr=ptr->next;
}
cout<<"\nSorted Linked List is :\n";
display(head);
}
void display(struct header * head)
{
struct node * ptr=head->next;
cout<<"\nLinked List is : ";
while(ptr!=NULL)
{
cout<<ptr->data<<" ";
ptr=ptr->next;
}
}
void avg(struct header * head)
{
int sum=0;
struct node * ptr=head->next;
while(ptr!=NULL)
{
sum=sum+ptr->data;
ptr=ptr->next;
}
head->avg=(float)sum/(head->totalnode);
}
void max_min(struct header * head)
{
struct node * ptr=head->next;
int max1=0,min1=0;
max1=INT_MIN;
min1=INT_MAX;
while(ptr!=NULL)
{
if(max1<ptr->data)
max1=ptr->data;
if(min1>ptr->data)
min1=ptr->data;
ptr=ptr->next;
}
head->max1=max1;
head->min1=min1;
}
void max2_min2(struct header * head)
{
struct node * ptr=head->next;
int min2=INT_MAX,max2=INT_MIN;
while(ptr!=NULL)
{
if(max2<ptr->data&&ptr->data!=head->max1)
max2=ptr->data;
if(min2>ptr->data&&ptr->data!=head->min1)
min2=ptr->data;
ptr=ptr->next;
}
head->max2=max2;
head->min2=min2;
}
void disp_odd(struct header * head)
{
struct node * ptr=head->next;
cout<<"\nElements at Odd position of Linked List are : ";
while(ptr!=NULL)
{
cout<<ptr->data<<" ";
if(ptr->next==NULL)
ptr=ptr->next;
else
ptr=ptr->next->next;
}
}
void disp_even(struct header * head)
{
struct node * ptr=head->next->next;
cout<<"\nElements at Even position of Linked List are : ";
while(ptr!=NULL)
{
cout<<ptr->data<<" ";
if(ptr->next==NULL)
ptr=ptr->next;
else
ptr=ptr->next->next;
}
}




Leave a comment