#include
#include
#include
#include
struct node
{
int info;
struct node *link;
};
typedef struct node NODE;
NODE *head;
/*Fucntion to create a linked lsit*/
NODE *GetNode()
{
NODE *NewNode;
NewNode=(NODE*)(malloc(sizeof(NODE)));
if(NewNode==NULL)
{
printf("\n\nEMPTY LIST");
exit(1);
}
return NewNode;
}
void create_link_list(NODE *CurrPtr)
{
NODE *NewNode;
int ans;
while(1)
{
printf("\n\nENTER INFO :");
scanf("%d",&CurrPtr->info);
printf("\n\nDO U WISH TO ENTER ONE MORE NODE PRESS 1 :");
scanf("%d",&ans);
if(ans==1)
{
NewNode=(NODE*)(malloc(sizeof(NODE)));
CurrPtr->link=NewNode;
CurrPtr=NewNode;
}
else
{
CurrPtr->link=NULL;
break;
}
}
}
void display(NODE *CurrPtr)
{
CurrPtr=head;
while(CurrPtr!=NULL)
{
printf("\n\n\t%d ",CurrPtr->info);
CurrPtr=CurrPtr->link;
}
}
int Delete_Beg()
{
NODE *CurrPtr;
int item;
if(head==NULL)
{
printf("\n\n DELETION NOT POSSIBLE");
exit(0);
}
CurrPtr=head;
item=CurrPtr->info;
head=CurrPtr->link;
free(CurrPtr);
return(item);
}
int Delete_Middle(int pos)
{
NODE *CurrPtr,*PrevPtr;
int item,i;
if(head==NULL)
{
printf("\n\nDELETION NOT POSSIBLE");
exit(0);
}
CurrPtr=head;
for(i=1;i
PrevPtr=CurrPtr;
CurrPtr=CurrPtr->link;
}
if(CurrPtr==NULL)
{
printf("\n\n Position out of range");
exit(0);
}
item=CurrPtr->info;
PrevPtr->link=CurrPtr->link;
free(CurrPtr);
return(item);
}
void Delete_End()
{
NODE *CurrPtr,*PrevPtr;
int item;
CurrPtr=head;
while(CurrPtr->link!=NULL)
{
PrevPtr=CurrPtr;
CurrPtr=CurrPtr->link;
}
PrevPtr->link=NULL;
free(CurrPtr);
}
void main()
{
int item,ch,pos;
NODE *CurrPtr,*PrevPtr,*loc;
clrscr();
head=(NODE*)malloc(sizeof(NODE));
create_link_list(head);
while(1)
{
printf("\n 1,DISPALY");
printf("\n 2. DELET FROM BEGIINING OF THE LINKED LIST \n");
printf("\n 3. DELETE MIDDLE/AT GIVEN POSITION OF THE LINKED LIST \n");
printf("\n 4. DELETE AT THE END OF THE LINKED LIST \n");
printf("\n 5. EXIT FORM MENU");
printf("\n\nENTER CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1: if(head==NULL)
printf("\n\nUNDERFLOW !!!!");
else
{
printf("\n \nLINKED LIST ELEMENTS ARE :");
display(head);
}
break;
case 2: item=Delete_Beg();
printf("\n\n%d DELETED FROM THE LINKED LIST: ");
break;
case 3: printf("\n\nENTER POSITION TO BE INSERT : ");
scanf("%d",&pos);
item=Delete_Middle(pos);
printf("%d IS DELTED ITEM AT END :",item);
break;
case 4: Delete_End();
printf("IS DELTED ITEM AT END :");
break;
case 5: exit(0);
break;
default : printf("\n\nINVALID CHOICE !!!!!!!!");
break;
}
}
getch();
}
No comments:
Post a Comment