#include
#include
struct node
{
int data;
struct node *next;
}*head=NULL;
void append(int x)
{
struct node *temp,*p;
p= (struct node *)malloc(sizeof(struct node));
p->data=x;
p->next=NULL;
if(head==NULL)
head=p;
else{
temp=head;
while(temp->next != NULL)
temp=temp->next;
temp->next=p;
}
}
void insertAfter(int x, int y)//insert x after the element y
{
struct node *temp,*p;
p=(struct node *)malloc(sizeof(struct node));
p->data=x;
p->next=NULL;
temp=head;
while(temp->data!=y)
temp=temp->next;
p->next=temp->next;
temp->next=p;
}
void insertBeg(int x)
{
struct node *temp,*p;
p=(struct node *)malloc(sizeof(struct node));
p->data=x;
p->next=head;
head=p;
}
void deleteEnd()
{
struct node *temp;
temp=head;
while(temp->next != NULL)
temp=temp->next;
temp->next=NULL;
}
void deleteBeg()
{
head=head->next;
}
void deleteAny(int x)//delete any Node with data part is x
{
struct node *temp;
temp=head;
while(temp->next->data!=x)
temp=temp->next;
temp->next=temp->next->next;
}
void display()
{
struct node *temp;
temp=head;
while(temp!=NULL){
printf("%d->",temp->data);
temp=temp->next;
}
}
int main()
{
int x,y,opt;
do
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.append\n");
printf("2.InsertAfter\n");
printf("3.InsertBeg\n");
printf("4.Delete Any\n");
printf("5.Delete Beginning\n");
printf("6.Delete End\n");
printf("7.Display\n");
printf("8.Exit\n");
printf("Enter your choice : ");
scanf("%d",&opt);
switch(opt)
{
case 1: printf("Enter the number to append ");
scanf("%d",&x);
append(x);
break;
case 2: printf("Enter the number to insert ");
scanf("%d",&x);
printf("Enter the location element after which element is inserted ");
scanf("%d",&y);
insertAfter(x,y);
break;
break;
case 3: printf("Enter the number to insert Beg ");
scanf("%d",&x);
insertBeg(x);
break;
case 4: printf("Enter the number to be deleted");
scanf("%d",&x);
deleteAny(x);
break;
case 5:
deleteBeg();
break;
case 6:
deleteEnd();
break;
case 7:
display();
break;
case 8:
break;
default: printf("Invalid option\n");
break;
}
}
while(opt!=8);
}
thanks
ReplyDelete