"C + +" to achieve all the two-way linked list of operations, including inverted double linked list (three methods) __c++

Source: Internet
Author: User
Tags prev

Create source file List.cpp

Include "List.h"

int main ()
{
Test ();
System ("pause");
return 0;
}

Create a header file List.h

#ifndef  __lish_h__ #define &NBSP;__LISH_H__ #include <iostream> using namespace std;

typedef int datatype;
Struct listnode {    listnode (datatype x)     :_next (NULL)

    , _prev (NULL)     , _data (x)     {}
    ListNode* _next;
    ListNode* _prev;
    DataType _data;


}; class list {public:     list ()         :_head ( NULL)         ,_tail (NULL)     {}      list (const list& s)         :_head (NULL)          , _tail (NULL)     {         listnode* cur = s. _head;         while  (cur)          {            this->pushback (cur->_data
);
            cur = cur->_next;         }          List&  operator=  (const list& s)     {         //first delete the node, then insert the node         if  (&s !=  This)         {         
   ListNode* pcur = _head;             while  (pcur)              {                 listnode* del =
 pcur;                 pcur =
 pcur->_next;                 delete 
Del                 del =
 NULL;             }       
      ListNode* cur = s._head;             while  (cur)              {         
       this->pushback (Cur->_data);                 cur = cur->_next;             }       
  }         return *this; &NBSP;&NBSP;&NBSP;&NBSP}     ~list ()     {    
    ListNode* cur = _head;         while  (cur)          {            ListNode* del = 
Cur
            cur = cur->_next;
            delete del;
            del = NULL;         &nbsp}    &nbsp}     //tail-Plug     void pushback ( datatype x)     {        //: 0 nodes    &NBSP;1, multi-node two cases         if  (_head == null)          {            _
Head = new listnode (x);
            _tail = _head;
            _tail->_next = NULL; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}         else          {           
 listnode* cur = new listnode (x);             _tail->_next = cur;
            cur->_prev = _tail;
            _tail = cur;
            _tail->_next = NULL; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}          //tail Delete      void popback ()     {         if  (_head == _tail)         {             if  (_head != null)              {          
      delete _head;                 _head = null;                 _tail =
 NULL;             }              else            
 {                return;             }        &NBSP;&NBSP}         else          {            listnode* prev =
 _tail->_prev;
            delete _tail;             _tail = NULL;
            _tail = prev;
            _tail->_next = NULL; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}    &nbsp}     //header      void pushfront (datatype x)     {         if  (_head == null)         { 
           pushback (x); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}         else          {           
 listnode* index = new listnode (x);             index->_next = _head;
            _head->_prev = index;
            _head = index;
            _head->_prev = NULL; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}         //head deleted      void popfront ()     {        if   (_head == _tail)         {             popback ();                      }          else         {            listnode* del = _head;             listnode* next = _head-
>_next;
            _head = next;
            _head->_prev = NULL;
            delete del;
            del = NULL; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}     }           //Insert Element     void insert (size_t pos, datatype x)      {        //: Is the end-plug     not the tail-plug       Two kinds of situation         listnode* cur = _head;         while  (--pos)          {                          cur = cur->_next;                  }          if  (cur == _tail)         {    
        pushback (x); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}         else          {           
 listnode* index = new listnode (x);            &nbSp
listnode* prev = cur->_prev;
            index->_next = cur;
            cur->_prev = index;
            prev->_next = index;
            index->_prev = prev; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}    &nbsp     //Lookup element      listnode* find (datatype x)     {     
   ListNode* cur = _head;         while  (cur)          {            if  (cur->_data == &NBSP;X) &NBSP;&NBSP;&NBSP;&NBsp;        {          
        return cur;             }       
      cur = cur->_next;         }         return 
NULL; &NBSP;&NBSP;&NBSP;&NBSP}     //Delete element     void erase (ListNode* 
POS)     {        if  (Pos == _head)         {          
  popfront ();         }         else if   (Pos == _tail)          {            popback ();    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}         else          {            
listnode* prev = pos->_prev;             listnode* next = pos-
>_next;
            prev->_next = next;
            next->_prev = prev;
            delete pos;
            pos = NULL; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}     }         &nbsp //Inversion method One: Go from both ends, Exchange data     /*void reverse ()     {  
      ListNode* begin = _head;
        ListNode* end = _tail;         while  (!) ( (begin == end)  | |   (end->_next == begin))         {   
         swap (Begin->_data, end->_data);
            begin = begin->_next;
            end = end->_prev; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}     }*/    // Inverse Method II: The precursor of the switching node and the successor     /*void reverse ()     {        listnode* cur = _head;         while  (cur)          {            swap (cur->_prev, cur-
>_next);
            cur = cur->_prev; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}         swap (_head,
 _tail);     }*/         //Inverse method Three: Pick the node, head plug the node      void reverse ()     {        listnode*
 cur = _head;
        ListNode* newhead = NULL;         while  (cur)          {&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;       listnode* tmp = cur;
            cur = cur->_next;
            if  (Newhead == null)             {      

          newhead = tmp;                 newhead->_
next = null;                 newhead->_
prev = null;                 _head =
 _tail = newhead;             }      &nBsp;      else              {                
newhead->_prev = tmp;                 tmp->_next  = newhead;               
              newhead = tmp;                 _head =
 newhead;                 _head->_
prev = null;             }                  } &NBSP;&NBSP;&NBSP;&NBSP}     //Print     void printlist ()      {        ListNode* cur = _head;          while  (cur)         {             cout << cur->_data 
<<  "->";
            cur = cur->_next;         }         cout 
<<  "NULL"  << endl;
&NBSP;&NBSP;&NBSP;&NBSP) Private:     ListNode* _head;
    ListNode* _tail;


}; Void test () {    List s;     s.pushback (1);    
 s.pushback (2);      s.pushback (3);
    s.pushback (4);
    s.pushback (5);

    s.printlist ();
    s.popback ();

    s.printlist ();
    s.pushfront (0);

    s.printlist ();
    s.popfront ();

    s.printlist ();
    s.insert (2,&NBSP;10);

    s.printlist ();
    s.reverse ();

    s.printlist (); } #endif  //__list_h__


This article from "Han Jing's Blog" blog, please be sure to keep this source http://10740184.blog.51cto.com/10730184/1747880

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.