#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
};
void main()
{
struct node *head,*temp,*tp,*tmp;
int value,ch,key;
clrscr();
head=NULL;
printf("1.Insert at first\n2.insert at last\n");
printf("3.insert in middle\n4.delete\n5.display\n");
do
{
printf("\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
tp=head;
printf("\nEnter
no:");
scanf("%d",&value);
temp=(struct node
*)malloc(sizeof(struct node));
temp->data=value;
temp->next=NULL;
if(head==NULL)
{
head=temp;
head->next=head;
}
else
{
temp->next=head;
head->next=temp;
head=temp;
}
break;
case 2:
printf("\nEnter
no:");
scanf("%d",&value);
temp=(struct node
*)malloc(sizeof(struct node));
temp->data=value;
temp->next=NULL;
if(head==NULL)
{
head=temp;
head->next=head;
}
else
{
tp=head;
while(tp->next!=head)
tp=tp->next;
temp->next=head;
tp->next=temp;
}
break;
case 3:
printf("\nEnter
no:");
scanf("%d",&value);
printf("\nEnter
target:");
scanf("%d",&key);
temp=(struct node
*)malloc(sizeof(struct node));
temp->data=value;
temp->next=NULL;
if(head==NULL)
{
head=temp;
head->next=head;
}
else
{
tp=head;
while(tp->data!=key
&& tp!=NULL)
tp=tp->next;
if(tp!=NULL)
{
temp->next=tp->next;
tp->next=temp;
}
else
printf("\nTarget
not found");
}
break;
case 4:
printf("\nEnter
target:");
scanf("%d",&key);
if(head==NULL)
printf("\nList is
empty");
else
{
tp=head;
while(tp->data!=key
&& tp!=NULL)
{
temp=tp;
tp=tp->next;
}
if(tp!=NULL)
{
if(tp==head)
{
while(tp->next!=head)
tp=tp->next;
head=head->next;
free(tp);
tp->next=head;
}
else
{
temp->next=tp->next;
free(tp);
}
}
else
printf("\nTarget
not found");
}
break;
case 5:
tp=head;
while(tp->next!=head)
{
printf("%d\t",tp->data);
tp=tp->next;
}
printf("%d",tp->data);
break;
}
}while(ch<6);
getch();
}
No comments:
Post a Comment