C Program to Print linked list in reverse order using recursion

By | November 29, 2016

Print linked list in reverse order


Write a C Program to Print linked list in reverse order using recursion. Here’s simple Program to Print linked list in reverse order using recursion in C Programming Language.


Recursion : :


  • Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
  • The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
  • Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.

Problem : :


This C Program uses recursive function & reverses the nodes in a linked list and displays the list.

A linked list is an ordered set of data elements, each containing a link to its successor. This program makes each data element to link to its predecessor.

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


SOURCE CODE : :


/*  C Program to Print linked list in reverse order using recursion  */

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

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

void insert_new_node(struct node ** head_ref, int new_data);
void print_reverse_recursive(struct node *);
void print(struct node *);
void create_new_node(struct node *, int );

//Driver Function
int main ()
{
    struct node *head = NULL;
    insert_new_node(&head, 11);
    insert_new_node(&head, 22);
    insert_new_node(&head, 33);
    insert_new_node(&head, 44);
    printf("Entered Linked List is :: ");
    print(head);
    printf("\n\nLinked List in reverse order :: ");
    print_reverse_recursive(head);
    printf("\n");
    return 0;
}

//Recursive Reverse
void print_reverse_recursive(struct node *head)
{
    if (head == NULL)
    {
        return;
    }

    //Recursive call first
    print_reverse_recursive(head -> next);
    //Print later
    printf("%d ", head -> data);
}

//Print the linkedlist normal
void print(struct node *head)
{
    if (head == NULL)
    {
        return;
    }
    printf("%d ", head -> data);
    print(head -> next);
}

//New data added in the start
void insert_new_node(struct node ** head_ref, int new_data)
{
    struct node * new_node = (struct node *) malloc (sizeof(struct node));
    new_node -> data = new_data;
    new_node -> next = (*head_ref);
    (*head_ref) = new_node;
}

Output : :


/*  C Program to Print linked list in reverse order using recursion  */

Entered Linked List is :: 44 33 22 11

Linked List in reverse order :: 11 22 33 44

Process returned 0

Above is the source code for C Program to Print linked list in reverse order using recursion which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

Leave a Reply