m2skills

rotate ll cpp

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