INSERTING New NODE at the BEGINNING of the LIST– जब कोई Linked List पहले से ही Created हो और उसकी शुरूआत में हमें नया Node Add करना हो] यानी START में नए Create किए गए Node का Address देना हो, तब सबसे पहले हमें एक Node Create करना होता है। फिर START में जिस Node का Address होता है वह Address Newly Created Node के LINK में देना होता है और START में Newly Created Node का Address देना होता है। ये काम करने के लिए हम निम्न Algorithm का प्रयोग कर सकते हैं-
[code]
Algorithm
Procedure: ADD_AT_BEGNNING(LIST, ITEM)
- SET PTR = START
- CREATE NEWNODE
- NEWNODE[INFO] = ITEM
- NEWNODE[LINK] = PTR
- SET START = NEWNODE
- EXIT
[/code]
इस Algorithm मे LIST एक Linked List है जिसमें पहले से एक या एक से अधिक Data Items Inserted हैं। NEWNODE Create होने वाला नया Node है। START में पहले से बनी हुई Linked List का Address है। इस Algorithm का प्रयोग करके हम निम्नानुसार एक Function Create कर सकते हैं-
[code]
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;
}
[/code]
इस Algorithm का प्रयोग हम निम्नानुसार main() Function में कर सकते हैं-
[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
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++;
}
//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++;
}
//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;
clrscr();
while(1)
{
printf("\n1. Enter Number at the BEGNNING of the LIST");
printf("\n2. Enter Number at the END of the LIST");
printf("\n3. Display Data Items");
printf("\n4. Total Data Items");
printf("\n5. EXIT");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\n Enter a NUMBER at the BEGINNIN of the LIST : " );
scanf("%d", &ITEM);
INSERT_AT_BEGNNING(&START, ITEM);
break;
case 2:
printf("\n Enter a NUMBER at the END of the LIST : " );
scanf("%d", &ITEM);
INSERT_AT_END(&START, ITEM);
break;
case 3:
DISPLAY(&START);
break;
case 4:
printf("\nTotal Number of Data Items are %d ", total);
break;
case 5:
exit(1);
}
}
}
[/code]
ये Article इस वेबसाईट पर Selling हेतु उपलब्ध EBook Data Structure and Algorithms in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी।
Data Structure and Algorithms in Hindi | Page: 433 | Format: PDF