//add_at_beg, add_at_end,add_at_specific,del_at_beg, del_at_end,del_at_specific
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct node
{
int info;
struct node *next;
}node;
node *start=NULL;
void insert_at_end(int x);
void insert_at_beg(int x);
void insert_at_specific(int ,int);
void disp();
void del_at_beg();
void del_at_end();
void del_at_specific(int po);
int seq_search(int item);
void sort();
int main()
{
int n,po,ch;
while(1)
{
printf("\n\t\t\tMenu");
printf("\n\t\t\t1 : Insertion at end");
printf("\n\t\t\t2 : Insertion at beg");
printf("\n\t\t\t3 : Insertion at specific");
printf("\n\t\t\t4 : Display");
printf("\n\t\t\t5 : Deletion at beg");
printf("\n\t\t\t6 : Deletion at end");
printf("\n\t\t\t7 : Deletion at specific");
printf("\n\t\t\t8 : Search");
printf("\n\t\t\t9 : Sorting");
printf("\n\t\t\t10 : Exit");
printf("\n\t\t\tEnter ur choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the info :");
scanf("%d",&n);
insert_at_end(n);
break;
case 2:
printf("\nEnter the info :");
scanf("%d",&n);
insert_at_beg(n);
break;
case 3:
printf("\nEnter the position :");
scanf("%d",&po);
printf("\nEnter the info :");
scanf("%d",&n);
insert_at_specific(n,po);
break;
case 4:
disp();
break;
case 5:
del_at_beg();
break;
case 6:
del_at_end();
break;
case 7:
printf("\nEnter the position :");
scanf("%d",&po);
del_at_specific(po);
break;
case 8:
printf("\nEnter the item :");
scanf("%d",&n);
po=seq_search(n);
if(po!=-1)
printf("\nFound at %d",po);
else
printf("\nNot Found.");
break;
case 9:
sort();
break;
case 10:
exit(0);
default:
printf("\nInvalid choice");
}
}
return 0;
}
void insert_at_end(int x)
{
node *temp,*new_node;
new_node=(node*)malloc(sizeof(node));
new_node->info=x;
new_node->next=NULL;
if(start==NULL)
start=new_node;
else
{
temp=start;
while(temp->next!=NULL)
temp=temp->next;
temp->next=new_node;
}
}
void insert_at_beg(int x)
{
node *new_node;
new_node=(node*)malloc(sizeof(node));
new_node->info=x;
new_node->next=start;
start=new_node;
}
void insert_at_specific(int x,int po)
{
int i;
node *new_node,*temp;
if(po<1 || po>count()+1)
{
printf("\nInvalid position");
return;
}
if(po==1)
insert_at_beg(x);
else
{
if(po==count()+1)
insert_at_end(x);
else
{
new_node=(node*)malloc(sizeof(node));
new_node->info=x;
new_node->next=NULL;
temp=start;
for(i=1;i<po-1;i++)
temp=temp->next;
new_node->next=temp->next;
temp->next=new_node;
}
}
}
void disp()
{
node *temp;
printf("\nElements in the list are : ");
for(temp=start;temp!=NULL;temp=temp->next)
printf("%d ",temp->info);
}
int count()
{
node *temp;
int c=0;
for(temp=start;temp!=NULL;temp=temp->next)
c++;
return c;
}
void del_at_beg()
{
node *temp;
if(start==NULL)
{
printf("\nList Empty");
return;
}
temp=start;
start=start->next;
printf("\nDeleted node is %d",temp->info);
free(temp);
}
void del_at_end()
{
node *temp;
int i;
if(start==NULL)
{
printf("\nList Empty");
return;
}
if(count()==1)
{
del_at_beg();
return;
}
temp=start;
for(i=1;i<count()-1;i++)
temp=temp->next;
printf("\nDeleted node is %d",temp->next->info);
free(temp->next);
temp->next=NULL;
}
void del_at_specific(int po)
{
int i;
node *temp,*temp1;
temp=start;
for(i=1;i<po-1;i++)
temp=temp->next;
temp1=temp->next;
temp->next=temp1->next;
printf("\nDeleted node is %d",temp->info);
free(temp1);
}
int seq_search(int item)
{
int po=0;
node *temp;
temp=start;
while(temp!=NULL)
{
po++;
if(temp->info==item)
return po;
temp=temp->next;
}
return -1;
}
void sort()
{
int t;
node *i,*j;
for(i=start;i!=NULL;i=i->next)
{
for(j=i->next;j!=NULL;j=j->next)
{
if(i->info > j->info)
{
t=i->info;
i->info=j->info;
j->info=t;
}
}
}
}