This is a program to implement the various functions of a link list. If you have any doubts please let me know.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *newn,*start=NULL,*ptr=NULL,*prev=NULL,*rev,*temp=NULL;
void insbeg();
void insend();
void inspos();
void insaft();
void delbeg();
void delend();
void delelement();
void display();
main()
{
int ch1,ch,ch2;
do
{
printf("\nMenu:\n1.Insertion\n2.Deletion\n3.Reversal\n4.Display\n5.Exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
do
{
printf("\nSubmenu\n1.Insertion at beginning\n2.Insetion at End\n3.Insert at a specified position\n4.Insert after a specified node\n5.Exit from menu\n");
printf("Enter your choice:");
scanf("%d",&ch1);
switch(ch1)
{
case 1:insbeg();
break;
case 2:
insend();
break;
case 3:
inspos();
break;
case 4:
insaft();
break;
case 5:
break;
default:printf("Wrong choice\n");
break;
}
}while(ch1!=5);
break;
case 2:
do
{
printf("\nSubmenu\n1.Deletion at beginning\n2.Deletion at end\n3.Deletion of specific node\n4.Exit from this menu\n");
printf("Enter your choice:");
scanf("%d",&ch2);
switch(ch2)
{
case 1:delbeg();
break;
case 2:delend();
break;
case 3:delelement();
break;
case 4:
break;
default:printf("Wrong choice\n");
break;
}
}while(ch2!=4);
break;
case 3:
if(start==NULL)
{
printf("\nUnderflow\n");
}
else
{
ptr=start;
prev=NULL;
while(ptr->next!=NULL)
{
rev=prev;
prev=ptr;
ptr=ptr->next;
prev->next=rev;
}
start=prev;
display();
}
break;
case 4:
display();
break;
case 5:
break;
default:printf("\nWrong Choice\n");
break;
}
}while(ch!=5);
}
void insbeg()
{
int num;
printf("Enter the element:");
scanf("%d",&num);
newn=(struct node *)malloc(sizeof(struct node));
if(start==NULL)
{
newn->data=num;
newn->next=NULL;
start=newn;
}
else
{
newn->data=num;
newn->next=start;
start=newn;
}
}
void insend()
{
int num;
printf("Enter the element:");
scanf("%d",&num);
newn=(struct node *)malloc(sizeof(struct node));
if(start==NULL)
{
newn->data=num;
newn->next=NULL;
start=newn;
}
else
{
ptr=start;
while((ptr->next)!=NULL)
ptr=ptr->next;
newn->data=num;
ptr->next=newn;
newn->next=NULL;
}
}
void inspos()
{
int num,k=0,n;
ptr=start;
printf("Enter the the position at which the element must be inserted:");
scanf("%d",&n);
if(n==1)
{
insbeg();
}
else
{
printf("Enter the element:");
scanf("%d",&num);
while(k!=(n-1))
{
prev=ptr;
ptr=ptr->next;
k++;
}
newn=(struct node *)malloc(sizeof(struct node));
newn->data=num;
newn->next=ptr;
prev->next=newn;
}
}
void insaft()
{
int n,num,c=0;
ptr=start;
printf("Enter the element:");
scanf("%d",&num);
printf("Enter the element after which insertion must be done:");
scanf("%d",&n);
while(ptr!=NULL)
{
if(ptr->data!=n)
{
ptr=ptr->next;
}
else
{
c++;
{
newn=(struct node *)malloc(sizeof(struct node));
newn->data=num;
newn->next=ptr->next;
ptr->next=newn;
ptr=ptr->next;
break;
}
}
}
if(c==0)
printf("NODE NOT FOUND");
}
void delbeg()
{
if(start==NULL)
{
printf("\nUnderflow\n");
}
else
{
ptr=start;
start=start->next;
free(ptr);
}
}
void delend()
{
if(start==NULL)
{
printf("\nUnderflow\n");
}
else if(start->next==NULL)
delbeg();
else
{
ptr=start;
while(ptr->next!=NULL)
{
prev=ptr;
ptr=ptr->next;
}
prev->next=NULL;
free(ptr);
}
}
void delelement()
{
int num,c=0;
if(start==NULL)
{
printf("\nUnderflow\n");
}
else
{
printf("Enter the element to be deleted:");
scanf("%d",&num);
ptr=start;
while(ptr!=NULL)
{
if(start->data==num)
{
c++;
delbeg();
ptr=start;
break;
}
else if(ptr->data==num)
{
c++;
temp=ptr;
prev->next=temp->next;
ptr=ptr->next;
free(temp);
break;
}
prev=ptr;
ptr=ptr->next;
}
if(c==0)
{
printf("NODE NOT FOUND");
}
}
}
void display()
{
if(start==NULL)
{
printf("\nUnderflow\n");
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
printf("=>%d ",ptr->data);
ptr=ptr->next;
}
printf("=>%d ",ptr->data);
}
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *newn,*start=NULL,*ptr=NULL,*prev=NULL,*rev,*temp=NULL;
void insbeg();
void insend();
void inspos();
void insaft();
void delbeg();
void delend();
void delelement();
void display();
main()
{
int ch1,ch,ch2;
do
{
printf("\nMenu:\n1.Insertion\n2.Deletion\n3.Reversal\n4.Display\n5.Exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
do
{
printf("\nSubmenu\n1.Insertion at beginning\n2.Insetion at End\n3.Insert at a specified position\n4.Insert after a specified node\n5.Exit from menu\n");
printf("Enter your choice:");
scanf("%d",&ch1);
switch(ch1)
{
case 1:insbeg();
break;
case 2:
insend();
break;
case 3:
inspos();
break;
case 4:
insaft();
break;
case 5:
break;
default:printf("Wrong choice\n");
break;
}
}while(ch1!=5);
break;
case 2:
do
{
printf("\nSubmenu\n1.Deletion at beginning\n2.Deletion at end\n3.Deletion of specific node\n4.Exit from this menu\n");
printf("Enter your choice:");
scanf("%d",&ch2);
switch(ch2)
{
case 1:delbeg();
break;
case 2:delend();
break;
case 3:delelement();
break;
case 4:
break;
default:printf("Wrong choice\n");
break;
}
}while(ch2!=4);
break;
case 3:
if(start==NULL)
{
printf("\nUnderflow\n");
}
else
{
ptr=start;
prev=NULL;
while(ptr->next!=NULL)
{
rev=prev;
prev=ptr;
ptr=ptr->next;
prev->next=rev;
}
start=prev;
display();
}
break;
case 4:
display();
break;
case 5:
break;
default:printf("\nWrong Choice\n");
break;
}
}while(ch!=5);
}
void insbeg()
{
int num;
printf("Enter the element:");
scanf("%d",&num);
newn=(struct node *)malloc(sizeof(struct node));
if(start==NULL)
{
newn->data=num;
newn->next=NULL;
start=newn;
}
else
{
newn->data=num;
newn->next=start;
start=newn;
}
}
void insend()
{
int num;
printf("Enter the element:");
scanf("%d",&num);
newn=(struct node *)malloc(sizeof(struct node));
if(start==NULL)
{
newn->data=num;
newn->next=NULL;
start=newn;
}
else
{
ptr=start;
while((ptr->next)!=NULL)
ptr=ptr->next;
newn->data=num;
ptr->next=newn;
newn->next=NULL;
}
}
void inspos()
{
int num,k=0,n;
ptr=start;
printf("Enter the the position at which the element must be inserted:");
scanf("%d",&n);
if(n==1)
{
insbeg();
}
else
{
printf("Enter the element:");
scanf("%d",&num);
while(k!=(n-1))
{
prev=ptr;
ptr=ptr->next;
k++;
}
newn=(struct node *)malloc(sizeof(struct node));
newn->data=num;
newn->next=ptr;
prev->next=newn;
}
}
void insaft()
{
int n,num,c=0;
ptr=start;
printf("Enter the element:");
scanf("%d",&num);
printf("Enter the element after which insertion must be done:");
scanf("%d",&n);
while(ptr!=NULL)
{
if(ptr->data!=n)
{
ptr=ptr->next;
}
else
{
c++;
{
newn=(struct node *)malloc(sizeof(struct node));
newn->data=num;
newn->next=ptr->next;
ptr->next=newn;
ptr=ptr->next;
break;
}
}
}
if(c==0)
printf("NODE NOT FOUND");
}
void delbeg()
{
if(start==NULL)
{
printf("\nUnderflow\n");
}
else
{
ptr=start;
start=start->next;
free(ptr);
}
}
void delend()
{
if(start==NULL)
{
printf("\nUnderflow\n");
}
else if(start->next==NULL)
delbeg();
else
{
ptr=start;
while(ptr->next!=NULL)
{
prev=ptr;
ptr=ptr->next;
}
prev->next=NULL;
free(ptr);
}
}
void delelement()
{
int num,c=0;
if(start==NULL)
{
printf("\nUnderflow\n");
}
else
{
printf("Enter the element to be deleted:");
scanf("%d",&num);
ptr=start;
while(ptr!=NULL)
{
if(start->data==num)
{
c++;
delbeg();
ptr=start;
break;
}
else if(ptr->data==num)
{
c++;
temp=ptr;
prev->next=temp->next;
ptr=ptr->next;
free(temp);
break;
}
prev=ptr;
ptr=ptr->next;
}
if(c==0)
{
printf("NODE NOT FOUND");
}
}
}
void display()
{
if(start==NULL)
{
printf("\nUnderflow\n");
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
printf("=>%d ",ptr->data);
ptr=ptr->next;
}
printf("=>%d ",ptr->data);
}
}
No comments:
Post a Comment