C++ program to print reverse of a linked list without actually reversing

C++ program to print reverse of a linked list without acually reversibg | PrepInsta

How to print reverse of a linked list without actually reversing in CPP programming?

In this article, we will learn to write a C++ program to print reverse of a linked list without actually reversing. For doing this we will create a function that will recursively print data from last node till the beginning node without actually reversing it. Further in this article we will see steps and algorithm for the same.

Steps write a C++ program to print reverse of a linked list without actually reversing

These steps are used to print reverse of a linked list without actually reversing in CPP programming

  1. Start the program and initialize the variables.
  2. Create function to create list (listBanao).
  3. The above created function will help us to create linked list.
  4. Now create another function toto print reverse of a linked list without actually reversing (reverse).
  5. This function will have our main algorithm to reverse the given linked list.
  6. This function will help to display the linked list in reverse order without actually reversing it.

How to struct node of a linked list?

Syntax

class Node  

    int data; 
    Node *next; 
}; 
C++ program to print reverse of a linked list without actually reversing

Algorithm to print reverse of a linked list without actually reversing in C++

Below is the algorithm used to print reverse of a linked list without actually reversing in C++

  • VOID REVERSE(NODE *STNODE)
  • IF (STNODE == NULL)
  • RETURN
  • REVERSE(STNODE->NEXTPTR)
  • PRINT STNODE->NUM

 

Program to print reverse of a linked list without actually reversing in C++

#include <iostream>
using namespace std;

struct node 
{
    int num;                
    node *nextptr;             
}*stnode; //node constructed

void listBanao(int n);                 
void reverse(node *stnode);
 
int main()
{
    int n,num,item;
		
    cout<<"Enter the number of nodes: ";
    cin>>n;
    listBanao(n);
    cout<<"After reversing\n";
    reverse(stnode);

    return 0;
}
void listBanao(int n) //function to create linked list.
{
    struct node *frntNode, *tmp;
    int num, i;
 
    stnode = (struct node *)malloc(sizeof(struct node));
    if(stnode == NULL)        
    {
        cout<<"Memory can not be allocated";
    }
    else
    {
                                  
        cout<<"Enter the data for node 1: ";
        cin>>num;
        stnode-> num = num;      
        stnode-> nextptr = NULL; //Links the address field to NULL
        tmp = stnode;
 
        for(i=2; i<=n; i++)
        {
            frntNode = (struct node *)malloc(sizeof(struct node)); 
 

            if(frntNode == NULL) //If frntnode is null no memory cannot be allotted
            {
                cout<<"Memory can not be allocated";
                break;
            }
            else
            {
                cout<<"Enter the data for node "<<i<<": "; // Entering data in nodes.
                cin>>num;
                frntNode->num = num;         
                frntNode->nextptr = NULL;    
                tmp->nextptr = frntNode;     
                tmp = tmp->nextptr;
            }
        }
    }
} 


void reverse(node *stnode) //function to reverse linked list
{
   if (stnode == NULL)
      return;
   reverse(stnode->nextptr);
  
   cout<<stnode->num<<"\t";
}
Output:
Enter the number of nodes: 5
Enter the data for node 1: 14
Enter the data for node 2: 15
Enter the data for node 3: 16
Enter the data for node 4: 17
Enter the data for node 5: 18
After reversing
18	17	16	15	14	

Want to learn more Data Structure?