इस ट्यूटोरियल में, हम एक प्रोग्राम पर चर्चा करेंगे जो सिंगली लिंक्ड लिस्ट को सर्कुलर लिंक्ड लिस्ट में परिवर्तित करता है।
इसके लिए हमें एक सिंगली लिंक्ड लिस्ट दी जाएगी। हमारा कार्य उस लिस्ट के तत्वों को लेकर उन्हें सर्कुलर लिंक्ड लिस्ट में बदलना है।
उदाहरण
#include <bits/stdc++.h> // लिंक्ड लिस्ट की नोड संरचना struct Node { int data; struct Node* next; }; // सिंगली लिंक्ड लिस्ट को // सर्कुलर लिंक्ड लिस्ट में बदलना struct Node* circular(struct Node* head){ struct Node* start = head; while (head->next != NULL) head = head->next; // यदि head->next NULL की ओर इशारा करता है तो start को head->next नोड असाइन करना head->next = start; return start; } void push(struct Node** head, int data){ // नए नोड का निर्माण struct Node* newNode = (struct Node*)malloc (sizeof(struct Node)); // नए नोड में डेटा डालना newNode->data = data; newNode->next = (*head); (*head) = newNode; } // सर्कुलर लिंक्ड लिस्ट के तत्वों को प्रदर्शित करना void print_list(struct Node* node){ struct Node* start = node; while (node->next != start) { printf("%d ", node->data); node = node->next; } printf("%d ", node->data); } int main(){ struct Node* head = NULL; push(&head, 15); push(&head, 14); push(&head, 13); push(&head, 22); push(&head, 17); circular(head); printf("Display list: \n"); print_list(head); return 0; }आउटपुट
Display list: 17 22 13 14 15