Program:-
// C Program for Linked List With all Operations
#include<stdio.h>
#include<stdlib.h>
struct link
{
int info;
struct link * next;
};
struct link * start=NULL;
void insertFirst();
void insertLast();
void insertPos();
void show();
void delFirst();
void delLast();
void delPos();
void main()
{
int ch,temp;
do
{
printf("\n\t Main menu");
printf("\n 1:insert at first");
printf("\n 2:insert at last");
printf("\n 3:insert at Specific Position");
printf("\n 4:Delete from First");
printf("\n 5:Delete from Last");
printf("\n 6:Delete from Specific Position");
printf("\n 7:Show");
printf("\n 8:Exit");
printf("\n Enter Your Choice ");
scanf("%d",&ch);
switch(ch)
{
case 1: insertFirst(); break;
case 2: insertLast(); break;
case 3: insertPos(); break;
case 4: delFirst(); break;
case 5: delLast(); break;
case 6: delPos(); break;
case 7: show(); break;
case 8: temp=1; break;
default: printf("\n wrong choice");
}
}while(ch!=8 && temp!=1);
}
void insertFirst()
{
struct link * temp;
int num;
temp=(struct link *)malloc(sizeof(struct link));
printf("Enter the number\n");
scanf("%d",&num);
temp->info=num;
temp->next=start;
start=temp;
}
void insertLast()
{
struct link * temp,* move;
int num;
temp=(struct link *)malloc(sizeof(struct link));
printf("Enter the number\n");
scanf("%d",&num);
temp->info=num;
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
move=start;
while(move->next!=NULL)
{
move=move->next;
}
move->next=temp;
}
}
void insertPos()
{
struct link * temp,* move,* back;
int num,pos,i;
temp=(struct link *)malloc(sizeof(struct link));
printf("Enter the Position to Insert The no.\n");
scanf("%d",&pos);
printf("Enter the number\n");
scanf("%d",&num);
temp->info=num;
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
move=start;
for(i=0;i<pos-1;i++)
{
back=move;
move=move->next;
}
temp->next=back->next;
back->next=temp;
}
}
void delFirst()
{
struct link * move;
int item;
if(start==NULL)
printf("link list is empty");
else
{
item=start->info;
start=start->next;
printf("item deleted=%d",item);
}
}
void delLast()
{
struct link * back,* move;
int item;
if(start==NULL)
printf("link list is empty");
else
{
move=start;
if(move->next==NULL)
{
item=move->info;
start=NULL;
}
else
{
while(move->next!=NULL)
{
back=move;
move=move->next;
}
item=move->info;
back->next=NULL;
}
printf("item deleted=%d",item);
}
}
void delPos()
{
struct link * move,* back;
int num,pos,i;
printf("Enter the Position From which to Delete The no.\n");
scanf("%d",&pos);
if(start==NULL)
printf("list is empty");
else
{
move=start;
for(i=0;i<pos-1;i++)
{
back=move;
move=move->next;
}
num=move->info;
back->next=move->next;
printf("item deleted=%d",num);
}
}
void show()
{
struct link * temp,* move;
if(start==NULL)
printf("list is empty");
else
{
move=start;
printf("Elements are ");
while(move->next!=NULL)
{
printf("%d ",move->info);
move=move->next;
}
printf("%d\n",move->info);
}
}
Output:-
Main menu
1:insert at first
2:insert at last
3:insert at Specific Position
4:Delete from First
5:Delete from Last
6:Delete from Specific Position
7:Show
8:Exit
Enter Your Choice 2
Enter the number
10
Main menu
1:insert at first
2:insert at last
3:insert at Specific Position
4:Delete from First
5:Delete from Last
6:Delete from Specific Position
7:Show
8:Exit
Enter Your Choice 2
Enter the number
20
Main menu
1:insert at first
2:insert at last
3:insert at Specific Position
4:Delete from First
5:Delete from Last
6:Delete from Specific Position
7:Show
8:Exit
Enter Your Choice 2
Enter the number
30
Main menu
1:insert at first
2:insert at last
3:insert at Specific Position
4:Delete from First
5:Delete from Last
6:Delete from Specific Position
7:Show
8:Exit
Enter Your Choice 7
Elements are 10 20 30
Main menu
1:insert at first
2:insert at last
3:insert at Specific Position
4:Delete from First
5:Delete from Last
6:Delete from Specific Position
7:Show
8:Exit
Enter Your Choice 1
Enter the number
40
Main menu
1:insert at first
2:insert at last
3:insert at Specific Position
4:Delete from First
5:Delete from Last
6:Delete from Specific Position
7:Show
8:Exit
Enter Your Choice 7
Elements are 40 10 20 30
Main menu
1:insert at first
2:insert at last
3:insert at Specific Position
4:Delete from First
5:Delete from Last
6:Delete from Specific Position
7:Show
8:Exit
Enter Your Choice 3
Enter the Position to Insert The no.
3
Enter the number
50
Main menu
1:insert at first
2:insert at last
3:insert at Specific Position
4:Delete from First
5:Delete from Last
6:Delete from Specific Position
7:Show
8:Exit
Enter Your Choice 7
Elements are 40 10 50 20 30
Main menu
1:insert at first
2:insert at last
3:insert at Specific Position
4:Delete from First
5:Delete from Last
6:Delete from Specific Position
7:Show
8:Exit
Enter Your Choice 4
item deleted=40
Main menu
1:insert at first
2:insert at last
3:insert at Specific Position
4:Delete from First
5:Delete from Last
6:Delete from Specific Position
7:Show
8:Exit
Enter Your Choice 7
Elements are 10 50 20 30
Main menu
1:insert at first
2:insert at last
3:insert at Specific Position
4:Delete from First
5:Delete from Last
6:Delete from Specific Position
7:Show
8:Exit
Enter Your Choice 5
item deleted=30
Main menu
1:insert at first
2:insert at last
3:insert at Specific Position
4:Delete from First
5:Delete from Last
6:Delete from Specific Position
7:Show
8:Exit
Enter Your Choice 7
Elements are 10 50 20
Main menu
1:insert at first
2:insert at last
3:insert at Specific Position
4:Delete from First
5:Delete from Last
6:Delete from Specific Position
7:Show
8:Exit
Enter Your Choice 6
Enter the Position From which to Delete The no.
2
item deleted=50
Main menu
1:insert at first
2:insert at last
3:insert at Specific Position
4:Delete from First
5:Delete from Last
6:Delete from Specific Position
7:Show
8:Exit
Enter Your Choice 7
Elements are 10 20
Main menu
1:insert at first
2:insert at last
3:insert at Specific Position
4:Delete from First
5:Delete from Last
6:Delete from Specific Position
7:Show
8:Exit
Enter Your Choice 8