C + + templates implement a doubly linked list

Source: Internet
Author: User

#include <iostream> #include <string>using namespace std;template<typename T> Struct node{     node (const t& d)           :_data (d)          ,_next (NULL)           , _prev (NULL)      {}      T _data;     Node<T>* _next;      Node<T>* _prev;}; Template<typename t>class dlist{public:     dlist ();      ~dlist ();      dlist (const dlist<t>& list);      dlist<t>& operator= (const dlist<t> list);      void pushback (const t& d);      void  Popback ();      void pushfront (const t& d);      void popfront ();      node<t>* find (const T& d);      void insert (node<t>* pos, const t& d);      void reverse ();      void sort ();      void remove (const t& d);      void removeall (const &NBSP;T&AMP;&NBSP;D);      void erase (Node<t>* pos);      friend ostream& operator<<<T> (ostream& output, const  Dlist<t>& s);p rivate:     node<t>* _head;      Node<T>* _tail;}; Template<typename t>    dlist<t>::D list ()      :_head (NULL)     ,_tail (NULL)     {}template< Typename t>dlist<t>::~dlist () {     node<t>* cur =  _head;     while  (cur)      {           Node<T>* del = cur;           cur = cur->_next;           delete del;     }}template<typename t> dlist<t ::D list (const dlist<t>& list)     :_head (NULL)      ,_tail (NULL) {         node<t>* cur =  list._head;         while  (cur)     {        Pushback (Cur->_data);      cur = cur->_next;      }}template<typename t>dlist<t>& dlist<t>::operator= (const  Dlist<t> list) {     swap ((node<t>*) _head,  (Node<T>*) list. _head)      swap ((node<t>*) _tail,  (node<t>*) list._tail);      return *this;} Template<typename t>void dlist<t&gt::P ushback (const t& d) {      if  (_head == null)      {           _head = new Node<T> (d);           _tail = _head;     }      Else     {         &nbSp Node<t>* newnode = new node<t> (d);           _tail->_next = NewNode;           newnode->_prev = _tail;          _tail  = newnode;     }}template<typename t>void dlist<t>:: Popback () {     if  (_head == null)      {           return;     }      else if  (_head== _tail)      {           delete _head;           _tail = NULL;          _head =  null;     }     else     {           _tail = _tail->_prev;           delete _tail->_next;          _ Tail->_next = null;     }}template<typename t>void dlist <t&gt::P ushfront (const t& d) {     node<t>* newnode  = new node<t> (d);     if  (_head == null)       {          _head = NewNode;           _tail = _head;           return; }     NewNode->_next =  _head;      _head->_prev = newnode;     _head = newnode;} Template<typename t>void dlist<t>::P Opfront () {     if  (_ Head == null)       return;     else if   (_head==_tail)      {           delete _head;          _head = NULL;           _tail = NULL;       }     else     {           _head = _head->_next;           delete _head->_prev;     }}template<typename T> ostream& operator<< (ostream& output, const dlist<t>& s) {     node<t>*  cur = s._head;     while  (cur)      {       cout<< cur->_data <<  "<->";       cur = cur->_next;     }      output <<  "over";      return output;} Template<typename t>node<t>* dlist<t>::find (const t& d) {      Node<T>* cur = _head;     while  (cur)     {          if  (Cur->_data &NBSP;==&NBSP;D)            return cur;           cur = cur->_next;     }     return  NULL;} Template<typename t>void dlist<t>::insert (node<t>* pos, const t &AMP;&NBSP;D) {     if  (pos == null)        return;     Node<T>* NewNode = new Node<T> (d);      if  (Pos == _tail)      {           _tail->_next = NewNode;           NewNode->_prev = _tail;           _tail = NewNode;     }      else     {          newnode- >_next = pos->_next;          newnode->_prev = pos;           pos->_next = NewNode;           NewNode->_next->_prev = NewNode;      }}template<typename t>void dlist<t>::reverse () {     node <T>* cur = _head;     while  (cur)       {          swap (Cur->_next, cur->_prev);           cur = cur->_prev;      }     swap (_head, _tail);} Template<typename t>void dlist<t>::sort () {     node<t>*  cur = _head;     node<t>* end = null;     while  (cur != end)       {      while  (Cur && cur->_next  != end)       {            if  (Cur->_data > cur->_next->_data)             {            T  Tmp = cur->_data;            cur-> _data = cur->_next->_data;             cur->_next->_data = tmp;            }           cur = cur->_next;      &nbSP;}       end = cur;      cur = _ Head;     }}template<typename t>void dlist<t>::remove (const &NBSP;T&AMP;&NBSP;D) {     node<t>* cur = _head;      while  (cur)      {         Node<T>* del = cur;      if  (cur->_data  ==&NBSP;D)       {       if  (cur ==  _head)        {             _head = _head->_next;             _head->_prev = NULL;       }        else if  (Cur!=_head && cur==_tail)        {             _tail = _tail->_prev;             _tail->_next = NULL;        }       else        {            cur->_ prev->_next = cur->_next;       }        delete del;       break;       }      cur = cur->_next; }}template<typename  t>void dlist<t>::removeall (const t& d) {     node <t>* cur = _head;     while  (cur)      {       node<t>* del = cur;      if  (cur->_data == &NBSP;D)       {       if  (cur ==  _head)        {             _head = _head->_next;             _head->_prev = NULL;             cur = _head;       }        else if  (Cur != _head && cur == _tail)         {            _ Tail = _tail->_prev;            _tail->_next = null;             cur = NULL;        }       else        {            cur->_prev->_next  = cur->_next;            cur->_ next->_prev = cur->_prev;             cur = cur->_next;       }        delete del;      }  else       cur = cur->_next; }}template<typename t>void dlist<t>:: Erase (Node<t>* pos) {     if  (pos == null)       return;      if  (Pos == _head && pos == _tail)       {          _head = NULL;           _tail = NULL;           return;     }     if  (POS  == _head)      {           _head = _head->_next;          _head-> _prev = null;     }     else if  (POS  == _tail)      {           _tail = _tail->_prev;          _tail->_next = null;      }     else     {           pos->_prev->_next = pos->_next;           pos->_next->_prev = pos->_prev;      } delete pos;}

C + + templates implement a doubly linked list

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.