Linked List Implementation using Dynamic Memory Allocation
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
struct node * insf(struct node *header,int item)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=item;
newnode->next=header->next;
header->next=newnode;
return header;
}
struct node * insr(struct node *header,int item)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->next=NULL;
newnode->data=item;
struct node *ptr=header->next;
if(ptr!= NULL)
{
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newnode;
}
else
header->next=newnode;
return header;
}
struct node * insany(struct node *header,int item,int pos)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->next=NULL;
newnode->data=item;
int i=1;
if(pos==1)
{
newnode->next=header->next;
header->next=newnode;
}
else
{
struct node *ptr=header->next;
while(i+1!=pos)
{
ptr=ptr->next;
i++;
}
if(ptr==NULL)
printf("No such position!!");
else
{
newnode->next=ptr->next;
ptr->next=newnode;
}
}
return header;
}
struct node * delf(struct node *header)
{
struct node *ptr=header->next;
if(ptr==NULL)
printf("\n List is empty!!");
else
{
header->next=ptr->next;
free(ptr);
}
return header;
}
struct node * delr(struct node *header)
{
struct node *ptr=header->next;
struct node *ptr1=NULL;
if(ptr==NULL)
printf("\n List is empty!!");
else
{
while(ptr->next!=NULL)
{
ptr1=ptr;
ptr=ptr->next;
}
if(header->next==ptr)
{
header->next=NULL;
}
else
ptr1->next=NULL;
free(ptr);
}
return header;
}
struct node * delany(struct node *header,int item)
{
int flag=0;
struct node *ptr=header->next;
struct node *ptr1=NULL;
struct node *ptr2=NULL;
if(ptr==NULL)
printf("\n List is empty!!");
else
{
while(ptr!=NULL && flag==0)
{
if(ptr->data==item)
flag=1;
else
{
ptr1=ptr;
ptr=ptr->next;
}
}
if(ptr==NULL)
printf("\n No such element found!!");
else
{
if(header->next==ptr)
header->next=ptr->next;
else
ptr1->next=ptr->next;
free(ptr);
}
}
return header;
}
void traverse(struct node *header)
{
struct node *ptr=header->next;
while(ptr!=NULL)
{
printf(" %d",ptr->data);
ptr=ptr->next;
}
}
int main()
{
struct node *header;
header=(struct node *)malloc(sizeof(struct node));
header->next=NULL;
int ch=0,item,pos;
do
{
printf("\n MENU ");
printf("\n 1.Insert at front");
printf("\n 2.Insert at rear");
printf("\n 3.Insert at any position");
printf("\n 4.Delete at front");
printf("\n 5.Delete at rear");
printf("\n 6.Delete any element");
printf("\n 7.Exit");
printf("\n Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\n Enter the value : ");
scanf("%d",&item);
header=insf(header,item);
traverse(header);
break;
case 2 : printf("\n Enter the value : ");
scanf("%d",&item);
header=insr(header,item);
traverse(header);
break;
case 3 : printf("\n Enter the value : ");
scanf("%d",&item);
printf("\n Enter the position : ");
scanf("%d",&pos);
header=insany(header,item,pos);
traverse(header);
break;
case 4 : header=delf(header);
traverse(header);
break;
case 5 : header=delr(header);
traverse(header);
break;
case 6 : printf("\n Enter the value : ");
scanf("%d",&item);
header=delany(header,item);
traverse(header);
break;
case 7 : exit(0);
default : printf("\n Wrong Choice!!");
}
}while(ch!=7);
}
#include<conio.h>
struct node
{
int data;
struct node *next;
};
struct node * insf(struct node *header,int item)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=item;
newnode->next=header->next;
header->next=newnode;
return header;
}
struct node * insr(struct node *header,int item)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->next=NULL;
newnode->data=item;
struct node *ptr=header->next;
if(ptr!= NULL)
{
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newnode;
}
else
header->next=newnode;
return header;
}
struct node * insany(struct node *header,int item,int pos)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->next=NULL;
newnode->data=item;
int i=1;
if(pos==1)
{
newnode->next=header->next;
header->next=newnode;
}
else
{
struct node *ptr=header->next;
while(i+1!=pos)
{
ptr=ptr->next;
i++;
}
if(ptr==NULL)
printf("No such position!!");
else
{
newnode->next=ptr->next;
ptr->next=newnode;
}
}
return header;
}
struct node * delf(struct node *header)
{
struct node *ptr=header->next;
if(ptr==NULL)
printf("\n List is empty!!");
else
{
header->next=ptr->next;
free(ptr);
}
return header;
}
struct node * delr(struct node *header)
{
struct node *ptr=header->next;
struct node *ptr1=NULL;
if(ptr==NULL)
printf("\n List is empty!!");
else
{
while(ptr->next!=NULL)
{
ptr1=ptr;
ptr=ptr->next;
}
if(header->next==ptr)
{
header->next=NULL;
}
else
ptr1->next=NULL;
free(ptr);
}
return header;
}
struct node * delany(struct node *header,int item)
{
int flag=0;
struct node *ptr=header->next;
struct node *ptr1=NULL;
struct node *ptr2=NULL;
if(ptr==NULL)
printf("\n List is empty!!");
else
{
while(ptr!=NULL && flag==0)
{
if(ptr->data==item)
flag=1;
else
{
ptr1=ptr;
ptr=ptr->next;
}
}
if(ptr==NULL)
printf("\n No such element found!!");
else
{
if(header->next==ptr)
header->next=ptr->next;
else
ptr1->next=ptr->next;
free(ptr);
}
}
return header;
}
void traverse(struct node *header)
{
struct node *ptr=header->next;
while(ptr!=NULL)
{
printf(" %d",ptr->data);
ptr=ptr->next;
}
}
int main()
{
struct node *header;
header=(struct node *)malloc(sizeof(struct node));
header->next=NULL;
int ch=0,item,pos;
do
{
printf("\n MENU ");
printf("\n 1.Insert at front");
printf("\n 2.Insert at rear");
printf("\n 3.Insert at any position");
printf("\n 4.Delete at front");
printf("\n 5.Delete at rear");
printf("\n 6.Delete any element");
printf("\n 7.Exit");
printf("\n Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\n Enter the value : ");
scanf("%d",&item);
header=insf(header,item);
traverse(header);
break;
case 2 : printf("\n Enter the value : ");
scanf("%d",&item);
header=insr(header,item);
traverse(header);
break;
case 3 : printf("\n Enter the value : ");
scanf("%d",&item);
printf("\n Enter the position : ");
scanf("%d",&pos);
header=insany(header,item,pos);
traverse(header);
break;
case 4 : header=delf(header);
traverse(header);
break;
case 5 : header=delr(header);
traverse(header);
break;
case 6 : printf("\n Enter the value : ");
scanf("%d",&item);
header=delany(header,item);
traverse(header);
break;
case 7 : exit(0);
default : printf("\n Wrong Choice!!");
}
}while(ch!=7);
}
OUTPUT :
MENU
1.Insert at front
2.Insert at rear
3.Insert at any position
4.Delete at front
5.Delete at rear
6.Delete any element
7.Exit
Enter your choice : 1
Enter the value : 1
1
MENU
1.Insert at front
2.Insert at rear
3.Insert at any position
4.Delete at front
5.Delete at rear
6.Delete any element
7.Exit
Enter your choice : 1
Enter the value : 2
2 1
MENU
1.Insert at front
2.Insert at rear
3.Insert at any position
4.Delete at front
5.Delete at rear
6.Delete any element
7.Exit
Enter your choice : 1
Enter the value : 3
3 2 1
MENU
1.Insert at front
2.Insert at rear
3.Insert at any position
4.Delete at front
5.Delete at rear
6.Delete any element
7.Exit
Enter your choice : 2
Enter the value : 4
3 2 1 4
MENU
1.Insert at front
2.Insert at rear
3.Insert at any position
4.Delete at front
5.Delete at rear
6.Delete any element
7.Exit
Enter your choice : 2
Enter the value : 5
3 2 1 4 5
MENU
1.Insert at front
2.Insert at rear
3.Insert at any position
4.Delete at front
5.Delete at rear
6.Delete any element
7.Exit
Enter your choice : 2
Enter the value : 6
3 2 1 4 5 6
MENU
1.Insert at front
2.Insert at rear
3.Insert at any position
4.Delete at front
5.Delete at rear
6.Delete any element
7.Exit
Enter your choice : 3
Enter the value : 7
Enter the position : 4
3 2 1 7 4 5 6
MENU
1.Insert at front
2.Insert at rear
3.Insert at any position
4.Delete at front
5.Delete at rear
6.Delete any element
7.Exit
Enter your choice : 4
2 1 7 4 5 6
MENU
1.Insert at front
2.Insert at rear
3.Insert at any position
4.Delete at front
5.Delete at rear
6.Delete any element
7.Exit
Enter your choice : 5
2 1 7 4 5
MENU
1.Insert at front
2.Insert at rear
3.Insert at any position
4.Delete at front
5.Delete at rear
6.Delete any element
7.Exit
Enter your choice : 6
Enter the value : 7
2 1 4 5
MENU
1.Insert at front
2.Insert at rear
3.Insert at any position
4.Delete at front
5.Delete at rear
6.Delete any element
7.Exit
Enter your choice :
No comments