Doubly linked list (C + + implementation)

Source: Internet
Author: User

There are many similarities between a doubly linked list and a single linked list, but there are also differences. The main difference between a doubly linked list and a single linked list is that the doubly linked list can traverse from two directions, but the single-linked list is only traversed from the head node to the tail node, not the head node from the tail node, and for some insertions and deletions in the list, the doubly linked list is simpler than the single-linked list. Therefore, the doubly linked list has its special meaning.


--The following is the basic function of a doubly linked list through C + +.

#include  <assert.h>//doubly linked list typedef int datatype;class listnode     Node Structure {    friend class list;public:    listnode (DataType &NBSP;X)     //Constructors        :D ata (x)         , _next (NULL)        , _prev (NULL)      { }    private:    DataType Data;     listnode * _next;      //Store the address of the next node      listnode * _prev;      //Store the address of the previous node};class list{ Public:    list ()      //non-parametric constructors        :_head (NULL)       , _tail (NULL)     { }        &Nbsp;~list ()    //destructors     {         Clear ();      }     public:     void  pushback (datatype x)     //End plug      {          if  (_head == null)           {            _head = _ Tail = new listnode (x);         }          else         {              ListNode * tmp =  New listnode (x);              _tail->_ next = tmp;             tmp->_prev = _tail;              tmp->_next = NULL;              _tail = tmp;          }     }           void popback ()       //     tail deletion  {         //Consider the case of no nodes, one node, multiple nodes           if  (_head == _tail)           {             if  (_head)             {                 delete _head;                 _head = _tail = NULL;              }         }           else          {             listnode * cur  = _tail->_prev;              delete _tail;             _tail =  cur;             _tail->_next =  NULL;          }        }   &nbsP;        void pushfront (datatype x)      Head Plug       {          listnode  * tmp = new listnode (x);           _head->_prev = tmp;          tmp->_next  = _head;          tmp->_prev = NULL;           _head = tmp;       }             void popfront ( )      //Head Delete       {           if  (_head)           {               listnode * tmp = _head;               _head = tmp->_next;               delete tmp;           }       }             void insert (Listnode * pos, datatype &NBSP;X)      //Insert node at POS location       {           //considering the value range of POS            assert (POS);          if  (Pos == _tail)           {             &Nbsp; pushback (x);          }           else         {               ListNode * cur =  pos;              listnode *  next = pos->_next;               listnode * tmp = new listnode (x);               cur->_next = tmp;               tmp->_prev = cur;               tmp->_next = next;               next->_prev = tmp;           }      }            listnode * find (datatype x)      //find       {          ListNode * cur =  _head;          while  (cur)           {              if  (cur->data == x)             {                 return  cur;            }             cur = cur->_next;         }          return NULL;     }           void erase (Listnode * pos)       //Delete Data at the POS location      {          if  (Pos == _head)         {             popfront ();         }          else if  (Pos == _tail)          {             Popback ();         }          else        {             listnode * tmp = pos;             ListNode * cur = pos->_prev;             ListNode * next = pos->_next;             cur->_next = next;             next->_prev = cur;                         /*tmp- >_prev->_next = tmp->_next;               tmp->_next->_prev = tmp->_prev;*/             delete tmp;        }     }           void reverse ()        / /inverse doubly linked list      {         listnode *  begin = _head;         listnode * end  = _tail;         while  (begin != end  && begin->_prev != end)         {             swap (Begin->data, end->data);             begin = begin->_next;             end = end->_prev;          }                   /*ListNode * cur = _head;            swap (_head, _tail);            while  (cur)            {                swap (cur->_prev, cur- >_next);               cur  = cur->_prev;           }*/      }          void printlist ()       //format Output     {        listnode  * cur = _head;        while  (cur)        {            cout << cur->Data <<   " ";            cur = cur->_next ;        }        cout  << endl;    }         void  Clear ()    //Release all nodes      {          ListNode * cur = _head;          while  (cur)         {             ListNode * del = cur;             cur = cur->_next;             delete del;            }     }    private:    ListNode * _head;     ListNode * _tail;};


This article from "Unintentional persistent" blog, declined reprint!

Doubly linked list (C + + implementation)

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.