#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *link;
};
struct node *first;
int main()
{
struct node *ptr,*cpt,*tpt;
int s;
char n;
do
{
printf("\nHi guys plase select the option to initiate the link list");
printf("\nPress 1 for creation");
printf("\nPress 2 for display");
printf("\nPress 3 for insertion at the beginning");
printf("\nPress 4 for insertion at end");
printf("\nPress 5 for insertion at a given location");
printf("\nPress 6 to delete from the beginning");
printf("\nPress 7 to delete from the end");
printf("\nPress 8 to delete a specific element");
printf("\nPress 9 to terminate");
printf("\nEnter your choice:");
scanf("%d",&s);
switch(s)
{
case 1:
//Create a new link list
ptr=(struct node *)malloc(sizeof(struct node));
printf("ENTER THE VALUE");
scanf("%d",&ptr->info);
first=ptr;
do
{
cpt=(struct node *)malloc(sizeof(struct node));
printf("ENTER THE VALUE");
scanf("%d",&cpt->info);
ptr->link=cpt;
ptr=cpt;
printf("\nEnter Y for more nodes");
scanf("%s",&n);
}
while(n=='y');
ptr->link=NULL;
break;
case 2: // Display a linked list
ptr=(struct node *)malloc(sizeof(struct node));
ptr=first;
while(ptr!=0)
{
printf("%d\t",ptr->info);
ptr=ptr->link;
}
break;
case 3: tpt=(struct node *)malloc(sizeof(struct node));
printf("ENTER THE VALUE");
scanf("%d",&tpt->info);
tpt->link=first;
first=tpt;
break;
case 4:tpt=(struct node *)malloc(sizeof(struct node));
printf("ENTER THE VALUE");
scanf("%d",&tpt->info);
ptr=first;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
ptr->link=tpt;
tpt->link=NULL;
break;
case 5: //Insert at a given location
ptr=(struct node *)malloc(sizeof(struct node));
int data;
struct node *tpt;
tpt=(struct node *)malloc(sizeof(struct node));
printf("ENTER THE VALUE");
scanf("%d",&tpt->info);
printf("Enter the location in which insertion has to be made!");
scanf("%d",&data);
ptr=first;
while(ptr->info!=data)
{
ptr=ptr->link;
}
tpt->link=ptr->link;
ptr->link=tpt;
ptr=(struct node *)malloc(sizeof(struct node));
ptr=first;
while(ptr!=0)
{
printf("%d\t",ptr->info);
ptr=ptr->link;
}
break;
case 6: ptr=first;
first=ptr->link;
free(ptr);
break;
case 7: ptr=first;
while(ptr->link!=NULL)
{
cpt=ptr;
ptr=ptr->link;
}
cpt->link=NULL;
free(ptr);
break;
case 8:
// Deletion of a specific location
ptr=(struct node *)malloc(sizeof(struct node));
cpt=(struct node *)malloc(sizeof(struct node));
int loc;
ptr=(struct node *)malloc(sizeof(struct node));
ptr=first;
while(ptr!=0)
{
printf("%d\t",ptr->info);
ptr=ptr->link;
}
printf("\nEnter the location where you have to delete:");
scanf("%d",&loc);
ptr=first;
while(ptr->info!=loc)
{
cpt=ptr;
ptr=ptr->link;
}
cpt->link=ptr->link;
free(ptr);
}
}
while(s<9);
return 0;
}
No comments:
Post a Comment