C Program to Display the Nodes of a Linked List in Reverse without using Recursion

«
»
This C program, using iteration, displays a linked list in reverse. 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 Display the Nodes of a Linked List in Reverse without 
  3.  * using Recursion
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. struct node
  10. {
  11.     int visited;
  12.     int a;
  13.     struct node *next;
  14. };
  15.  
  16. void generate(struct node **);
  17. void display(struct node *);
  18. void linear(struct node *);
  19. void delete(struct node **);
  20.  
  21. int main()
  22. {
  23.     struct node *head = NULL;
  24.  
  25.     generate(&head);
  26.     printf("\nPrinting the list in linear order\n");
  27.     linear(head);
  28.     printf("\nPrinting the list in reverse order\n");
  29.     display(head);
  30.     delete(&head);
  31.  
  32.     return 0;
  33. }
  34.  
  35. void display(struct node *head)
  36. {
  37.     struct node *temp = head, *prev = head;
  38.  
  39.     while (temp->visited == 0)
  40.     {
  41.         while (temp->next != NULL && temp->next->visited == 0)
  42.         {
  43.             temp = temp->next;
  44.         }
  45.         printf("%d  ", temp->a);
  46.         temp->visited = 1;
  47.         temp = head;
  48.     }    
  49. }
  50.  
  51. void linear(struct node *head)
  52. {
  53.     while (head != NULL)
  54.     {
  55.         printf("%d  ", head->a);
  56.         head = head->next;
  57.     }
  58.     printf("\n");
  59. }
  60.  
  61. void generate(struct node **head)
  62. {
  63.     int num, i;
  64.     struct node *temp;
  65.  
  66.     printf("Enter length of list: ");
  67.     scanf("%d", &num);
  68.     for (i = num; i > 0; i--)
  69.     {
  70.         temp = (struct node *)malloc(sizeof(struct node));
  71.         temp->a = i;
  72.         temp->visited = 0;
  73.         if (*head == NULL)
  74.         {
  75.             *head = temp;
  76.             (*head)->next = NULL;
  77.         }
  78.         else
  79.         {
  80.             temp->next = *head;
  81.             *head = temp;
  82.         }
  83.     }
  84. }
  85.  
  86. void delete(struct node **head)
  87. {
  88.     struct node *temp;
  89.     while (*head != NULL)
  90.     {
  91.         temp = *head;
  92.         *head = (*head)->next;
  93.         free(temp);
  94.     }
  95. }

advertisement
$ gcc revnode_iter.c -o revnode_iter
$ a.out
Enter length of list: 5
 
Printing the list in linear order
1  2  3  4  5  
 
Printing the list in reverse order
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 Linked List, go to Linked List. If you wish to look at programming examples on all topics, 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