#include<stdio.h>
#include<conio.h>
typedef
struct lst
{
int
data;
struct
lst *link;
}node;
typedef
struct
{
node *head;
int
count;
}list;
int
menu();
void
insert(int,int);
void
delete();
void
show();
node *setposition(int);
list mylist={NULL,0};
void
main()
{
int choice,pos,data;
do
{
clrscr();
choice=menu();
switch(choice)
{
case 1:
printf("\n ENTER THE
DATA AND POSITION :->");
scanf("%d %d",&data,&pos);
insert(data,pos);
getch();
break;
case 2:
delete();
getch();
break;
case 3:
show();
getch();
break;
case 4:
printf("\n\nEND OF COMPUTATION");
getch();
}
}while(choice!=4);
}
int
menu()
{
int
choice;
do
{
printf("\n\n\t\t\tLINKED LIST OPERATION");
printf("\n\t\t\t*********************");
printf("\nMAIN MENU");
printf("\n=========");
printf("\n\t 1.INSERT");
printf("\n\t 2.DELETE");
printf("\n\t 3.SHOW");
printf("\n\t 4.EXIT");
printf("\n\nEnter your choice :->");
scanf("%d",&choice);
}while(choice<1||choice>4);
return
choice;
}
void
insert(int x,int
p)
{
node *newnode;
node *currnode;
if(p<1||p>mylist.count+1)
{
printf("\nINVALID LOCATION");
return;
}
newnode=(node*)malloc(sizeof(node));
newnode->data=x;
if(p==1)
{
newnode->link=mylist.head;
mylist.head=newnode;
printf("\n\n %d is INSERTED IN LIST",x);
}
else
{
currnode=setposition(p-1);
newnode->link=currnode->link;
currnode->link=newnode;
printf("\n\n %d is INSERTED IN LIST",x);
}
mylist.count++;
}
void
delete()
{
node *prev,*temp;
int
val;
if(mylist.head!=NULL)
{
printf("\n\n ENTER THE DATA TO BE DELETED:");
scanf("%d",&val);
if(mylist.head->data==val)
{
temp=mylist.head->link;
free(mylist.head);
mylist.head=temp;
mylist.count--;
printf("\n\n%d IS DELETED FROM THE LIST",val);
}
else
{
prev=mylist.head;
while(prev->link!=NULL &&
prev->link->data!=val)
prev=prev->link;
if(prev->link!=NULL)
{
temp=prev->link->link;
free(prev->link);
prev->link=temp;
mylist.count--;
}
else
printf("\n\n NO MATCH");
}
}
else
printf("\n\nTHE LIST IS EMPTY");
}
node *setposition(int p)
{
int
i;
node *t=mylist.head;
for(i=2;i<=p;i++)
t=t->link;
return
t;
}
void
show()
{
node *t;
if(mylist.head!=NULL)
{
t=mylist.head;
while(t!=NULL)
{
printf("\n AT %p : %d // %p \n",t,t->data,t->link);
t=t->link;
}
}
else
{
printf("\nTHE LIST IS EMPTY \n");
}
}
No comments:
Post a Comment