//Operation on singly linked list
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
{
int data;
struct node *next;
}*start=NULL;
void beg_insert();
void mid_insert();
void end_insert();
void display();
void delete();
void search();
int main()
{
int ch;
while(1)
{
printf("\n***************Operation on singly linked list**********************\n");
printf("1.To insert at beginning\n");
printf("2.To insert at middle\n");
printf("3.To insert at end\n");
printf("4.To print data\n");
printf("5.To delete a data\n");
printf("6.To search a data\n");
printf("0.To exit\n");
printf("\nEnter your choice....\n");
scanf("%d",&ch);
switch(ch)
{
case 1: beg_insert();
break;
case 2: mid_insert();
break;
case 3: end_insert();
break;
case 4: display();
break;
case 5: delete();
break;
case 6: search();
break;
case 0: exit(0);
default:printf("\nPlease enter a valid choice");
}
}
}
void beg_insert()
{
int d;
struct node *r;
r=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data to insert.");
scanf("%d",&d);
r->data=d;
r->next=start;
start=r;
}
void mid_insert()
{
int d,n;
struct node *r;
r=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data to insert.");
scanf("%d",&d);
printf("\nEnter data after which new data to be insert");
scanf("%d",&n);
if(start==NULL)
printf("\nList is empty");
else
{
struct node *temp=start;
for(;temp!=NULL;temp=temp->next)
{
if(temp->data==n)
break;
}
r->data=d;
r->next=temp->next;
temp->next=r;
}
}
void end_insert()
{
struct node *r;
int d;
r=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data to insert.");
scanf("%d",&d);
r->data=d;
r->next=NULL;
if(start==NULL)
start=r;
else
{
struct node*temp;
for(temp=start;temp->next!=NULL;temp=temp->next);
temp->next=r;
}
}
void display()
{
struct node *temp=start;
if(start==NULL)
printf("\nlist is empty");
else
for(;temp!=NULL;temp=temp->next)
{
printf("\ndata-> %d",temp->data);
}
}
void delete()
{
int n;
struct node *temp,*prev;
printf("\nEnter data to delete.");
scanf("%d",&n);
if(start==NULL)
printf("\nlist is empty");
else
{
for(temp=start,prev=temp;temp!=NULL;prev=temp,temp=temp->next)
{
if(temp->data==n)
break;
}
prev->next=temp->next;
free(temp);
}
}
void search()
{
int n;
struct node *temp=start;
printf("\nEnter data to search.");
scanf("%d",&n);
if(start==NULL)
printf("\nlist is empty");
else
{
for(;temp!=NULL;temp=temp->next)
{
if(temp->data==n)
break;
}
if(temp==NULL)
printf("\n No data found");
else
printf("\nData found and data-> %d",temp->data);
}
}


No comments:
Post a Comment
Note: only a member of this blog may post a comment.