INSERTING New NODE at the End of the LINKED LIST – जब हम पहली बार कोई Linked List Create करते हैं तब वास्तव में हम START में एक नए Node का Address दे रहे होते हैं यानी यदि ये माना जाए कि START किसी पहले से बनी हुई Linked List को Point कर रहा है तो Create होने वाला नया Node इस Start के अन्त में जुडता है। इस स्थिति में हम CREATE() व INSERT_AT_END() दोनों Functions को निम्नानुसार एक ही Function के रूप में लिख सकते हैं-
[code]
void INSERT_AT_END(struct LIST **PTR, int ITEM)
{
struct LIST *temp, *NEWNODE;
if(*PTR == NULL) //[IF START = NULL ] then Create new NODE
{
temp = (struct LIST *)malloc(sizeof(struct LIST));
temp->INFO = ITEM;
temp->LINK = NULL;
*PTR = temp;
}
else
{
struct LIST *temp = *PTR;
//Go to End of the Linked List
while(temp->LINK != NULL)
temp = temp->LINK;
//Create New Node
NEWNODE = (struct LIST *)malloc(sizeof(struct LIST));
NEWNODE->INFO = ITEM;
NEWNODE->LINK = NULL;
temp->LINK = NEWNODE;
}
}
[/code]
इस Function का प्रयोग करते हुए हम निम्नानुसार एक Program बना सकते हैं-
[code]
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
// Structure
struct LIST
{
//Information Part
int INFO;
//Link Part
struct LIST *LINK;
};
struct LIST *START = NULL;
// Function
void INSERT_AT_END(struct LIST **PTR, int ITEM)
{
struct LIST *temp, *NEWNODE;
if(*PTR == NULL) //[IF START = NULL ] then Create new NODE
{
temp = (struct LIST *)malloc(sizeof(struct LIST));
temp->INFO = ITEM;
temp->LINK = NULL;
*PTR = temp;
}
else
{
struct LIST *temp = *PTR;
//Go to End of the Linked List
while(temp->LINK != NULL)
temp = temp->LINK;
//Create New Node
NEWNODE = (struct LIST *)malloc(sizeof(struct LIST));
NEWNODE->INFO = ITEM;
NEWNODE->LINK = NULL;
temp->LINK = NEWNODE;
}
}
//main() Function
main()
{
struct LIST *ptr;
char choice= 'y';
int ITEM;
clrscr();
while(choice=='y' || choice=='Y')
{
printf("\n Enter a NUMBER in Information Part : " );
scanf("%d", &ITEM)
INSERT_AT_END(&START, ITEM);
printf("\n Do You wish to Continuous ? Y/N ");
fflush(stdin);
choice = (char)getchar();
}
ptr = START;
while(ptr)
{
printf("\n Inserted Number \t %d", ptr->INFO);
ptr = ptr->LINK;
}
free(ptr);
free(START);
getch();
}
//Output
Enter a NUMBER in Information Part : 123123
Do You wish to Continuous ? Y/N y
Enter a NUMBER in Information Part : 787878
Do You wish to Continuous ? Y/N y
Enter a NUMBER in Information Part : 989898
Do You wish to Continuous ? Y/N y
Enter a NUMBER in Information Part : 784112
Do You wish to Continuous ? Y/N n
Inserted Number 123123
Inserted Number 787878
Inserted Number 989898
Inserted Number 784112
[/code]
ये Article इस वेबसाईट पर Selling हेतु उपलब्ध EBook Data Structure and Algorithms in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी।
Data Structure and Algorithms in Hindi | Page: 433 | Format: PDF