Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> सी प्रोग्रामिंग

स्टैक का उपयोग करके लिंक्ड सूची को उल्टा प्रिंट करें


एक लिंक्ड सूची दी गई है, प्रोग्राम को स्टैक डेटा संरचना का उपयोग करके सूची को अंत से सामने तक प्रिंट करना होगा

Input : 10 -> 5 -> 3 -> 1 -> 7 -> 9 Output: 9 -> 7 -> 1 -> 3 -> 5 -> 10

यहां उपयोगकर्ता स्टैक से तत्वों को पॉप करने के दृष्टिकोण का उपयोग कर सकता है, स्टैक[0] स्थान पर शीर्ष को इंगित करते हुए और फिर स्टैक[n] तत्व तक जा सकता है

एल्गोरिदम

START Step 1 -> create structure Linked_list     Declare int data     Declare struct linked_list *next End Step 2 -> declare int stack[30], top = -1 Step 3 -> declare struct linked_list* head = NULL Step 4 -> create function int printfromstack(int stack[])     Loop While top>=0         Print stack[--top] End Step 5 -> create function int push(struct linked_list** head, int n)     declare struct linked_list* newnode = (struct linked_list*)malloc(sizeof(struct linked_list))     set newnode->data = n     set newnode->next = (*head)     set (*head) = newnode step 6 -> create function int intostack(struct linked_list* head)     Loop While head!=NULL         Print head->data         Set stack[++top] = head->data         Set head = head->next     End End Step 7 -> goto main()     Call push(&head, 10)     Call push(&head, 20)     Call push(&head, 30)     Call push(&head, 40)     Call intostack(head)     Call printfromstack(stack) STOP

उदाहरण

#include  #include  struct linked_list {     int data;     struct linked_list *next; }; int stack[30], top = -1; struct linked_list* head = NULL; int printfromstack(int stack[]) {     printf("\nStack:\n");     while(top>=0) {         printf("%d ", stack[top--]);     } } int push(struct linked_list** head, int n) {     struct linked_list* newnode = (struct linked_list*)malloc(sizeof(struct linked_list));     newnode->data = n;     newnode->next = (*head);     (*head) = newnode; } int intostack(struct linked_list* head) {     printf("Linked list:\n");     while(head!=NULL) {         printf("%d ", head->data);         stack[++top] = head->data;         head = head->next;     } } int main(int argc, char const *argv[]) {     push(&head, 10);     push(&head, 20);     push(&head, 30);     push(&head, 40);     intostack(head);     printfromstack(stack);     return 0; }

आउटपुट

यदि हम उपरोक्त प्रोग्राम चलाते हैं तो यह निम्नलिखित आउटपुट उत्पन्न करेगा

Linked list: 40 30 20 10 Stack: 10 20 30 40

  1. C भाषा में लिंक्ड लिस्ट को वास्तव में रिवर्स किए बिना रिवर्स प्रिंट करना C भाषा में लिंक्ड लिस्ट को वास्तव में रिवर्स किए बिना रिवर्स प्रिंट करना

    कार्य दिए गए लिंक्ड लिस्ट के रिवर्स को रिकर्सिव फंक्शन का उपयोग करके प्रिंट करना है। प्रोग्राम को रिवर्स प्रिंट करना होगा लेकिन लिस्ट को रिवर्स नहीं करना होगा यानी नोड्स का क्रम समान रहेगा। यहां, प्रोग्राम पहले नोड का एड्रेस रखने वाले हेड पॉइंटर को अगले नोड पर तब तक ले जाएगा जब तक NULL की जांच नही

  1. C प्रोग्राम में अतिरिक्त स्पेस और संशोधन के बिना लिंक्ड लिस्ट का रिवर्स प्रिंट करें C प्रोग्राम में अतिरिक्त स्पेस और संशोधन के बिना लिंक्ड लिस्ट का रिवर्स प्रिंट करें

    समस्या विवरण कार्य लिंक्ड लिस्ट के अंत से शुरू होने वाले नोड्स को प्रिंट करना है बिना अतिरिक्त स्पेस का उपयोग किए, जिसका अर्थ है कि कोई अतिरिक्त वेरिएबल नहीं होना चाहिए, इसके बजाय हेड पॉइंटर जो पहले नोड को इंगित करता है, को स्थानांतरित किया जाएगा। उदाहरण इनपुट: 10 21 33 42 89 आउटपुट: 89 42 33 21 1

  1. C++ में पुनरावृत्ति का उपयोग करके लिंक्ड लिस्ट के वैकल्पिक नोड्स प्रिंट करें C++ में पुनरावृत्ति का उपयोग करके लिंक्ड लिस्ट के वैकल्पिक नोड्स प्रिंट करें

    लिंक्ड लिस्ट एक रैखिक डेटा संरचना है जो तत्वों को गैर-सन्निहित मेमोरी स्थानों में संग्रहीत करती है। प्रत्येक तत्व में लिंक्ड लिस्ट के अगले तत्व का एक पॉइंटर होता है। उदाहरण - इस समस्या में, हमें एक लिंक्ड लिस्ट दी गई है और हमें इस लिंक्ड लिस्ट के तत्वों को प्रिंट करना है लेकिन केवल वैकल्पिक तत्वों