लिंक्ड लिस्ट डायनामिक मेमोरी एलोकेशन का उपयोग करती हैं यानी वे तदनुसार बढ़ती और सिकुड़ती हैं। यह नोड्स का संग्रह है।
नोड के दो भाग होते हैं:
- डेटा
- लिंक
लिंक्ड लिस्ट के प्रकार
C प्रोग्रामिंग भाषा में लिंक्ड लिस्ट के प्रकार निम्नानुसार हैं:
- सिंगल / सिंगली लिंक्ड लिस्ट
- डबल / डबली लिंक्ड लिस्ट
- सर्कुलर सिंगल लिंक्ड लिस्ट
- सर्कुलर डबल लिंक्ड लिस्ट
एल्गोरिथम
डायनामिक लिंक्ड लिस्ट का उपयोग करके कार की जानकारी स्टोर करने के लिए नीचे दिया गया एल्गोरिथम देखें।
चरण 1 - स्ट्रक्चर वेरिएबल्स डिक्लेयर करें।
चरण 2 - डिस्प्ले के लिए फंक्शन डेफिनिशन डिक्लेयर करें।
चरण 3 - वेरिएबल को डायनामिक मेमोरी एलोकेशन आवंटित करें।
चरण 4 - कार की जानकारी दर्ज करने के लिए do-while लूप का उपयोग करें।
चरण 5 - डिस्प्ले फंक्शन कॉल करें चरण 2 पर जाएं।
उदाहरण
निम्नलिखित C प्रोग्राम डायनामिक लिंक्ड लिस्ट का उपयोग करके कार की जानकारी स्टोर करता है:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node{
char model[10], color[10];
int year;
struct node *next;
};
struct node *temp, *head;
void display(struct node *head){
temp = head;
while(temp != NULL){
if(temp->year > 2010 && (strcmp("yellow", temp->color) == 0))
printf(" %s \t\t %s \t\t %d", temp->model, temp->color, temp->year);
temp = temp->next;
printf("\n");
}
}
int main(){
int n;
char option, enter;
head = (struct node *)malloc(sizeof(struct node));
temp = head;
do{
printf("\nenter car model: ");
scanf("%s", temp->model);
printf("enter car color: ");
scanf("%s", temp->color);
printf("enter car year: ");
scanf("%d", &temp->year);
printf("\nDo you want continue Y(es) | N(o) : ");
scanf("%c", &enter);
scanf("%c", &option);
if(option != 'N'){
temp->next = (struct node *)malloc(sizeof(struct node));
temp = temp->next;
} else {
temp->next = NULL;
}
}while(option != 'N');
display(head);
return 0;
}
आउटपुट
enter car model: I20 enter car color: white enter car year: 2016 Do you want continue Y(es) | N(o) : Y enter car model: verna enter car color: red enter car year: 2018 Do you want continue Y(es) | N(o) : Y enter car model: creta enter car color: Maroon enter car year: 2010 Do you want continue Y(es) | N(o) : N