INSERTING New NODE Algorithm Implementation of the LINKED LIST in Hindi – पिछले Section तक हमने जितने भी Algorithms का प्रयोग करके Functions Create किए हैं, उन सभी को Use करते हुए हम निम्नानुसार एक Main() Function Create करके उसमें इन सभी Algorithm Functions को Utilize कर सकते हैं-
[code] Program #include <stdio.h> #include <conio.h> #include <alloc.h> #include <stdlib.h> static int total=0; //Structure struct LIST { //Information Part int INFO; //Link Part struct LIST *LINK; }; struct LIST *START = NULL; //Function int COUNT(struct LIST **PTR) { int i = 1; struct LIST *temp; temp = *PTR; while(temp->LINK != NULL) { temp = temp->LINK; i++; } return i; } //Function void INSERT_AT_BEGNNING(struct LIST **PTR, int ITEM) { struct LIST *NEWNODE; NEWNODE = (struct LIST *)malloc(sizeof(struct LIST)); NEWNODE->INFO = ITEM; NEWNODE->LINK = *PTR; *PTR = NEWNODE; total = COUNT(&START); } //Function void INSERT_AT_MIDDLE(struct LIST **PTR, int ITEM, int LOCATION) { struct LIST *NEWNODE, *temp; int i; temp = *PTR; for(i=0; i<LOCATION; i++) { temp = temp->LINK; if(temp == NULL) { printf("\nData Items are Less then LOCATION\n"); return; } } NEWNODE = (struct LIST *)malloc(sizeof(struct LIST)); NEWNODE->INFO = ITEM; NEWNODE->LINK = temp->LINK; temp->LINK = NEWNODE; total = COUNT(&START); } //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; } total = COUNT(&START); } //Function void DISPLAY(struct LIST **PTR) { struct LIST *temp; temp = *PTR; while(temp != NULL) { printf("\n Inserted Number \t %d", temp->INFO); temp = temp->LINK; } } //main() Function main() { char choice; int ITEM, LOCATION; clrscr(); while(1) { printf("\n1. Enter Number at the BEGINNING of the LIST"); printf("\n2. Enter Number after the Specified LOCATION of the LIST"); printf("\n3. Enter Number at the END of the LIST"); printf("\n4. Display Data Items"); printf("\n5. Total Data Items"); printf("\n6. EXIT"); printf("\n\nEnter Your CHOICE"); scanf("%d", &choice); switch(choice) { case 1: printf("\n Enter a NUMBER at the BEGINNING of the LIST : " ); scanf("%d", &ITEM); INSERT_AT_BEGNNING(&START, ITEM); break; case 2: printf("\n Enter a NUMBER at the MIDDLE of the LIST : " ); scanf("%d", &ITEM); printf("\nEnter LOCATION Number : "); scanf("%d", &LOCATION); INSERT_AT_MIDDLE(&START, ITEM, LOCATION); break; case 3: printf("\n Enter a NUMBER at the END of the LIST : " ); scanf("%d", &ITEM); INSERT_AT_END(&START, ITEM); break; case 4: DISPLAY(&START); break; case 5: printf("\nTotal Number of Data Items are %d ", total); break; case 6: exit(1); } } } [/code]
ये Article इस वेबसाईट पर Selling हेतु उपलब्ध EBook Data Structure and Algorithms in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी।
Data Structure and Algorithms in Hindi | Page: 433 | Format: PDF