Linked list implementation with following operations:
1. Add Node.
2. Display linked list.
3.Remove duplicate Nodes
4. Delete nth Node from linked list
5. Add 2 numbers and create new linked list from 2 linked lists, if the first position of number is placed in the head of linked lists.
#include<iostream>
#include<conio.h>
using namespace std;
template<typename T>
struct Node{
T data;
Node* next;
};
template<typename T>
class linkedList
{
private:
Node<T>* findNode(T x);
public:
Node<T>* head;
linkedList();
~linkedList();
void addNode(T info);
int display();
int removeDuplicate();
int deleteNthNode(Node<T>*);
linkedList<T> addTwoLinkedList(linkedList& x);//Assume number in liked list in reverse order.
//The integer in 1's position is in head;
};
template <typename T>
linkedList<T> linkedList<T>::addTwoLinkedList(linkedList<T>& x)
{
linkedList<int> result;
Node<T> * tmp1=head;
Node<T>* tmp2=x.head;
T val=0;
T carry=0;
while(tmp1)
{
val+=tmp1->data;
tmp1=tmp1->next;
while(tmp2)
{
val+=tmp2->data;
tmp2=tmp2->next;
break;
}
carry= val%10;
val=val/10;
if(carry > 0)
result.addNode(carry);
}
carry=val%10;
val=val/10;
if(carry>0)
result.addNode(carry);
while(tmp2)
{
val+=tmp2->data;
tmp2=tmp2->next;
carry= val%10;
val=val/10;
if (carry >0)
result.addNode(carry);
}
carry=val%10;
val=val/10;
if(carry > 0)
result.addNode(carry);
if(val > 0)
result.addNode(val);
return result;
}
template<typename T>
Node<T>* linkedList<T>::findNode(T x)
{
if( head == NULL)
return NULL;
Node<T>* tmp= head;
while(tmp)
{
if(tmp->data==x)
return tmp;
else
tmp=tmp->next;
}
return NULL;
}
template<typename T>
int linkedList<T>::deleteNthNode(Node<T>* n)
{
if(n)
return 0;
Node<T>* tmp=n->next;
n->next=tmp->next;
n->data=tmp->data;
delete tmp;
return 1;
}
template<typename T>
linkedList<T>::linkedList()
{
head=NULL;
}
template<typename T>
linkedList<T>::~linkedList()
{
Node<T>* tmp;
Node<T>* prev=head;
while(prev)
{
tmp= prev;
prev=prev->next;
delete tmp;
}
}
template<typename T>
void linkedList<T>::addNode(T info)
{
Node<T>* tmp=new Node<T>();
tmp->data=info;
tmp->next=NULL;
if(head==NULL)
{
head=tmp;
}
else
{
Node<T>* prev=head;
Node<T>* tmp1=head->next;
while(tmp1)
{
prev=tmp1;
tmp1=tmp1->next;
}
prev->next=tmp;
}
}
template<typename T>
int linkedList<T>::display()
{
if(head==NULL)
return 0;
Node<T>* tmp=head;
while(tmp)
{
cout<<tmp->data<<endl;
tmp=tmp->next;
}
return 1;
}
template<typename T>
int linkedList<T>::removeDuplicate()
{
if(head==NULL)
return 0;
Node<T>* prev=head;
Node<T>* tmp;
for(Node<T>* current=head->next; current !=NULL;)
{
Node<T>* runner;
for(runner=head; runner !=current; runner=runner->next)
{
if(current->data== runner->data)
{
tmp= current;
prev->next=current->next;
current=current->next;
delete tmp;
tmp=NULL;
break;
}
}
if(current==runner)
{
prev=current;
current=current->next;
}
}
return 1;
}
int main()
{
linkedList<int> l;
l.addNode(1);
l.addNode(1);
l.addNode(3);
l.addNode(1);
cout<<"Display l"<<endl;
l.display();
linkedList<int> m;
m.addNode(2);
m.addNode(3);
m.addNode(4);
m.addNode(5);
cout<<"Display m:"<<endl;
m.display();
cout<<"Display sum:"<<endl;
linkedList<int> sum=l.addTwoLinkedList(m);
sum.display();
cout<<"Remove Duplicates"<<endl;
l.removeDuplicate();
l.display();
cout<<"Remove head"<<endl;
l.deleteNthNode(l.head);
l.display();
getch();
}
1. Add Node.
2. Display linked list.
3.Remove duplicate Nodes
4. Delete nth Node from linked list
5. Add 2 numbers and create new linked list from 2 linked lists, if the first position of number is placed in the head of linked lists.
#include<iostream>
#include<conio.h>
using namespace std;
template<typename T>
struct Node{
T data;
Node* next;
};
template<typename T>
class linkedList
{
private:
Node<T>* findNode(T x);
public:
Node<T>* head;
linkedList();
~linkedList();
void addNode(T info);
int display();
int removeDuplicate();
int deleteNthNode(Node<T>*);
linkedList<T> addTwoLinkedList(linkedList& x);//Assume number in liked list in reverse order.
//The integer in 1's position is in head;
};
template <typename T>
linkedList<T> linkedList<T>::addTwoLinkedList(linkedList<T>& x)
{
linkedList<int> result;
Node<T> * tmp1=head;
Node<T>* tmp2=x.head;
T val=0;
T carry=0;
while(tmp1)
{
val+=tmp1->data;
tmp1=tmp1->next;
while(tmp2)
{
val+=tmp2->data;
tmp2=tmp2->next;
break;
}
carry= val%10;
val=val/10;
if(carry > 0)
result.addNode(carry);
}
carry=val%10;
val=val/10;
if(carry>0)
result.addNode(carry);
while(tmp2)
{
val+=tmp2->data;
tmp2=tmp2->next;
carry= val%10;
val=val/10;
if (carry >0)
result.addNode(carry);
}
carry=val%10;
val=val/10;
if(carry > 0)
result.addNode(carry);
if(val > 0)
result.addNode(val);
return result;
}
template<typename T>
Node<T>* linkedList<T>::findNode(T x)
{
if( head == NULL)
return NULL;
Node<T>* tmp= head;
while(tmp)
{
if(tmp->data==x)
return tmp;
else
tmp=tmp->next;
}
return NULL;
}
template<typename T>
int linkedList<T>::deleteNthNode(Node<T>* n)
{
if(n)
return 0;
Node<T>* tmp=n->next;
n->next=tmp->next;
n->data=tmp->data;
delete tmp;
return 1;
}
template<typename T>
linkedList<T>::linkedList()
{
head=NULL;
}
template<typename T>
linkedList<T>::~linkedList()
{
Node<T>* tmp;
Node<T>* prev=head;
while(prev)
{
tmp= prev;
prev=prev->next;
delete tmp;
}
}
template<typename T>
void linkedList<T>::addNode(T info)
{
Node<T>* tmp=new Node<T>();
tmp->data=info;
tmp->next=NULL;
if(head==NULL)
{
head=tmp;
}
else
{
Node<T>* prev=head;
Node<T>* tmp1=head->next;
while(tmp1)
{
prev=tmp1;
tmp1=tmp1->next;
}
prev->next=tmp;
}
}
template<typename T>
int linkedList<T>::display()
{
if(head==NULL)
return 0;
Node<T>* tmp=head;
while(tmp)
{
cout<<tmp->data<<endl;
tmp=tmp->next;
}
return 1;
}
template<typename T>
int linkedList<T>::removeDuplicate()
{
if(head==NULL)
return 0;
Node<T>* prev=head;
Node<T>* tmp;
for(Node<T>* current=head->next; current !=NULL;)
{
Node<T>* runner;
for(runner=head; runner !=current; runner=runner->next)
{
if(current->data== runner->data)
{
tmp= current;
prev->next=current->next;
current=current->next;
delete tmp;
tmp=NULL;
break;
}
}
if(current==runner)
{
prev=current;
current=current->next;
}
}
return 1;
}
int main()
{
linkedList<int> l;
l.addNode(1);
l.addNode(1);
l.addNode(3);
l.addNode(1);
cout<<"Display l"<<endl;
l.display();
linkedList<int> m;
m.addNode(2);
m.addNode(3);
m.addNode(4);
m.addNode(5);
cout<<"Display m:"<<endl;
m.display();
cout<<"Display sum:"<<endl;
linkedList<int> sum=l.addTwoLinkedList(m);
sum.display();
cout<<"Remove Duplicates"<<endl;
l.removeDuplicate();
l.display();
cout<<"Remove head"<<endl;
l.deleteNthNode(l.head);
l.display();
getch();
}