लिंक्ड लिस्ट डायनामिक मेमोरी आवंटन का उपयोग करती हैं यानी वे तदनुसार बढ़ती और सिकुड़ती हैं। उन्हें नोड्स के संग्रह के रूप में परिभाषित किया जाता है। यहां, नोड्स के दो भाग होते हैं: डेटा और लिंक। डेटा, लिंक और लिंक्ड लिस्ट का प्रतिनिधित्व नीचे दिया गया है।
लिंक्ड लिस्ट पर ऑपरेशन्स
C भाषा में लिंक्ड लिस्ट पर तीन प्रकार के ऑपरेशन्स होते हैं:
- इन्सर्शन
- डिलीशन
- ट्रैवर्सिंग
डिलीशन
नीचे दिए गए उदाहरण पर विचार करें:
नोड 2 डिलीट करें
नोड 1 डिलीट करें
नोड 3 डिलीट करें
प्रोग्राम
लिंक्ड लिस्ट में एलिमेंट्स के डिलीशन के लिए C प्रोग्राम:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void push(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void deleteNode(struct Node **head_ref, int position) {
// यदि लिस्ट खाली है
if (*head_ref == NULL)
return;
struct Node* temp = *head_ref;
if (position == 0) {
*head_ref = temp->next;
free(temp);
return;
}
for (int i = 0; temp != NULL && i < position - 1; i++)
temp = temp->next;
if (temp == NULL || temp->next == NULL)
return;
struct Node *next = temp->next->next;
free(temp->next); // मेमोरी फ्री करें
temp->next = next;
}
void printList(struct Node *node) {
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
}
int main() {
struct Node* head = NULL;
push(&head, 7);
push(&head, 1);
push(&head, 3);
push(&head, 2);
push(&head, 8);
puts("बनाई गई लिस्ट: ");
printList(head);
deleteNode(&head, 3);
puts("\n पोजीशन 3 पर डिलीशन के बाद लिस्ट: ");
printList(head);
return 0;
}
आउटपुट
बनाई गई लिस्ट: 8 2 3 1 7 पोजीशन 3 पर डिलीशन के बाद लिस्ट: 8 2 3 7