Data Structure Foundation (9)--the design and implementation of single linked list (2) Advanced Operation __ Data structure

Source: Internet
Author: User

links to Linked lists:

After linking all the contents of the second list to the first list, the full implementation code and resolution are as follows:

[CPP] View plain copy//linked list    template <typename type>   Void mylist<type >::concatenate (const mylist<type> &list)    {        if  (IsEmpty ())//If your own list is empty        {            first = list.first;            return ;       }       else if  (List.isEmpty ())     //If the second list is empty        {            return ;       }           Node<Type> *endNode = first->next;        //Find the end node of the first linked list        while  (endnode->next !=  NULL) &NBsp;      {           endnode =  endNode->next;       }          // Find the first true element of the second list        Node<Type> *secondListNode =  ( List.first)->next;       //Note:  need to copy the element values from the second list         //cannot link the header of the second list directly to the footer        //of the first linked list otherwise an error occurs when the destructor reclaims memory (that is: Same memory release two times)        while  (secondlistnode != null)         {           node<type> *newnode  = new Node<Type> (secondlistnode->data);            newNode->next = NULL;            endnode->next = newNode;              //Two linked list at the same time forward            endNode = endNode->next;            secondListNode = secondListNode->next;        }  }  

the reversal of a linked list:

Basic idea:

Iterate through the list, using a secondary pointer (here, pointer R), storing the next element that the current pointer points to during the traversal, and then reversing the pointer of the current node element and then continuing through the stored pointer to the back.[CPP] View plain copy//list reversal    template <typename type>   Void mylist<type >::invort ()    {       if  (!isempty ())         {           //p The first real node to the forward linked list             //Subsequently,  p will also traverse the positive direction to the end of the list             Node<Type> *p = first->next;               //q will become the first true node of the inverted             //first set Q to null:  to ensure that after the reverse            //the last element's pointer field points to NULL ,  to indicate the end of the list            node<type> *q =  NULL;           while  (p != null)             {                node<type> *r = q;  //temporary Q node currently pointing                 //q back (Forward backward)                 q = p;                //p forward (along forward),  guarantee P can always lead q a position                 p = p -> next;                //reverses the pointer reverse                 //Note: It is important to ensure that this statement runs after the p pointer moves,                //otherwise p will not go ... (because Q changes the direction of the pointer)                q -> next = r;            }              //at this time Q becomes the first true element of the reverse list            //But to maintain the previous first pointer to a useless node (so that the previous operation does not error)             //So we need to point the pointer field to q            first->next = q;       }  }   

linked list printing:

Overloaded mylist << operator, outputting all elements of a list for testing purposes[CPP] View plain Copy//display all data in the list (test)    template <typename type>   ostream  &operator<< (ostream &os, const mylist<type> &list)    {        for  (node<type> *searchnode = list.first - > next;               searchnode  != NULL;                Searchnode = searchnode -> next)        {            os << searchNode -> data;           if  (Searchnode -> next != null)  //has not yet reached the end of the list                cout  <<  " ->  ";       }          return  os;  }  

Attach-Test code:[CPP] View plain Copy Int main ()    {       cout <<  "-------- ---- 1 ------------" << endl;       MyList<int>  first;       for  (int i = 0; i < 5;  ++i)        {            First.insert (i+1, i+1);       }        First.remove (5);          MyList<int> second;       for  (int i = 0; i < 5; ++i)        {           second.insert (i+6, i +1);       }       second.insertfront (5);      &Nbsp; second.insert (88, 7);          cout <<  " Before concatenate ... " << endl;       cout <<   "first: "  << first << endl;       cout  <<  "second: "  << second << endl;          cout <<  "After concatenate ..."  << endl;        first.concatenate (second);       cout <<   "first: "  << first << endl;       cout  <<  "second: "  << second << endl;             cout <<  "\------------ 2 ------------"   << endl;       MyList<char> chList;        for  (char ch =  ' 0 '; ch <=  ' 9 ';  ++ ch)         {           chlist.insertfront (CH);        }       cout <<  "Before invort ... " << endl;       cout << chList <<  endl;          cout <<  "After invort ..."  << endl;       chlist.invort ();        cout << chList << endl;           cout <<  "After remove (' 5 ') ..."  << endl;        chlist.remOve (' 5 ');       cout << chList << endl;           cout <<  "\------------ 3 ------------"  << endl;       MyList<double> dList;       dlist.insert (1.1, 1);       dlist.insertfront (2.2);        cout << dList << endl;           return 0;  }  



Original address: http://blog.csdn.net/zjf280441589/article/details/42430075

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.