C Program to Reverse a Stack using Recursion

«
»
This C program, using recursion, 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 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 **, 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.     if (head != NULL)
  27.     {
  28.         stack_reverse(&head, &(head->next));
  29.     }
  30.     printf("\nThe contents in stack after reversal\n");
  31.     display(head);
  32.     delete(&head);
  33.  
  34.     return 0;
  35. }
  36.  
  37. void stack_reverse(struct node **head, struct node **head_next)
  38. {
  39.     struct node *temp;
  40.  
  41.     if (*head_next != NULL)
  42.     {
  43.          temp = (*head_next)->next;
  44.         (*head_next)->next = (*head);
  45.         *head = *head_next;
  46.         *head_next = temp;
  47.         stack_reverse(head, head_next);
  48.     }
  49. }
  50.  
  51. void display(struct node *head)
  52. {
  53.     if (head != NULL)
  54.     {
  55.         printf("%d  ", head->a);
  56.         display(head->next);
  57.     }
  58. }
  59.  
  60. void generate(struct node **head)
  61. {
  62.     int num, i;
  63.     struct node *temp;
  64.  
  65.     printf("Enter length of list: ");
  66.     scanf("%d", &num);
  67.     for (i = num; i > 0; i--)
  68.     {
  69.         temp = (struct node *)malloc(sizeof(struct node));
  70.         temp->a = i;
  71.         if (*head == NULL)
  72.         {
  73.             *head = temp;
  74.             (*head)->next = NULL;
  75.         }
  76.         else
  77.         {
  78.             temp->next = *head;
  79.             *head = temp;
  80.         }
  81.     }
  82. }
  83.  
  84. void delete(struct node **head)
  85. {
  86.     struct node *temp;
  87.     while (*head != NULL)
  88.     {
  89.         temp = *head;
  90.         *head = (*head)->next;
  91.         free(temp);
  92.     }
  93. }

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

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Reference 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.

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter