m2skills

reverse Linked List c++

Jan 28th, 2018
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. // program to reverse a Linked list
  2.  
  3. #include <iostream>
  4. #include <stack>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. class node{
  10.  
  11. protected:
  12.     int element;
  13.     node* link;
  14.  
  15. public:
  16.     //constructor that accepts only element
  17.     node(int element) {
  18.         this->element = element;
  19.         this->link = NULL;
  20.     }
  21.  
  22.     //constructor that accepts both link and element
  23.     node(int element, node* link){
  24.         this->element = element;
  25.         this->link = link;
  26.     }
  27.  
  28.     //method to update the element
  29.     void updateData(int element){
  30.         this->element = element;
  31.     }
  32.  
  33.     //method to update or setup link
  34.     void updateLink(node* link){
  35.         this->link = link;
  36.     }
  37.  
  38.     //method to get the element from the node
  39.     int getElement(){
  40.         return this->element;
  41.     }
  42.  
  43.     //method to get the next node
  44.     node* getNextNode(){
  45.         return this->link;
  46.     }
  47. };
  48.  
  49. class Linkedlist {
  50. public:
  51.     node *head;
  52.  
  53.     //constructor for the Linked List class
  54.     Linkedlist() {
  55.         head = NULL;
  56.     }
  57.  
  58.     //returns head node
  59.     node *getHead() {
  60.         return this->head;
  61.     }
  62.  
  63.     // method to add a node at the end
  64.     void insert(int element) {
  65.         node *tempNode = new node(element);
  66.         node *p = head;
  67.         if (head == NULL) {
  68.             head = tempNode;
  69.             return;
  70.         }
  71.         else {
  72.             while (p->getNextNode() != NULL) {
  73.                 p = p->getNextNode();
  74.             }
  75.             p->updateLink(tempNode);
  76.             return;
  77.         }
  78.     }
  79.  
  80.     //method to display all the elements of the Linked List
  81.     void display() {
  82.         cout << "\n";
  83.         node *tempNode = head;
  84.         while (tempNode != NULL) {
  85.             if (tempNode->getNextNode() != NULL)
  86.                 cout << tempNode->getElement() << " --> ";
  87.             else
  88.                 cout << tempNode->getElement();
  89.  
  90.             tempNode = tempNode->getNextNode();
  91.         }
  92.         return;
  93.     }
  94. };
  95.  
  96. // function to reverse linked list
  97. node* reverse(node* head){
  98.     if(head == NULL){
  99.         return head;
  100.     }
  101.  
  102.     node* next = NULL;
  103.     node* prev = NULL;
  104.     node* current = head;
  105.     while(current != NULL){
  106.         next = current->getNextNode();
  107.         current->updateLink(prev);
  108.         prev = current;
  109.         current = next;
  110.     }
  111.     return prev;
  112. }
  113.  
  114. int main() {
  115.     Linkedlist l1;
  116.     l1.insert(1);
  117.     l1.insert(2);
  118.     l1.insert(3);
  119.     l1.insert(4);
  120.     l1.insert(5);
  121.     l1.insert(6);
  122.     l1.insert(7);
  123.     l1.insert(8);
  124.  
  125.     cout<<"\nBefore Reversing the linked list is : ";
  126.     l1.display();
  127.     l1.head = reverse(l1.head);
  128.     cout<<"\nAfter Reversing the linked list is : ";
  129.     l1.display();
  130.  
  131.     return 0;
  132. }
Add Comment
Please, Sign In to add comment