diff --git a/linked-lists/README.md b/linked-lists/README.md index d320fc5c..06119ce0 100644 --- a/linked-lists/README.md +++ b/linked-lists/README.md @@ -8,6 +8,7 @@ 4. [Circular Linked List](c-or-cpp/circular.cpp) 5. [Insertion Linked List](c-or-cpp/all-possible-insertion.cpp) 6. [Josephus Problem Using Circular Linked List](c-or-cpp/josephus-problem.c) +7. [Merge Two Singly linked List](c-or-cpp/merge.cpp) ### Java diff --git a/linked-lists/c-or-cpp/merge.cpp b/linked-lists/c-or-cpp/merge.cpp new file mode 100644 index 00000000..c6c41122 --- /dev/null +++ b/linked-lists/c-or-cpp/merge.cpp @@ -0,0 +1,117 @@ +#include + +template +class LinkedList{ + public: + int size = 0; + void pushfront(const T& data); + T& front(); + void popfront(); + void display(); + void merge(const LinkedList& other); + LinkedList& operator=(const LinkedList& other); + + private: + struct node{ + T data; + node* next; + node(const T& data): data(data), next(nullptr){} + }; + node* head_ = nullptr; +}; + +template +LinkedList& LinkedList::operator=(const LinkedList& other){ + + //clearing all node in 'this' list + while(head_ != nullptr){ + this->popfront(); + } + node* current = other.head_; + while(current != nullptr){ + this->pushfront(current->data); + current = current->next; + } + return *this; +} + +template +T& LinkedList::front(){ + //returns the first node data of the linked list + + return head_->data; +} + +template +void LinkedList::popfront(){ + //sets the head pointer to nullptr and the next node as the new head + + //if there is no node then do nothing + if(head_ == nullptr) return; + if(head_->next == nullptr){ + delete head_; + head_ = nullptr; + --size; + return; + } + node* oldhead = head_; + head_ = head_->next; + delete oldhead; + oldhead = nullptr; + size--; +} + +template +void LinkedList::pushfront(const T& data){ + node* n = new node(data); + /*if the list is empty pointer the head pointer to + newly created node */ + if(head_ == nullptr){ head_ = n; } + else{ + n->next = head_; + head_ = n; + } + size++; +} + + +template +void LinkedList::display(){ + node* thru = head_; + while(thru != nullptr){ + std::cout << thru->data << std::endl; + thru = thru->next; + } +} + +template +void LinkedList::merge(const LinkedList& other) { + LinkedList l1 ; + l1 = other; + while(l1.size > 0){ + //inserting the data of the other list to the current list + this->pushfront(l1.front()); + //removing the first node of the list + //this will continue until there is no more node in the other list + l1.popfront(); + } +} + +int main(){ + LinkedList l1, l2; + + l1.pushfront(1); + l1.pushfront(2); + l1.pushfront(3); + l2.pushfront(21); + l2.pushfront(11); + l2.pushfront(31); + + l2.merge(l1); + + std::cout << "AFTER MERGE" << std::endl; + std::cout << "LIST 1" << std::endl; + l1.display(); + std::cout << "LIST 2" << std::endl; + l2.display(); +}