DATA STRUCTURES

DATA STRUCTURES
JAYASHREE ORACLE CERTIFIED JAIN UNIVERSITY

Tuesday, March 8, 2011

//PART-B PRG 1. INSERT NODE TO SINGLY LINKED LIST AT DIFFERENT POSITION
#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%d : %d",CurrPtr->info,&CurrPtr->info);
CurrPtr=CurrPtr->link;
}
}
NODE *Insert_Beg(NODE *HeadPtr,int item)
{
NODE *NewNode;
NewNode=GetNode();
NewNode->info=item;
NewNode->link=HeadPtr;
return NewNode;
}
void Insert_Middle(int item,int pos)
{
NODE *NewNode,*CurrPtr;
int i;
NewNode=(NODE*)malloc(sizeof(NODE));
NewNode->info=item;
CurrPtr=head;
for(i=0;ilink;i++)
CurrPtr=CurrPtr->link;
if(CurrPtr==NULL)
{
printf("\n\nPOSITION OUT OF RANGE");
exit(0);
}
NewNode->link=CurrPtr->link;
CurrPtr->link=NewNode;
}
void Insert_End(NODE *CurrPtr,int item)
{
NODE *NewNode;
NewNode=GetNode();
NewNode->info=item;
NewNode->link=NULL;
while(CurrPtr->link!=NULL)
{
CurrPtr=CurrPtr->link;
}
CurrPtr->link=NewNode;
NewNode->link=NULL;

}
void main()
{
int item,ch,pos;
clrscr();
head=(NODE*)malloc(sizeof(NODE));
while(1)
{
printf("\n 1. CREATE OR INSERT NODE \n");
printf("\n 2. DISPALY NODE\n ");
printf("\n 3. INSERT BEGINING OF THE LINKED LIST \n");
printf("\n 4. INSERT MIDDLE/AT GIVEN POSITION OF THE LINKED LIST \n");
printf("\n 5. INSERT AT THE END OF THE LINKED LIST \n");
printf("\n 6.EXIT FORM MENU");

printf("\n\nENTER CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1: create_link_list(head);
break;
case 2: printf("\n \nLINKED LIST ELEMENTS ARE :");
display(head);
break;
case 3: printf("\n\n ENTER ITEM TO BE INSERT :");
scanf("%d",&item);
head=Insert_Beg(head,item);
printf("\n\nLINKED LIST ELEMENTS ARE : ");
break;
case 4: printf("\n\nENTER ITEM TO BE INSERT : ");
scanf("%d",&item);
printf("\n\nENTER POSITION TO BE INSERT : ");
scanf("%d",&pos);
Insert_Middle(item,pos);
break;
case 5: printf("ENTER ITEM TO BE INSERT AT END :");
scanf("%d",&item);
Insert_End(head,item);
break;
case 6: exit(0);
break;
default : printf("\n\nINVALID CHOICE !!!!!!!!");
break;
}
}
getch();
}




No comments:

Post a Comment