C Program to Reverse a Stack without Recursion

This C program, using iteration, reverses a stack content. Stack here is represented using a linked list. A linked list is an ordered set of data elements, each containing a link to its successor.

Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Reverse a Stack without using Recursion
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct node
  8. {
  9.     int a;
  10.     struct node *next;
  11. };
  12.  
  13. void generate(struct node **);
  14. void display(struct node *);
  15. void stack_reverse(struct node **);
  16. void delete(struct node **);
  17.  
  18. int main()
  19. {
  20.     struct node *head = NULL;
  21.  
  22.     generate(&head);
  23.     printf("\nThe sequence of contents in stack\n");
  24.     display(head);
  25.     printf("\nInversing the contents of the stack\n");
  26.     stack_reverse(&head);
  27.     printf("\nThe contents in stack after reversal\n");
  28.     display(head);
  29.     delete(&head);
  30.     return 0;
  31. }
  32.  
  33. void stack_reverse(struct node **head)
  34. {
  35.     struct node *temp, *prev;
  36.  
  37.     if (*head == NULL)
  38.     {
  39.         printf("Stack does not exist\n");
  40.     }
  41.     else if ((*head)->next == NULL)
  42.     {
  43.         printf("Single node stack reversal brings no difference\n");
  44.     }
  45.     else if ((*head)->next->next == NULL)
  46.     {
  47.         (*head)->next->next = *head;
  48.         *head = (*head)->next;
  49.         (*head)->next->next = NULL;
  50.     }
  51.     else
  52.     {
  53.         prev = *head;
  54.         temp = (*head)->next;
  55.         *head = (*head)->next->next;
  56.         prev->next = NULL;
  57.         while ((*head)->next != NULL)
  58.         {
  59.             temp->next = prev;
  60.             prev = temp;
  61.             temp = *head;
  62.             *head = (*head)->next;
  63.         }
  64.         temp->next = prev;
  65.         (*head)->next = temp;
  66.     }
  67. }
  68.  
  69. void display(struct node *head)
  70. {
  71.     if (head != NULL)
  72.     {
  73.         printf("%d  ", head->a);
  74.         display(head->next);
  75.     }
  76. }
  77.  
  78. void generate(struct node **head)
  79. {
  80.     int num, i;
  81.     struct node *temp;
  82.  
  83.     printf("Enter length of list: ");
  84.     scanf("%d", &num);
  85.     for (i = num; i > 0; i--)
  86.     {
  87.         temp = (struct node *)malloc(sizeof(struct node));
  88.         temp->a = i;
  89.         if (*head == NULL)
  90.         {
  91.             *head = temp;
  92.             (*head)->next = NULL;
  93.         }
  94.         else
  95.         {
  96.             temp->next = *head;
  97.             *head = temp;
  98.         }
  99.     }
  100. }
  101.  
  102. void delete(struct node **head)
  103. {
  104.     struct node *temp;
  105.     while (*head != NULL)
  106.     {
  107.         temp = *head;
  108.         *head = (*head)->next;
  109.         free(temp);
  110.     }
  111. }

$ cc revstack_iter.c -o revstack_iter
$ a.out
Enter length of list: 8
 
The sequence of contents in stack
1  2  3  4  5  6  7  8  
Inversing the contents of the stack
 
The contents in stack after reversal
8  7  6  5  4  3  2  1

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Stacks & Queues, go to C Programming Examples on Stacks & Queues. If you wish to look at programming examples on all topics of C, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.