#include<stdio.h>
#include<process.h>
struct list
{
int info;
struct list *link;
};
struct list start,*temp,*prev,*node;
void insert()
{
int i,j,item;
printf("Enter the item to insert=");
scanf("%d",&item);
printf("\n 1.insertion at begining");
printf("\n 2.insertion at end");
printf("\n 3.insertion at middle");
printf("Enter your choice=");
scanf("%d",&i);
switch(i)
{
case 1:if(start.link==NULL)
{
node=(struct list *)malloc(sizeof(struct list));
node->info=item;
node->link=NULL;
start.link=node;
return;
}
else
{
node=(struct list *)malloc(sizeof(struct list));
node->info=item;
node->link=start.link;
start.link=node;
return;
}
case 2:if(start.link==NULL)
{
node=(struct list *)malloc(sizeof(struct list));
node->info=item;
node->link=NULL;
start.link=node;
return;
}
else
{
temp=start.link;
while(temp->link!=NULL)
{
temp=temp->link;
}
node=(struct list *)malloc(sizeof(struct list));
temp->link=node;
return;
}
case 3:printf("Enter position=");
scanf("%d",&j);
if(start.link==NULL)
{
node=(struct list *)malloc(sizeof(struct list));
node->info=item;
node->link=NULL;
start.link=node;
return;
}
else
{
temp=start.link;
while(temp->link!=NULL)
{
if(temp->info==j)
{
break;
}
temp=temp->link;
}
node=(struct list*)malloc(sizeof(struct list));
temp->link=node;
return;
}
default:printf("Wrong choice");
}
}
int del()
{
int i,j,item;
printf("/n1.Deletion at beginning");
printf("/n2.Deletion at end");
printf("/n3.Deletion at middle");
printf("Enter the choice");
scanf("%d",&i);
switch(i)
{
case 1:if(start.link==NULL)
{
printf("list empty");
return 0;
}
else
{
temp=start.link;
item=temp->info;
start.link=temp->link;
free(temp);
return item;
}
case 2:if(start.link==NULL)
{
printf("list empty");
return 0;
}
else
{
temp=start.link;
prev=&start;
while(temp->link!=NULL)
{
temp=temp->link;
prev=prev->link;
}
item=temp->info;
prev->link=NULL;
free(temp);
return item;
}
case 3:if(start.link==NULL)
{
printf("list empty");
return 0;
}
else
{
printf("enter the item to be deleted");
scanf("%d",&i);
temp=start.link;
prev=&start;
while(temp->link!=NULL)
{
if(temp->info==i)
{
break;
}
prev=prev->link;
temp=temp->link;
}
if(temp->link==NULL)
{
printf("item not available");
return 0;
}
else
{
item=temp->info;
prev->link=temp->link;
free(temp);
return item;
}
}
default:printf("wrong choice");
}
}
void display()
{
temp=start.link;
while(temp)
{
printf("%d",temp->info);
temp=temp->link;
}
}
void main()
{
void insert();
int del();
void display();
int i,j,k;
char ch;
clrscr();
start.link=NULL;
do
{
clrscr();
printf("\n1.Insert");
printf("\n2.Delete");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your Choice :");
scanf("%d",&i);
switch(i)
{
case 1:insert();
display();
break;
case 2:j=del();
printf("item deleted is %d\n",j);
display();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("wrong choice");
}
fflush(stdin);
printf("any more?=");
scanf("%c",&ch);
}while(ch=='y');
getch();
}