Q:

C Program to Print linked list in reverse order using recursion

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Recursion Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program to Display Nodes of Linked List using Re... >>
<< C Program to implement Matrix Multiplication using...