You are on page 1of 5

​#include<stdio.

h>
#include<conio.h>
struct node
{
int n;
struct node *next;
};
typedef struct node node;
node *start=NULL;

//Function to add a element at beg

void addbeg()
{
node *t;
t=(node *)malloc(sizeof(node));
if(t==NULL)
{
printf("\n\n Fail to create the node");
}
else
{
printf("\n\n Enter the value for the node");
scanf("%d",&t->n);
t->next=NULL;
if(start==NULL)
{
start=t;
}
else
{
t->next=start;
start=t;
}
}
}

//Function to add a element at last

void addlst()
{
node *t,*p;
t=(node *)malloc(sizeof(node));
if(t==NULL)
{
printf("\n\n Fail to create the node");
}
else
{
printf("\n\n Enter the value for the node");
scanf("%d",&t->n);
t->next=NULL;
if(start==NULL)
{
start=t;
}
else
{
for(p=start;p->next!=NULL;p=p->next);
p->next=t;
}
}
}

//Function to add a element at mid before

void addmdb()
{
node *t,*p;
int srno;
t=(node *)malloc(sizeof(node));
if(t==NULL)
{
printf("\n\n Fail to create the node");
}
else
{
printf("\n\n Enter the value for the node");
scanf("%d",&t->n);
t->next=NULL;
if(start==NULL)
{
start=t;
}
else
{
printf("\n\n\n Enter search number");
scanf("%d",&srno);
for(p=start;p->next->n!=srno && p!=NULL;p=p->next);
if(p==NULL)
printf("\n\n\a Search number not found");
else
{
t->next=p->next;
p->next=t;
}
}
}
}

//Function to remove a element


void rmv()
{
node *t,*p;
int srno;
printf("\n\n\n Enter search number");
scanf("%d",&srno);
if(start->n!=srno)
{
for(p=start;p->next->n!=srno && p!=NULL;p=p->next);
if(p==NULL)
printf("\n\n\a Search number not found");
else
{
t=p->next;
p->next=t->next;
free(t);
}
}
else
{
t=start;
start=t->next;
free(t);
}

}
// Function to display the list

void disp()
{
node *p;
printf("\n\n\n");
for(p=start;p!=NULL;p=p->next)
{
printf("%5d",p->n);
}
}

void main()
{
int l;

while(1)
{
clrscr();
printf("\n\n\n\n 1..... Add a node at beg");
printf("\n\n 2..... Add a node at end");
printf("\n\n 3..... Add a node at mid before");
printf("\n\n 4..... Add a node at mid after");
printf("\n\n 5..... Display the list");
printf("\n\n 6..... Remove the element from the list");
printf("\n\n 9..... quit");
printf("\n\n\n\n\n Enter your choice");
scanf("%d",&l);
switch(l)
{
case 1:addbeg();break;
case 2:addlst();break;
case 3:addmdb();break;
case 5:disp();break;
case 6:rmv();break;
}

if(l==9)
break;
getch();
}

You might also like