Sunday 17 July 2016

Remove Duplicates in a linked list

/* Remove Duplicates in a linked list */
/*  Using two loops like approach
    TC : O(n^2)
*/

 #include <iostream>  
 #include <string.h>  
 using namespace std;  
 class node {  
   private :  
     int data;  
     node *next;  
   public :  
     node(){  
       this->data = 0;  
       this->next = NULL;  
     }  
   friend class linkedList;  
 };  
 class linkedList {  
   private:  
     node *head;  
   public:  
     linkedList(){  
       this->head = NULL;  
     }  
     ~linkedList(){  
       node *current = this->head;  
       while(current){  
         node *t = current->next;  
         delete current;  
         current = t;  
       }  
       this->head = NULL;  
       cout << "Destructor called!" << endl;  
     }  
     void insertAtEnd(int data){  
       node *last = new node();  
       last->data = data;  
       last->next = NULL;  
       if(this->head == NULL){  
         this->head = last;  
       } else {  
         node *t = this->head;  
         while(t->next){  
           t = t->next;  
         }  
         t->next = last;  
       }  
     }  
     void insertAtFront(int data){  
       node *last = new node();  
       last->data = data;  
       last->next = NULL;  
       if(this->head == NULL){  
         this->head = last;  
       } else {  
         last->next = this->head;  
         this->head = last;  
       }  
     }  
     void insertAtN(int data, int n){  
       node *last = new node();  
       last->data = data;  
       last->next = NULL;  
       if(n <= 0){  
         cout << "N can't be negative!" << endl;  
         return;  
       }  
       if(this->head == NULL && n != 1){  
         cout << "List is empty and insertion possible only at the front!" <<endl;  
         return;  
       }  
       if(n == 1){  
         insertAtFront(data);  
         return;  
       } else {  
         node *t = this->head;  
         int i = 1;  
         while(i < (n - 1) && t->next){  
           i++;  
           t = t->next;  
         }  
         if(i < (n - 1)){  
           cout << n << " : Out of range error!" << endl;  
           return;  
         } else{  
           last->next = t->next;  
           t->next = last;  
         }  
       }  
     }  
     void deleteFromHead(){  
       if(this->head == NULL){  
         cout << "List is empty! Deletion not possible!" << endl;  
         return;  
       }  
       node *t = this->head;  
       this->head = this->head->next;  
       delete t;  
     }  
     void deleteFromTail(){  
       //No node  
       if(this->head == NULL){  
         cout << "List is empty! Deletion not possible!" << endl;  
         return;  
       }  
       //Just one node, reset head  
       else if(this->head->next == NULL){  
         this->head = NULL;  
         return;  
       } else{  
         node *t = this->head;  
         node *prev;  
         while(t->next){  
           prev = t;  
           t = t->next;  
         }  
         prev->next = NULL;  
         delete t;  
       }  
     }  
     void deleteFromN(int n){  
       if(n <= 0){  
         cout << "N should be a positive integer!" << endl;  
       }  
       //No node  
       if(this->head == NULL){  
         cout << "List is empty! Deletion not possible!" << endl;  
         return;  
       }  
       //N == 1, delete from head  
       else if(n == 1){  
         deleteFromHead();  
         return;  
       } else{  
         int i = 1;  
         node *t = this->head;  
         while(i < (n - 1) && t->next){  
           i++;  
           t = t->next;  
         }  
         if(i <= (n - 1) && t->next == NULL){  
           cout << n << " is out of bound!" << endl;  
           return;  
         } else{  
           node *del = t->next;  
           t->next = del->next;  
           delete del;  
         }  
       }  
     }  
     void deleteByElement(int elt){  
       //No node  
       if(this->head == NULL){  
         cout << "List is empty! Deletion not possible!" << endl;  
         return;  
       }  
       //N == 1, delete from head  
       else if(this->head->data == elt){  
         deleteFromHead();  
         return;  
       } else{  
         node *t = this->head;  
         node *prev;  
         while(t->next && t->data != elt){  
           prev = t;  
           t = t->next;  
         }  
         if(t->data != elt){  
           cout << elt << " not found in the list!" << endl;  
           return;  
         } else{  
           prev->next = t->next;  
           delete t;  
         }  
       }  
     }  
     void Print(){  
       if(this->head == NULL){  
         cout << "List is empty!"<<endl;  
         return;  
       } else {  
         node *t = this->head;  
         while(t->next){  
           cout << t->data << " -> ";  
           t = t->next;  
         }  
         cout << t->data << " -> NULL";  
         cout << endl;  
       }  
     }  
     void removeDups(){  
       node *t1 = this->head;  
       node *t2;  
       while(t1){  
         t2 = t1;  
         while(t2->next){  
           if(t2->next->data == t1->data){  
             t2->next = t2->next->next;  
           } else{  
             t2 = t2->next;  
           }  
         }  
         t1 = t1->next;  
       }  
     }  
 };  
 int main(){  
   linkedList A, B, C;  
   C.insertAtN(10, 1);  
   C.insertAtN(20, 2);  
   C.insertAtN(30, 3);  
   C.insertAtN(40, 2);  
   C.insertAtN(30, 3);  
   C.insertAtN(50, 3);  
   C.insertAtN(40, 3);  
   C.Print();  
   C.removeDups();  
   C.Print();  
   C.insertAtN(50, 3);  
   C.insertAtN(40, 3);  
   C.Print();  
   C.removeDups();  
   C.Print();  
   return 0;  
 }  
Share:

0 comments:

Post a Comment

Contact Me

Name

Email *

Message *

Popular Posts

Blog Archive