Users Online

· Guests Online: 101

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

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

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. }

 

$ 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

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 1.12 seconds
10,835,352 unique visits