Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
TemplateDoubly
//g++ 7.4.0 //Template Double Linked List //class code credit internet #include <iostream> using namespace std; template <class V> class node { public: V data; node<V>* next; node<V>* prev; }; template <class T> class Double_List { node<T>* head, * tail; public: Double_List(){head=NULL; tail=NULL; } void create_list(T n); void add_begin(T n); void display(); void reverse(); node<T>* gethead() { return head;} node<T>* gettail(){return tail;} void after(node<T> *q, T v) { node<T>* p = new node<T>; p->data=v; p->next=q->next; q->next = p; p->prev= q; } void before (node<T>* q , T v) { node<T> * p= new node<T>; p->data= v; p->prev= q->prev; q->prev = p; p->next = q; } node<T>* delnode(node<T>* begin, T item) { node<T>* temp; if (begin == NULL) { cout <<" Empty list \n"; return begin; } if (begin->next == NULL){ if(begin->data == item) { temp = begin; begin = NULL; delete temp; return begin; } else { cout<<"Item "<<item<<" isn't found\n"; return begin; } } if (begin->data == item) { temp = begin; begin = begin->next; begin->prev= NULL; delete temp; return begin; } temp = begin->next; while (temp->next != NULL) { if(temp->data == item) { temp->prev->next = temp->next; temp->next->prev = temp->prev; delete temp; return begin; } temp = temp ->next; } if (temp->data == item) { temp->prev->next= NULL; delete temp; return begin; } cout<<"Item "<<item<<" isn't found\n"; return begin; } void remove(); bool isempty() { if (head == NULL) return true; return false; } }; template <class T> void Double_List<T>::create_list(T n) { node<T> *newnode; newnode=new node<T>; newnode->data=n; if(head==NULL) { head=newnode; newnode->prev=NULL; newnode->next=NULL; tail=newnode; } else { newnode->prev=tail; tail->next=newnode; newnode->next=NULL; tail=newnode; } } template <class T> void Double_List<T>::add_begin(T n) { if(head==NULL) { cout<<" NO element, ist create : \n"; return; } node<T> *temp=new node<T>; temp->data=n; temp->prev=NULL; temp->next=head; head->prev=temp; head=temp; cout<<" Insert Sucessfull.... \n"; } template <class T> void Double_List<T>::display() { node<T> *q; if(head==NULL) { cout<<" list empty.. \n"; return; } q=head; cout<<" The Double Linked list is \n"; while(q!=NULL) { cout<<q->data<<"<-->"; q=q->next; } } template <class T> void Double_List<T>::reverse() { node<T> *r; if(tail==NULL) { cout<<"EMPTY list: \n"; return; } else { r=tail; while(r!=NULL) { cout<<r->data<<" <--> "; r=r->prev; }cout<<"reverse sucessful: \n"; } } template <class T> void Double_List<T>:: remove() { if(isempty()) { cout<<"is empty"; } else if (head == tail){ delete head; head= tail =NULL; } else { node<T>* temp, *temp2; temp2= tail; temp= head; while (temp->next!= NULL) { temp2= temp; temp=temp->next; } tail = temp2; tail->next=NULL; delete temp; while(temp->prev != NULL) { temp= temp2; temp2= temp2->prev;} head = temp; tail->prev= NULL; delete temp2; } } int main() { Double_List<int> dl; Double_List<char> cl; dl.create_list(10); dl.create_list(11); dl.create_list(12); dl.add_begin(9); dl.after(dl.gethead()->next->next,7); dl.before(dl.gettail()->prev,8); dl.display(); cout<<endl; dl.reverse(); cl.create_list('H'); cl.create_list('!'); cl.after(cl.gettail(),'!'); cl.display(); cout<<endl; cl.reverse(); return 0; }
run
|
edit
|
history
|
help
0
e-olymp.com---problem1590---Birthday 2
VecHotel
LIS
My Tuple class
Bucket Graph Creation
For Hello World
20201123
offsetof
mergesort tree
random lotto number game