#include<stdio.h>
struct node
{
int info;
struct node *next;
};
typedef struct node *newnode;
newnode first,newn,pred,temp,avail,save;
void main()
{
int i,n,w,q,y,x;
clrscr();
textcolor(11);
first=NULL;
avail=(newnode)malloc(sizeof(struct node));
avail->next=NULL;
printf("\n\n\t\tENTER VALUE OF NODE WHICH YOU WANT TO CREATE NODE SPACE>>");
scanf("%d",&n);
temp=avail;
for(i=1;i<n;i++)
{
temp->next=(newnode)malloc(sizeof(struct node));
temp=temp->next;
temp->next=NULL;
}
m:
clrscr();
textcolor(10);
printf("\n\n\t\tCREATED BY DIXIT PANCHAL");
printf("\n\t\t ");
printf("\n\t\t ");
printf("\n\t\t PRESS 1 For insert at First ");
printf("\n\t\t PRESS 2 For insert at Last ");
printf("\n\t\t PRESS 3 For insert as order ");
printf("\n\t\t PRESS 4 For Display ");
printf("\n\t\t PRESS 5 For Delete First Element ");
printf("\n\t\t PRESS 6 For Delete Last Element ");
printf("\n\t\t PRESS 7 For Delete Order Element ");
printf("\n\t\t PRESS 0 For Exit ");
printf("\n\t\t ");
printf("\n\t\t ");
scanf("%d",&w);
switch(w)
{
case 1:
printf("\n\n\t\t<<YOU HAVE PRESSES 1 FOR INSERT NODE AT FIRST.>>");
if(avail==NULL)
printf("\n\t\t<<<Node is not available>>>");
else
{
newn=avail;
avail=avail->next;
printf("\n\t\t<<<<Enter value>>>");
scanf("%d",&q);
newn->info=q;
if(first==NULL)
{
newn->next=NULL;
first=newn;
}
else
{
newn->next=first;
first=newn;
}
}
getch();
goto m;
case 2:
printf("\n\n\t\t<<YOU HAVE PRESSES 2 FOR INSERT NODE AT LAST.>>");
if (avail==NULL)
printf("\n\n\t\t<<Sorry Node is not Available.>>");
else
{
newn=avail;
avail=avail->next;
printf("\n\n\t\t<<Enter Value>>");
scanf("%d",&newn->info);
newn->next=NULL;
if(first==NULL)
{
first=newn;
}
else
{
save=first;
while(save->next!=NULL)
{
save=save->next;
}
save->next=newn;
}
}
getch();
goto m;
case 3:
printf("\n\n\t\t<<YOU HAVE PRESSES 3 FOR INSERT NODE AS ORDER.>>");
if(avail==NULL)
printf("\n\n\t\t<<Sorry, Node is not Available>>");
else
{
newn=avail;
avail->next=avail;
printf("\n\n\t\t<<Enter Value>>");
scanf("%d",&newn->info);
if(first==NULL)
{
newn->next=NULL;
first=newn;
}
else
{
if(newn->info<=first->info)
{
newn->next=first;
first=newn;
}
else
{
save=first;
while(save->next!=NULL&&(save->next)->info<=newn->info)
{
save=save->next;
}
newn->next=save->next;
save->next=newn;
}
}
}
getch();
goto m;
case 4:
printf("\n\n\t\t<<YOU HAVE PRESSES 4 FOR DISPLAY.>>");
getch();
if(first==NULL)
{
printf("\n\n\t\t<<NO NODE IN LINKED LIST >>");
}
else
{
printf("\n\n\t\t| ELEMENTS |ADD. |ADD.OF NEXT NODE |\n\n ");
temp=first;
while(temp !=NULL)
{
printf("\n\t\t| %d | | %u | | %u |",temp->info,temp->next,temp);
temp=temp->next;
}
}
getch();
goto m;
case 5:
printf("\n\n\t\t<<YOU HAVE PRESSES 5 FOR DELETE FIRST NODE .>>");
getch();
if(first==NULL)
{
printf("\n\t\t NO NODE IN LINKED LIST ");
}
else
{
save=first;
first=save->next;
//save->info=NULL;
save->next=avail;
avail=save;
printf("\n\t\t FIRST NODE DELETED ");
}
getch();
goto m;
case 6:
printf("\n\n\t\t<<YOU HAVE PRESSES 6 FOR DELETE LAST NODE .>>");
getch();
if(first==NULL)
{
printf("\n\t\t NO NODE IN LINKED LIST ");
}
else
{
if(first->next==NULL)
{
y=first->info;
first->next=avail;
first=avail;
first=NULL;
}
else
{
save=first;
while(save->next!=NULL)
{
pred=save;
save=save->next;
}
}
pred->next=NULL;
save->next=avail;
save=avail;
printf("\n\t\t LAST NODE DELETED ");
}
getch();
goto m;
case 7:
printf("\n\n\t\t<<YOU HAVE PRESSES 7 FOR DELETE AS OREDER.>>");
if(first==NULL)
{
printf("\n\t\t NO NODE IN LINKED LIST ");
}
else
{
printf("\n\t\t INSERT VALUE WHICH YOU WANT TO DELETE ");
scanf("%d",&x);
if(first->info==x)
{
first->next=avail;
first=avail;
first=NULL;
}
else
{
temp=first;
while(temp->next!=NULL&&temp->info!=x)
{
pred=temp;
temp=temp->next;
}
if(temp->info!=x)
{
printf("\n\t\t NODE NOT FOUND ");
}
else
{
pred->next=temp->next;
temp->next=avail;
temp=avail;
printf("\n\t\t NODE DELETED ");
}
}
}
getch();
goto m;
case 0:
printf("\n\n\t\t ");
printf("\n\t\t Do you want to exit? ");
printf("\n\t\t Press any key to Exit ");
printf("\n\t\t ");
getch();
exit(0);
}
getch();
}