m2skills

middleLL cpp

Mar 29th, 2017
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. // Program to find the middle node of the linked list in a single traversal
  2. #include <iostream>
  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. protected:
  48.     node* head;
  49. public:
  50.  
  51.     //constructor for the Linked List class
  52.     Linkedlist(){
  53.         head = NULL;
  54.     }
  55.  
  56.     //method returns true if the list is empty
  57.     bool isEmpty(){
  58.         return head == NULL;
  59.     }
  60.  
  61.     //returns head node
  62.     node* getHead(){
  63.         return this->head;
  64.     }
  65.  
  66.     //method to add a node at starting
  67.     void addToStart(int element){
  68.         node* tempNode = new node(element);
  69.         if(head == NULL){
  70.             head = tempNode;
  71.         }
  72.         else{
  73.             tempNode->updateLink(head);
  74.             head = tempNode;
  75.         }
  76.     }
  77.  
  78.     // method to add a node at the end
  79.     void addToEnd(int element){
  80.         node* tempNode = new node(element);
  81.         node* p = head;
  82.         if(head == NULL){
  83.             head = tempNode;
  84.             return;
  85.         }
  86.         else{
  87.             while(p->getNextNode()!= NULL){
  88.                 p = p->getNextNode();
  89.             }
  90.             p->updateLink(tempNode);
  91.             return;
  92.         }
  93.     }
  94.  
  95.     //method to display all the elements of the Linked List
  96.     void display(){
  97.         cout<<"\n";
  98.         node* tempNode = head;
  99.         while(tempNode != NULL){
  100.             if(tempNode->getNextNode() != NULL)
  101.                 cout<<tempNode->getElement()<<" --> ";
  102.             else
  103.                 cout<<tempNode->getElement();
  104.  
  105.             tempNode = tempNode->getNextNode();
  106.         }
  107.         return;
  108.     }
  109.  
  110.     int middleNode(){
  111.         node* tempNode = head;
  112.         node* fast = head;
  113.         node* slow = head;
  114.         while(1){
  115.             if(fast->getNextNode() != NULL){
  116.                 fast = fast->getNextNode();
  117.                 slow = slow->getNextNode();
  118.             }else{
  119.                 break;
  120.             }
  121.            
  122.             if(fast->getNextNode() != NULL){
  123.                 fast = fast->getNextNode();
  124.             }else{
  125.                 break;
  126.             }
  127.         }
  128.        
  129.         return slow->getElement();
  130.     }
  131.  
  132. };
  133.  
  134. int main() {
  135.     Linkedlist l1;
  136.     l1.addToStart(23);
  137.     l1.addToStart(1);
  138.     l1.addToStart(5);
  139.     l1.addToStart(26);
  140.     l1.addToStart(27);
  141.     l1.addToStart(28);
  142.     l1.addToEnd(24);
  143.     l1.addToEnd(22);
  144.     l1.addToEnd(13);
  145.     cout<<"\nThe Elements of the Linked List are : ";
  146.     l1.display();
  147.  
  148.     cout<<"\n\nMiddle Node of the Linked List is : "<<l1.middleNode();
  149.  
  150.     return 0;
  151. }
Add Comment
Please, Sign In to add comment