C program to display a Linked List in Reverse

Display a linked list in reverse: In this tutorial, we will learn how to implement a C program to display the linked list in reverse using recursion. By Radib Kar Last updated : August 01, 2023

Problem statement

Write a program to display the linked list in reverse order. Note that the original linked list will not change.

Solution

  1. Create and build the linked list
  2. Display the original linked list
  3. Displaying in reverse order can be done using recursive function
    Function reverse_display(node) 
        IF (node!= NULL) 
    	    reverse_display(node->next)
    	    display node value
        END IF

Example with Explanation

Let's check how the program runs...

Let the input linked list to be: 1->2->3->4->NULL with head at 1

In Main() it calls
reverse_display(1)
----------------------------------------------------------------

reverse_display(1):
node is not null
reverse_display(1->next) thus it calls reverse_display(2)
----------------------------------------------------------------

reverse_display(2):
node is not null
reverse_display(2->next) thus it calls reverse_display(3)
----------------------------------------------------------------

reverse_display(3):
node is not null
reverse_display(3->next) thus it calls reverse_display(4)
----------------------------------------------------------------

reverse_display(4):
node is not null
reverse_display(4->next) thus it calls reverse_display(NULL)
----------------------------------------------------------------

reverse_display(NULL):
node is null
no further call, control returned to reverse_display(4)
----------------------------------------------------------------

At reverse_display(4)
Control returned from reverse_display(4->next)
So it prints the node value that is 4
Control returns to reverse_display(3)
----------------------------------------------------------------

At reverse_display(3)
Control returned from reverse_display(3->next)
So it prints the node value that is 3
Control returns to reverse_display(2)
----------------------------------------------------------------

At reverse_display(2)
Control returned from reverse_display(2->next)
So it prints the node value that is 2
Control returns to reverse_display(1)
----------------------------------------------------------------

At reverse_display(1)
Control returned from reverse_display(1->next)
So it prints the node value that is 1
Control returns to Main function

Thus it displays 4 3 2 1

C program to display a Linked List in Reverse

#include <stdio.h>
#include <stdlib.h>

struct node {
    int data; // data field
    struct node* next;
};

void display(struct node* head)
{
    struct node* current = head; // current node set to head

    printf("traversing the list...\n");
    while (current != NULL) { //traverse until current node isn't NULL
        printf("%d ", current->data);
        current = current->next; // go to next node
    }
}

void reverse_display(struct node* head)
{
    if (head) {
        //recursive call to display in reverse order
        reverse_display(head->next);
        printf("%d ", head->data);
    }
}

struct node* creatnode(int d)
{
    struct node* temp = malloc(sizeof(struct node));
    temp->data = d;
    temp->next = NULL;
    return temp;
}

int main()
{
    printf("creating the linked list by inserting new nodes at the end\n");

    printf("enter 0 to stop building the list, else enter any integer\n");

    int k, count = 1, x;

    struct node *curr, *temp;

    scanf("%d", &k);
    struct node* head = creatnode(k); //buliding list, first node
    scanf("%d", &k);
    temp = head;

    ///////////////////inserting at the end//////////////////////
    while (k) {
        curr = creatnode(k);
        temp->next = curr; //appending each node
        temp = temp->next;
        scanf("%d", &k);
    }

    display(head); // displaying the list
    printf("\ndisplaying in reverse order...\n");
    reverse_display(head); //display in reverse order

    return 0;
}

Output

First run:
creating the linked list by inserting new nodes at the end
enter 0 to stop building the list, else enter any integer
1 2 3 4 0
traversing the list...
1 2 3 4
displaying in reverse order...
4 3 2 1

Second run:
creating the linked list by inserting new nodes at the end
enter 0 to stop building the list, else enter any integer
34 55 2 4 76 -8 6 0
traversing the list...
34 55 2 4 76 -8 6
displaying in reverse order...
6 -8 76 4 2 55 34


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.