Single linked list

Program to perform operations on a singly linked list
Operations: -
1 : Insert element at the front
2 : Insert element at the end
3 : Insert element after element
4 : Insert element at a position
5 : Delete at the front
6 : Delete at the end
7 : Delete the element
8 : Delete element after

#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
#include"alloc.h"
struct node
{
int info;
struct node *next;
};
typedef struct node *nodeptr;
nodeptr getnode()
{
nodeptr p;
p=(nodeptr)malloc(sizeof(struct node));
p->next=NULL;
return p;
}
void freenode(nodeptr p)
{
free(p);
}
nodeptr ins_front(nodeptr list,int x)
{
nodeptr p;
p=getnode();
p->info=x;
if(list==NULL)
{
list=p;
}
else
{
p->next=list;
list=p;
}
return list;
}
void display(nodeptr list)
{
nodeptr p;
if(list==NULL)
{
printf("\nList empty..!");
return;
}
for(p=list;p!=NULL;p=p->next)
printf("\n%d",p->info);
printf("\n");
}
nodeptr ins_end(nodeptr list,int x)
{
nodeptr p,q;
p=getnode();
p->info=x;
if(list==NULL)
{
list=p;
}
else
{
for(q=list;q->next!=NULL;q=q->next);
q->next=p;
}
return list;
}
nodeptr ins_y(nodeptr list,int x,int y)
{
nodeptr p,q;
int flag=0;
q=getnode();
q->info=x;
if(list==NULL)
{
printf("\nList empty..! Element not found");
}
else
{
for(p=list;p!=NULL;p=p->next)
if(p->info==y)
{
flag=1;
break;
}
if(flag)
{
for(p=list;p->info!=y;p=p->next);
q->next=p->next;
p->next=q;
}
}
return list;
}
nodeptr ins_pos(nodeptr list,int x,int pos)
{
nodeptr p,t,r;
int c=0,i;
p=getnode();
p->info=x;
for(t=list;t!=NULL;t=t->next)
c++;
if(pos==1)
{
p->next=list;
list=p;
}
else
{
if(pos<=c+1)
{
t=list;
for(i=1;i 'lesser than' pos-1;i++)
t=t->next;
r=t->next;
t->next=p;
p->next=r;
}
}
return list;
}
nodeptr del_front(nodeptr list)
{
nodeptr p;
int x;
if(list==NULL)
{
printf("\nInvalid operation...!");
return 0;
}
else
{
p=list;
list=list->next;
x=p->info;
freenode(p);
printf("\n%d deleted from the list",x);
return list;
}
}
nodeptr del_end(nodeptr list)
{
nodeptr p,q;
int x;
if(list==NULL)
{
printf("\nInvalid operation..!");
return 0;
}
else
{
q=NULL;
for(p=list;p->next!=NULL;p=p->next)
q=p;
x=p->info;
freenode(p);
q->next=NULL;
printf("\n%d deleted from the list..",x);
}
return list;
}
nodeptr del_y(nodeptr list,int y)
{
nodeptr p,q;
if(list==NULL)
{
printf("\nInvalid operaton..!");
return NULL;
}
else
if(list->info==y)
{
p=list;
list=list->next;
freenode(p);
printf("\n%d deleted from the list...",y);
}
else
{
q=NULL;
for(p=list;p!=NULL;p=p->next)
{
if(y==p->info)
break;
q=p;
}
q->next=p->next;
freenode(p);
printf("\n%d deleted from the list..",y);
}
return list;
}
nodeptr del_afty(nodeptr list,int y)
{
nodeptr p,q;
int x;
q=NULL;
for(p=list;p!=NULL;p=p->next)
{
if(p->info==y)
{
q=p->next;
p->next=q->next;
x=q->info;
freenode(q);
printf("\n%d is deleted from the list",x);
break;
}
}
return list;
}

main()
{
int x,y,pos,n,n1;
nodeptr list;
clrscr();
list=NULL;
printf("Operations\n-----------");
for(;;)
{

printf("\n1 : Insert\n2 : Delete\n3 : Display\n4 : Exit\n");
printf("\nENTER YOUR CHOICE : ");
scanf("%d",&n);
switch(n)
{
case 1:printf("\n1 : Insert element at the front\n2 : Insert element at the end\n3 : Insert element after element Y\n4 : Insert element at a position\n");
printf("\nEnter your option : ");
scanf("%d",&n1);
switch(n1)
{
case 1:printf("\nEnter the element to be inserted : ");
scanf("%d",&x);
clrscr();
printf("\n%d inserted at the front...\n",x);
list=ins_front(list,x);
break;
case 2:printf("\nEnter the element to be inserted : ");
scanf("%d",&x);
clrscr();
printf("\n%d inserted at the end...\n",x);
list=ins_end(list,x);
break;
case 3:printf("\nEnter the element to be inserted : ");scanf("%d",&x);
printf("Enter the element Y : ");scanf("%d",&y);
printf("\n%d inserted after element y",x);
clrscr();
list=ins_y(list,x,y);
break;
case 4:printf("\nEnter the element to be inserted : ");scanf("%d",&x);
printf("Enter the position : ");scanf("%d",&pos);
printf("\n%d inserted at position %d",x,pos);
clrscr();
list=ins_pos(list,x,pos);
break;
default :printf("\nInavalid operation!!");
break;
}
break;
case 2:printf("\n1 : Delete at the front\n2 : Delete at the end\n3 : Delete the element Y\n4 : Delete element after Y\n");
printf("\nEnter your option : ");scanf("%d",&n1);
switch(n1)
{
case 1:list=del_front(list);
break;
case 2:list=del_end(list);
break;
case 3:printf("\nEnter the element Y : ");scanf("%d",&y);
list=del_y(list,y);
break;
case 4:printf("\nEnter the element Y: ");scanf("%d",&y);
list=del_afty(list,y);
break;
default:printf("\nInvalid operation!!");
break;
}
break;
case 3:clrscr();
printf("\nElements in the list are\n------------------------");
display(list);
break;
case 4:exit(0);
break;
default:printf("\nInvalid operatiom...");
break;
}
}
getch();
return 0;
}

Category:

0 comments: