"C + +" single-linked list. cpp

Source: Internet
Author: User

Before, in the C language stage using C to write a single-linked list, easy to understand, then, today using C + + again to write a single-linked list, to compare the difference between the two and similarities and differences:

The following is the CPP implementation code:

SList.h file:

#pragma  oncetypedef int datatype;class slistnode{    friend class  slist;public:    slistnode (datatype x)          :_data (x)         ,_next (NULL)     {} private:    slistnode* _next;    datatype _data;}; Class slist{public:    slist ()         :_head ( NULL)         ,_tail (NULL)     {}     ~slist ()     {        clear ();     }    slist (const slist& s)          :_head (NULL)         ,_tail (NULL)     {          slistnode* cur = s._head;        while (cur )         {             this->pushback (Cur->_data);             cur = cur->_next;        }    The modern notation of  }        //assignment function     SList&  Operator= (slist s)     {        swap (_head,s._ Head);         swap (_tail,s._tail);         return *this;    }    /*   traditional notation for assignment functions      slist& operator= (const slist &s)     {      &nbsP;  if (this != &s)         {             this->clear ();             SListNode* cur = s._head;             while (cur)              {                 this->pushback (Cur->_data);                 cur = cur->_next;             }        }         return *this;    }    */public:     void pushback (datatype x)     {        if (_head == NULL)         {             _head = new slistnode (x);             _tail = _head;        }         else        {             _tail->_next = new slistnode (x);             _tail = _tail->_next;                 }    }     void popback ()     {         if (_head ==&nbsP NULL)   //  null         {             return;        }         else if (_head->_next == null)   //a node          {             delete _head;            _head =  NULL;        }         else   //Multiple Nodes         {             SListNode *cur = _head;             SListNode *prev = NULL;             while (Cur->_next)              {                prev =  cur;                cur  = cur->_next;            }             free (cur);             prev->_next = NULL;         }    }    void pushfront (DataType x)      {        if (_head == null)    //  ①. For empty         {             _hEad = new slistnode (x);             _head->_next = null;        }         else               //②. Not empty         {             slistnode* tmp = new slistnode (x);             tmp->_next = _head;             _head = tmp; //  new head node          }    }    void popfront ()      {        if (_head == null)   //Air          {            return;         }        else if (_head->_next  == null)  //a node         {             delete _head;             _head = NULL;        }         else               //Multiple Nodes         {             SListNode* tmp = _head;             _head = _head->_next;             delete tmp;        }    }     void insert (slistnode* pos,datatype x)     {         assert (POS);         slistnode* tmp  = new slistnode (x);        tmp->_next =  pos->_next;        pos->_next = tmp;         }    slistnode* find (DataType x)      {        if (_head == null)          {            return  Null;        }        else         {            slistnode*  cur = _head;            while (cur)             {                 if (cur->_data == x)                  {                     return cur;                 }                 cur = cur->_ next;            }         }     }    void erase (Slistnode* pos)     {         assert (POS);         ASSERT (_head);         if (_head == pos)          {            _head  = _head->_next;            delete  pos;            return;         }        slistnode* prev =  _head;        while (prev)          {            if (prev->_next ==  pos) &NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;       {                 prev->_next = pos->_next;                 delete pos;                 break;             }             prev = prev->_next;        }     }    void clear ()     {         slistnode* cur = _head;        while (cur)         {             slistnode* del = cur;            cur = cur- >_next;            delete del;         }                 _head = null;        _tail =  null;    }    void printslist ()     {         SListNode* cur = _head;         while (cur)         {             cout<<cur->_data<< ";   "          cur = cur->_next;         }        cout<< "NULL" <<endl;    }private:     slistnode* _head; //pointer to first node     SListNode* _tail;  //pointer to the last node};void test1 () {    slist s1;    s1. Pushback (1);//The S1 is passed to the this pointer     s1. Pushback (2);     s1. Pushback (3);     s1. Pushback (4);     s1. Pushback (5);     s1. Printslist ();     slist s2 (S1);//test copy Construction     s2. Printslist ();     slist s3;    s3 = s2; //test assignment operator overloading     s3. Printslist ();     s1. Popback ();     s1. Printslist ();     s2. Popfront ();     s2. Printslist ();     s3. Insert (S3. Find (3), 6);    S3. Printslist ();     s3. Erase (S3. Find (3));     s3. Printslist ();}

SList.cpp file:

#include <iostream> #include <assert.h>using namespace std; #include "SList.h" int main () {Test1 (); return 0;}


This article from "vs LV" blog, declined reprint!

"C + +" single-linked list. cpp

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.