m2skills

reverse starting ll cpp

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