Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- using namespace std;
- class node{
- protected:
- int element;
- node* link;
- public:
- //constructor that accepts only element
- node(int element) {
- this->element = element;
- this->link = NULL;
- }
- //constructor that accepts both link and element
- node(int element, node* link){
- this->element = element;
- this->link = link;
- }
- //method to update the element
- void updateData(int element){
- this->element = element;
- }
- //method to update or setup link
- void updateLink(node* link){
- this->link = link;
- }
- //method to get the element from the node
- int getElement(){
- return this->element;
- }
- //method to get the next node
- node* getNextNode(){
- return this->link;
- }
- };
- class Linkedlist {
- public:
- node *head;
- //constructor for the Linked List class
- Linkedlist() {
- head = NULL;
- }
- //returns head node
- node *getHead() {
- return this->head;
- }
- // method to add a node at the end
- void insert(int element) {
- node *tempNode = new node(element);
- node *p = head;
- if (head == NULL) {
- head = tempNode;
- return;
- }
- else {
- while (p->getNextNode() != NULL) {
- p = p->getNextNode();
- }
- p->updateLink(tempNode);
- return;
- }
- }
- //method to display all the elements of the Linked List
- void display() {
- cout << "\n";
- node *tempNode = head;
- while (tempNode != NULL) {
- if (tempNode->getNextNode() != NULL)
- cout << tempNode->getElement() << " --> ";
- else
- cout << tempNode->getElement();
- tempNode = tempNode->getNextNode();
- }
- return;
- }
- };
- // merge 2 sorted linked lists
- node* merge(node* head1, node* head2){
- node* head = NULL;
- node* current = NULL;
- node* curr1 = head1;
- node* curr2 = head2;
- while(curr1 != NULL && curr2 != NULL){
- if(curr1->getElement() > curr2->getElement()){
- if(head == NULL){
- head = curr2;
- current = head;
- }else{
- current->updateLink(curr2);
- current = current->getNextNode();
- }
- curr2 = curr2->getNextNode();
- }else{
- if(head == NULL){
- head = curr1;
- current = head;
- }else{
- current->updateLink(curr1);
- current = current->getNextNode();
- }
- curr1 = curr1->getNextNode();
- }
- }
- if(curr1 != NULL){
- current->updateLink(curr1);
- current = current->getNextNode();
- }
- if(curr2 != NULL){
- current->updateLink(curr2);
- current = current->getNextNode();
- }
- return head;
- }
- int main() {
- cout<<"Program to merge 2 Linked Lists";
- Linkedlist l1;
- l1.insert(1);
- l1.insert(3);
- l1.insert(5);
- l1.insert(7);
- l1.insert(8);
- l1.insert(9);
- l1.insert(11);
- l1.insert(13);
- cout<<"\nFirst Linked list is : ";
- l1.display();
- Linkedlist l2;
- l2.insert(2);
- l2.insert(4);
- l2.insert(5);
- l2.insert(6);
- l2.insert(9);
- l2.insert(8);
- cout<<"\nSecond Linked list is : ";
- l2.display();
- l1.head = merge(l1.getHead(), l2.getHead());
- cout<<"\nMerged Linked list is : ";
- l1.display();
- return 0;
- }
Add Comment
Please, Sign In to add comment