m2skills

merge sorted ll c++

Jan 28th, 2018
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 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. // merge 2 sorted linked lists
  94. node* merge(node* head1, node* head2){
  95.     node* head = NULL;
  96.     node* current = NULL;
  97.     node* curr1 = head1;
  98.     node* curr2 = head2;
  99.  
  100.     while(curr1 != NULL && curr2 != NULL){
  101.         if(curr1->getElement() > curr2->getElement()){
  102.             if(head == NULL){
  103.                 head = curr2;
  104.                 current = head;
  105.             }else{
  106.                 current->updateLink(curr2);
  107.                 current = current->getNextNode();
  108.             }
  109.             curr2 = curr2->getNextNode();
  110.         }else{
  111.             if(head == NULL){
  112.                 head = curr1;
  113.                 current = head;
  114.             }else{
  115.                 current->updateLink(curr1);
  116.                 current = current->getNextNode();
  117.             }
  118.             curr1 = curr1->getNextNode();
  119.         }
  120.     }
  121.  
  122.     if(curr1 != NULL){
  123.         current->updateLink(curr1);
  124.         current = current->getNextNode();
  125.     }
  126.  
  127.     if(curr2 != NULL){
  128.         current->updateLink(curr2);
  129.         current = current->getNextNode();
  130.     }
  131.  
  132.     return head;
  133. }
  134.  
  135. int main() {
  136.     cout<<"Program to merge 2 Linked Lists";
  137.     Linkedlist l1;
  138.     l1.insert(1);
  139.     l1.insert(3);
  140.     l1.insert(5);
  141.     l1.insert(7);
  142.     l1.insert(8);
  143.     l1.insert(9);
  144.     l1.insert(11);
  145.     l1.insert(13);
  146.  
  147.     cout<<"\nFirst Linked list is : ";
  148.     l1.display();
  149.  
  150.     Linkedlist l2;
  151.     l2.insert(2);
  152.     l2.insert(4);
  153.     l2.insert(5);
  154.     l2.insert(6);
  155.     l2.insert(9);
  156.     l2.insert(8);
  157.  
  158.     cout<<"\nSecond Linked list is : ";
  159.     l2.display();
  160.  
  161.     l1.head = merge(l1.getHead(), l2.getHead());
  162.  
  163.     cout<<"\nMerged Linked list is : ";
  164.     l1.display();
  165.  
  166.     return 0;
  167. }
Add Comment
Please, Sign In to add comment