Linked List Implementation
#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 * 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 * search(struct node *header,int item)
{
int flag=0,i=1;
struct node *ptr=header->next;
struct node *ptr1=NULL;
if(ptr==NULL)
printf("\n List is empty!!");
else
{
while(ptr!=NULL && flag==0)
{
if(ptr->data==item)
flag=1;
else
{
i++;
ptr1=ptr;
ptr=ptr->next;
}
}
if(ptr==NULL)
printf("\n No such element found!!\n");
else
{
printf("\nThe element found at position : %d \n",i);
}
}
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;
do
{
printf("\n MENU ");
printf("\n 1.Insert at front");
printf("\n 2.Delete at rear");
printf("\n 3.Search an element");
printf("\n 4.Traverse");
printf("\n 5.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 : header=delr(header);
traverse(header);
break;
case 3 : printf("\n Enter the value to be searched of : ");
scanf("%d",&item);
header=search(header,item);
traverse(header);
break;
case 4 : traverse(header);
break;
case 7 : exit(0);
default : printf("\n Wrong Choice!!");
}
}while(ch!=5);
}
OUTPUT:
MENU
1.Insert at front
2.Delete at rear
3.Search an element
4.Traverse
5.Exit
Enter your choice : 1
Enter the value : 1
1->
MENU
1.Insert at front
2.Delete at rear
3.Search an element
4.Traverse
5.Exit
Enter your choice : 1
Enter the value : 2
2-> 1->
MENU
1.Insert at front
2.Delete at rear
3.Search an element
4.Traverse
5.Exit
Enter your choice : 1
Enter the value : 3
3-> 2-> 1->
MENU
1.Insert at front
2.Delete at rear
3.Search an element
4.Traverse
5.Exit
Enter your choice : 1
Enter the value : 4
4-> 3-> 2-> 1->
MENU
1.Insert at front
2.Delete at rear
3.Search an element
4.Traverse
5.Exit
Enter your choice : 1
Enter the value : 5
5-> 4-> 3-> 2-> 1->
MENU
1.Insert at front
2.Delete at rear
3.Search an element
4.Traverse
5.Exit
Enter your choice : 2
5-> 4-> 3-> 2->
MENU
1.Insert at front
2.Delete at rear
3.Search an element
4.Traverse
5.Exit
Enter your choice : 2
5-> 4-> 3->
MENU
1.Insert at front
2.Delete at rear
3.Search an element
4.Traverse
5.Exit
Enter your choice : 3
Enter the value to be searched of : 5
The element found at position : 1
5-> 4-> 3->
MENU
1.Insert at front
2.Delete at rear
3.Search an element
4.Traverse
5.Exit
Enter your choice : 3
Enter the value to be searched of : 4
The element found at position : 2
5-> 4-> 3->
MENU
1.Insert at front
2.Delete at rear
3.Search an element
4.Traverse
5.Exit
Enter your choice : 4
5-> 4-> 3->
MENU
1.Insert at front
2.Delete at rear
3.Search an element
4.Traverse
5.Exit
Enter your choice :
No comments