Operations of Linear Linked List ...
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct linked
{
int val;
struct linked *next;
}lnk;
lnk *create(lnk*,int);
lnk *insertbefore(lnk*,int);
void insertafter(lnk*,int);
lnk *del(lnk*,int);
void disp(lnk*);
void main()
{
lnk *h=NULL;
int v,ch;
while(1)
{
printf("\n====================================");
printf("\n1)Create\n2)Insert Before\n3)Insert After\n4)Delete\n5)Display\n6)Exit");
printf("\n====================================");
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the value you want to enter:");
scanf("%d",&v);
h=create(h,v);
break;
case 2: printf("\nEnter the value before which you want to Insert:");
scanf("%d", &v);
h=insertbefore(h,v);
break;
case 3: printf("\nEnter the value after which you want to Insert:");
scanf("%d",&v);
insertafter(h,v);
break;
case 4: printf("\nEnter the value which you want to delete:");
scanf("%d",&v);
h=del(h,v);
break;
case 5: printf("\nThe values in the list are:");
disp(h);
break;
case 6: exit(0);
default: printf("\n Not a proper choice,Sir");
}
}
}
//create the linked list
lnk *create(lnk *h,int v)
{
lnk *temp,*ptr;
temp=(lnk*)malloc(sizeof(lnk));
temp->val=v;
temp->next=NULL;
if(h==NULL)
{
h=temp;
}
else
{
ptr=h;
while(ptr->next!=NULL)
{
ptr=ptr->next;
ptr->next=temp;
}
}
return h;
}
//insert a node before a particular node
lnk *insertbefore(lnk *h,int v)
{
lnk *ptr,*pre,*temp;
int p;
printf("\nEnter the value of the inserting node:");
scanf("%d",&p);
temp=(lnk*)malloc(sizeof(lnk));
temp->val=p;
temp->next=NULL;
if(h->val==v)
{
temp->next=h;
h=temp;
}
else
{
ptr=h;
while(ptr->val!=v && ptr!=NULL)
{
pre=ptr;
ptr=ptr->next;
}
temp->next=pre->next;
pre->next=temp;
}
return h;
}
//insert a node after a particular node
void insertafter(lnk *h,int v)
{
lnk *ptr,*temp;
int p;
ptr=h;
while(ptr->val!=v && ptr!=NULL)
{
ptr=ptr->next;
}
printf("\nEnter the value of the inserting node:");
scanf("%d",&p);
temp=(lnk*)malloc(sizeof(lnk));
temp->next=NULL;
temp->val=p;
if(ptr!=NULL)printf("\n====================================");
{
temp->next=ptr->next;
ptr->next=temp;
}
else
printf("\nvalue not found");
}
//delete a particular node
lnk *del(lnk *h,int v)
{
lnk *pre,*ptr;
ptr=h;
if(h->val!=v)
{
while(ptr->val!=v && ptr!=NULL)
{
pre=ptr;
ptr=ptr->next;
}
if(ptr!=NULL)
{
pre->next=ptr->next;
}
else
{
printf("\nvalue not found");
}
}
else
{
h=h->next;
}
return h;
}
//display the linked list
void disp(lnk *h)
{
lnk *ptr;
ptr=h;
while(ptr!=NULL)
{
printf("%d->",ptr->val);
ptr=ptr->next;
}
}
I/O : detail description coming soon .....
#include<conio.h>
#include<stdlib.h>
typedef struct linked
{
int val;
struct linked *next;
}lnk;
lnk *create(lnk*,int);
lnk *insertbefore(lnk*,int);
void insertafter(lnk*,int);
lnk *del(lnk*,int);
void disp(lnk*);
void main()
{
lnk *h=NULL;
int v,ch;
while(1)
{
printf("\n====================================");
printf("\n1)Create\n2)Insert Before\n3)Insert After\n4)Delete\n5)Display\n6)Exit");
printf("\n====================================");
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the value you want to enter:");
scanf("%d",&v);
h=create(h,v);
break;
case 2: printf("\nEnter the value before which you want to Insert:");
scanf("%d", &v);
h=insertbefore(h,v);
break;
case 3: printf("\nEnter the value after which you want to Insert:");
scanf("%d",&v);
insertafter(h,v);
break;
case 4: printf("\nEnter the value which you want to delete:");
scanf("%d",&v);
h=del(h,v);
break;
case 5: printf("\nThe values in the list are:");
disp(h);
break;
case 6: exit(0);
default: printf("\n Not a proper choice,Sir");
}
}
}
//create the linked list
lnk *create(lnk *h,int v)
{
lnk *temp,*ptr;
temp=(lnk*)malloc(sizeof(lnk));
temp->val=v;
temp->next=NULL;
if(h==NULL)
{
h=temp;
}
else
{
ptr=h;
while(ptr->next!=NULL)
{
ptr=ptr->next;
ptr->next=temp;
}
}
return h;
}
//insert a node before a particular node
lnk *insertbefore(lnk *h,int v)
{
lnk *ptr,*pre,*temp;
int p;
printf("\nEnter the value of the inserting node:");
scanf("%d",&p);
temp=(lnk*)malloc(sizeof(lnk));
temp->val=p;
temp->next=NULL;
if(h->val==v)
{
temp->next=h;
h=temp;
}
else
{
ptr=h;
while(ptr->val!=v && ptr!=NULL)
{
pre=ptr;
ptr=ptr->next;
}
temp->next=pre->next;
pre->next=temp;
}
return h;
}
//insert a node after a particular node
void insertafter(lnk *h,int v)
{
lnk *ptr,*temp;
int p;
ptr=h;
while(ptr->val!=v && ptr!=NULL)
{
ptr=ptr->next;
}
printf("\nEnter the value of the inserting node:");
scanf("%d",&p);
temp=(lnk*)malloc(sizeof(lnk));
temp->next=NULL;
temp->val=p;
if(ptr!=NULL)printf("\n====================================");
{
temp->next=ptr->next;
ptr->next=temp;
}
else
printf("\nvalue not found");
}
//delete a particular node
lnk *del(lnk *h,int v)
{
lnk *pre,*ptr;
ptr=h;
if(h->val!=v)
{
while(ptr->val!=v && ptr!=NULL)
{
pre=ptr;
ptr=ptr->next;
}
if(ptr!=NULL)
{
pre->next=ptr->next;
}
else
{
printf("\nvalue not found");
}
}
else
{
h=h->next;
}
return h;
}
//display the linked list
void disp(lnk *h)
{
lnk *ptr;
ptr=h;
while(ptr!=NULL)
{
printf("%d->",ptr->val);
ptr=ptr->next;
}
}
I/O : detail description coming soon .....
Comments
Post a Comment