Problem Question
To implement Reversal of a Linked List using Recursion. This paradigm makes use of processor stack, instead of using a dedicated loop to do the job.
Explanation of Problem
In this program we would be implementing a Linked List. Make sure you have a strong understanding of pointers to understand Linked Lists. A linked list is a basic data structure that is used in dynamic memory allocation applications. It comprises of 'nodes' which are linked together to form a sequence of nodes called Linked List. The linkage is done using memory addresses of adjacent nodes (next node in singly linked list, and both next & previous node in doubly linked list).
In this program we use a struct to implement the node of our linked list. We will implement addition function to get some data in linked list before we can perform desired operations on it. Adding a new node to the list means, creating a new node structure, allocating memory to it and linking it to the list.
Code
#include<iostream>
/**@Title: LinkedList v1.7.cpp*
*@Programming Paradigm: Procedural*
*@Language: C++*
*@Compiler: GNU GCC*
*@IDE: Code::Blocks 13.12*
*@Author: Rogue Coder*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: 27-12-2014*
*/
struct node
{
  int data;
  node* next;
};
void addAtLast(node** rootNode);
node* reverseList(node* rootNode);
void displayList(node* rootNode);
int main()
{
  int choice;
  node *startList = NULL;
  std::cout << "Welcome to LinkedList v1.5" << std::endl << "Made by Rogue Coder" << std::endl;
  do
  {
    std::cout << std::endl << "1 : Add a Node to the list" <<
         std::endl << "2 : Reverse List" <<
         std::endl << "3 : Display List" <<
         std::endl << "4 : Exit" <<
         std::endl << "Enter your choice : ";
    std::cin>>choice;
    switch(choice)
    {
    case 1:
        addAtLast(&startList);
        break;
    case 2:
        startList = reverseList(startList);
        break;
    case 3:
        displayList(startList);
        break;
    case 4:
      std::cout<<std::endl<<"Thank you for using LinkedList v1.7"<<std::endl<<"Made by Rogue Coder"
           <<std::endl<<"Press any key to exit"<<std::endl;
      break;
    default:
      std::cout<<"\a\aWrong Choice\a\a"<<std::endl;
      break;
    }
  }
  while(choice != 4);
  std::cin.get();
  return 0;
}
void addAtLast(node** rootNode)
{
   node* newNode = new node;
   std::cout<<std::endl<<"Enter data : ";
   std::cin>>newNode -> data;
   if(*rootNode == NULL)
   {
      *rootNode = newNode;
   }
   else
   {
       node* currentNode = *rootNode;
       while(currentNode->next != NULL)
       {
           currentNode = currentNode->next;
       }
       currentNode -> next = newNode;
   }
   newNode -> next = NULL;
}
node* reverseList(node* rootNode)
{
    if (rootNode == NULL || rootNode -> next == NULL)
    {
        return rootNode;
    }
    node* remainingList = reverseList(rootNode -> next);
    rootNode -> next -> next = rootNode;
    rootNode -> next = NULL;
    return remainingList;
}
void displayList(node* rootNode)
{
  node *currentNode = rootNode;
  if(currentNode == NULL)
  {
    std::cout<<std::endl<<"\aList Empty\a"<<std::endl;
  }
  else
  {
    std::cout<<std::endl;
    while(currentNode != NULL)
    {
      std::cout<<currentNode->data<<"->";
      currentNode=currentNode->next;
    }
    std::cout<<"End of List"<<std::endl;
  }
}
Explanation of Code
#include <iostream> -> The compiler calls the Preprocessor to include the IOSTREAM(Standard Input / Output Streams Library) header file into the program, thus letting the use of the Standard Input / Output Streams functions like std::cin and std::cout. As per C++11 specification, including <iostream> automatically includes also <ios>, <streambuf>, <istream>, <ostream> and <iosfwd>.
int main() -> The entry point of the program where the execution starts. This function has to be named main. As per the ANSI specification, the return type has to be int. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.
std::cin (extern istream cin) -> Standard Input Stream, and object of class istream. It is generally used with the extraction operator (>>), though we can use member functions like get (cin.get()), read (cin.read()), etc. for the input. The use of extraction operator is much more popular due to the fact that it aids in getting formatted input.
std::cout (extern ostream cout) -> Standard Output Stream, and object of class ostream. It is generally used with the insertion operator (<<), though we can use member functions like write (cout.write()) for the output. The use of insertions operator is much more popular due to the fact that it aids in giving formatted output.
using namespace std; -> In modern IDEs, we have to explicitly write std::cout instead of cout to use the ostream cout object. Namespace std helps in easing off the pain of writing std:: again and again. Though make sure you are not trapped! The classes defined in std should not be redefined by you. So in case you want to define a class 'distance', you can't do so if you have used std namespace. Though you can define 'Distance' (capital D).
std::endl (ostream& endl (ostream& os)) -> This is a function which is used to insert a newline character and flush the stream. Because this function is a manipulator, it is designed to be used alone with no arguments in conjunction with the insertion (<<) operations on output streams.
{
  int data;
  node* next;
}; ->
node* startList; -> The global pointer ‘startList’ which we are going to use to point to the first node/root node/ start node of the linked list, we are going to implement in this program.
int choice; -> This variable 'choice' will be used for the user’s choice in the menu driven program.
{
   node* newNode = new node;
   std::cout<<std::endl<<"Enter data : ";
   std::cin>>newNode -> data;
   if(*rootNode == NULL)
   {
      *rootNode = newNode;
   }
   else
   {
       node* currentNode = *rootNode;
       while(currentNode->next != NULL)
       {
           currentNode = currentNode->next;
       }
       currentNode -> next = newNode;
   }
   newNode -> next = NULL;
} ->
{
    if (rootNode == NULL || rootNode -> next == NULL)
    {
        return rootNode;
    }
    node* remainingList = reverseList(rootNode -> next);
    rootNode -> next -> next = rootNode;
    rootNode -> next = NULL;
    return remainingList;
} ->
The heart of the function is to reverse one node, and push rest of the list to stack. Thus, node* remainingList = reverseList(rootNode -> next);, here we send send the list but the first node back to the function recursively, until we are at the last node, when we would simply return(if clause, as explained above).
Then we make this separated rootNode to be the successor of it's sucessor, reversing the connection in effect. Then we mark this node to point to NULL, as if it was the last node, and we return back the remaining List, via, remainingList, which is the pointer to rootNode of the rest of the list.
Consider example list 1 -> 2 -> 3 -> 4 -> 5 .
Lets iterate through the function's recursion stack and see what happens.
- rootNode is not NULL. rootNode is not the only node either. So we have rootNode pointing to 1, and we send pointer to 2 to the new call.
- Now rootNode is holding pointer to 2. This is not NULL, and is not the only node either. So we send pointer to 3 to the new call.
- Now rootNode is holding pointer to 3. This is not NULL, and is not the only node either. So we send pointer to 4 to the new call.
- Now rootNode is holding pointer to 4. This is not NULL, and is not the only node either. So we send pointer to 5 to the new call.
- Now rootNode is holding pointer to 5. This is not NULL, but is the only node. So we return pointer to 5 back to the call.
- Now the control is with the previous call, where we called our function with pointer to 4. Here rootNode is holding 4 (and 4 points to 5 as the original 'next' value), and remainingList is holding 5. We will now reverse the connections. Right now the list is 4 -> 5. So we make 5 to point to 4, and 4 to point to NULL. Thus our list becomes 5 -> 4 . We return back the address of 5 to the call.
- Now the control is with the previous call, where we called our function with pointer to 3. Here rootNode is holding 3 (and 3 points to 4 as the original 'next' value), and remainingList is holding address to 5(thus in effect 5 -> 4, as returned by previous call). We will now reverse the connections. Right now the list is 3 -> 4 and 5 -> 4. So we make 4 to point to 3, and 3 to point to NULL. Thus our list becomes 5 -> 4 -> 3. We return back the address of 5 to the call.
- Now the control is with the previous call, where we called our function with pointer to 2. Here rootNode is holding 2 (and 2 points to 3 as the original 'next' value), and remainingList is holding address to 5(thus in effect 5 -> 4 -> 3, as returned by previous call). We will now reverse the connections. Right now the list is 2 -> 3 and 5 -> 4 -> 3. So we make 3 to point to 2, and 2 to point to NULL. Thus our list becomes 5 -> 4 -> 3 -> 2. We return back the address of 5 to the call.
- Now the control is with the previous call, where we called our function with pointer to 1. Here rootNode is holding 1 (and 1 points to 2 as the original 'next' value), and remainingList is holding address to 5(thus in effect 5 -> 4 -> 3 -> 2, as returned by previous call). We will now reverse the connections. Right now the list is 1 -> 2 and 5 -> 4 -> 3 -> 2. So we make 2 to point to 1, and 1 to point to NULL. Thus our list becomes 5 -> 4 -> 3 -> 2 -> 1. We return back the address of 5 to the call.
- Previous one was the last call on our recursion stack. Thus, we return the address of 5 as the address of rootNode to the call. Thus, we have reversed the list.
{
  node *currentNode = rootNode;
  if(currentNode == NULL)
  {
    std::cout<<std::endl<<"\aList Empty\a"<<std::endl;
  }
  else
  {
    std::cout<<std::endl;
    while(currentNode != NULL)
    {
      std::cout<<currentNode->data<<"->";
      currentNode=currentNode->next;
    }
    std::cout<<"End of List"<<std::endl;
  }
} ->
do{..}while() -> The program loop which encapsulates the whole program. Until the user chooses to exit the program, the control loops within this.
exit(0); -> This function is used to exit the program with an error code as it's argument. '0' implies normal exit. Other values are used for debugging purposes.
std::cin.get() -> This statement is used to pause our program, until user presses a key. This function is not necessary in your program, I use it to see my outputs at a paused screen. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.
No comments:
Post a Comment
Need help?