Self-written C + + doubly linked list

Source: Internet
Author: User

Try to write a linked list using lambda and template


 #include "stdafx.h" Template<class t>struct node{t value;struct Node * pnext;struct Node * PPREV;}; Template<class t>class list{private:node<t> * M_phead;int M_len; node<t>* List<t>::newnode (node<t>* prev, T value); node<t>* list<t>::D eletenode (node<t>* Node, std::function<void (T) > Paction); void List<T >::movenext (node<t>* last, std::function<node<t>* (node<t>*) > Paction); node<t>* List<t>::moveto (T value); node<t>* list<t>::movetoat (int index);p ublic:list (); ~list (); int getlength (); bool IsEmpty (); bool Insert ( t value); bool Remove (t value, std::function<void (t) > Paction); bool RemoveAt (int index, std::function<void (t) > Paction); T elementat (int index), void Dispose (Std::function<void (T) > Paction), void Dispose (), void Actionasc (std:: Function<void (t) > Paction); void Actiondesc (Std::function<void (t) > Paction); 

#include "stdafx.h" #include "List.h" Template<class t>list<t>::list () {This->m_len = 0;this->m_ Phead = NULL;} Template<class T>list<t>::~list () {if (This->m_len > 0) {this->dispose ();}} Template<class t>node<t>* List<t>::newnode (node<t>* prev, T value) {node<t>* temp = new Node<t> (); temp->pnext = Null;temp->pprev = Prev;temp->value = Value;return temp;}; Template<class t>node<t>* list<t>::D eletenode (node<t>* Node, std::function<void (T) > paction) {bool Headflag = node = = M_phead; node<t>* result = NULL; T Tempvalue = Node->value;node->value = Null;if (paction! = NULL) paction (Tempvalue); if (This->m_len! = 1) {NODE&L T t>* prev = node->pprev; node<t>* next = Node->pnext;prev->pnext = Next;next->pprev = Prev;result = Node->pnext;} Delete Node;this->m_len--;if (headflag) {m_phead = result;} return result;} Template<class t>node<t>* LIST&LT;T&GT;::moveto (T value) {if (M_phead = = null) return null; Node<t>*p = m_phead;do{if (P->value = = Value) {return p;} Else{p = P->pnext;}} while (p = = null? false:p! = this->m_phead); return null;} Template<class t>node<t>* list<t>::movetoat (int index) {if (M_phead = = NULL &&this->m_len <= index) return NULL; Node<t>*p = M_phead;int Tempindex = -1;do{tempindex++;if (index = = tempindex) {return p;} Else{p = P->pnext;}} while (p = = null? false:p! = this->m_phead); return null;} Template<class t>void List<t>::movenext (node<t>* last,std::function<node<t>* (Node<T >*) > Paction) {if (last = = NULL | | paction = = NULL) return; Node<t>*p = Last;do{p = Paction (p);} while (p = = NULL? false:p! = last); Template<class t>int list<t>::getlength () {return this->m_len;}; Template<class T>bool List<t>::isempty () {return this->getlength () = = 0;}; Template<class T>bool List<t>::insert(T value) {node<t> * temp = This->newnode (null, value); if (This->m_phead = = NULL)//head is empty {Temp->pprev = temp; This->m_phead = temp;} else{node<t> * p = this->m_phead;p = P->pprev;p->pnext = Temp;temp->pprev = p;this->m_pHead-> Pprev = temp;} Temp->pnext = M_phead;this->m_len++;return true;}; Template<class t>bool list<t>::remove (t value, std::function<void (t) > paction) {Node<T>*p = This->moveto (value); if (P! = NULL) {This->deletenode (P, paction);} return p! = NULL; };template<class t>bool list<t>::removeat (int index, std::function<void (T) > paction) {Node<T >*p = this->movetoat (index), if (P! = NULL) {This->deletenode (P, paction);} return p! = NULL;}; Template<class t>t list<t>::elementat (int index) {node<t>*p = This->movetoat (value); return p! = null?p->value:null;}; Template<class t>void list<t>::D ispose (std::function<void (T) > paction) {this->moVenext (this->m_phead, [=] (node<t>* p) {return This->deletenode (P, paction);});}; Template<class t>void list<t>::D ispose () {This->dispose ([] (T-t) {});} Template<class t>void List<t>::actionasc (std::function<void (T) > Paction) {this->MoveNext ( This->m_phead,[=] (node<t>* p) {paction (p->value); return p->pnext;});}; Template<class t>void List<t>::actiondesc (std::function<void (T) > Paction) {this->MoveNext ( This->m_phead->pprev, [=] (node<t>* p) {paction (p->value); return p->pprev;});};


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Self-written C + + doubly linked list

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.