Data Structure Foundation (11)--design and implementation of circular chain list

Source: Internet
Author: User

Circular linked list: The pointer of the last node pointer field also refers back to the first node of the linked list;

The difference between a circular single-linked list and a single-linked list is that the pointer to the most node in the table is no longer null, but instead points to the head node (and therefore to our original mylist slightly modified) so that the entire list forms a ring.

Therefore, the empty condition of the circular single-linked list is no longer the pointer of the head node is empty, but whether he is equal to the head node;

In fact, if only the simple implementation of the chain list of single-link performance improvement is not obvious, but to increase the complexity of the code implementation, but if the next two-way linked list in combination, the speed of the promotion is very amazing, so this blog right as a transition bar, The combination of the previous blog and the next blog play a connecting role.

The following is the complete code and parsing MyList.h, because the code is more, I hope to be able to read carefully, but in fact, most of the following code is similar to the previous only a few of them slightly modified, encountered with the single-linked list of differences, I will be with the + + symbol as a comment point:

#ifndef mylist_h_included#define mylist_h_included#include <iostream> #include <stdexcept>using namespace std;//loop link list//forward declaration template <typename Type>class mylist;template <typename type>class listiterator ;//List node template <typename type>class node{//You can use the MyList class as a friend of node//You can also make the node class MyList nested class, nested in MyList, or you can complete the    function Friend Class mylist<type>;    Friend Class listiterator<type>; Template <typename t> friend Ostream &operator<< (ostream &os, const mylist<t> &list);p Riv    Ate://constructor Description://next = NULL;  Because this is a newly generated node, the next node is the null node (const Type &datavalue):d ata (DataValue), Next (NULL) {} Type data; Data field: node data nodes *next; Pointer field: Next node};//list template <typename type>class mylist{template <typename t> friend Ostream &operato    r<< (ostream &os, const mylist<t> &list);    Friend class listiterator<type>;p ublic:mylist ();    ~mylist (); Inserting an elementTable header void Insertfront (const Type &data);    Inserts an element into position index (index starting at 1) void Insert (const Type &data, int index);    Remove all nodes in the table that have values of data of void Remove (const Type &data); BOOL IsEmpty () Const;private://Pointer to the first node node<type> *first;};    Template <typename type>mylist<type>::mylist () {//first points to an empty node first = new node<type> (0); + + This is a key point. The next element of the//first points to first//At this point, the judgment that indicates whether the linked list has reached the end is no longer (equal to NULL)//And instead (equals first)//because at this point it represents the last of the linked list element//At the same time, first is also the previous element of the second element-------next = primary;}    Template <typename type>mylist<type>::~mylist () {node<type> *deletenode = NULL;    + + Save chain footer element node<type> *tmp = first;    + + first points to the first real element, firstly = first->next;        All the way to the end of the list while (first! = tmp) {deletenode = first;        First = next;    Delete Deletenode; }//+ release to the list of empty nodes delete tmp;} This step is the same as the previous version of the linked list template <typename type>void MYLIST&LT;TYPE&GT;::insertfront (const Type &data) {node<type> *newnode = new node<type> (data);    NewNode next = First and next; First--next = NewNode;} Template <typename type>void mylist<type>::insert (const Type &data, int index) {//Because we added an empty node to the table header//    So if the list is empty, or the element is added at the 1 position of the list//its operation is the same as adding an element in another location int count = 1;    At this time Searchnode certainly not for first node<type> *searchnode = first; + + NOTE: This will change the null to first//find the location to insert//If the given index is too large (over the length of the list)//The element is inserted into the list footer//reason is searchnode->next! = Fir        St This condition has not satisfied the while (Count < index && Searchnode->next! = First) {+ + count;    Searchnode = searchnode->next;    }//Insert list node<type> *newnode = new node<type> (data);    Newnode->next = searchnode->next; Searchnode->next = NewNode;}    Template <typename type>void mylist<type>::remove (const Type &data) {if (IsEmpty ()) return; node<type> *previous = fiRst            Save the previous node for the node you want to delete for (node<type> *searchnode = first->next; Searchnode! = NULL;     + + Note here is no longer a judge of whether null searchnode! = First;        + + is not equal to first, first represents the end of the list Searchnode = Searchnode->next) {if (Searchnode->data = = data)            {Previous->next = searchnode->next;            Delete Searchnode; Readjust the Searchnode pointer//Continue traversing the list to see if there is an equal element//+ + NOTE//If the current Searchnode has reached the last node//Also            Searchnode->next is already equal to first, the following statement cannot execute if (Previous->next = = first) break;        Searchnode = previous->next;    } previous = Searchnode; }}//Note Empty conditions template <typename type>bool mylist<type>::isempty () const{return first->next = = First;} Show all data in a linked list (test) template <typename type>ostream &operator<< (ostream &os, const MYLIST&LT;TYPE > &list) {for (node<type> *searchnode = LiSt.first, Next;   Searchnode! = List.first;        + + Note Searchnode = Searchnode next) {OS << searchnode-data;    if (searchnode next! = List.first)//+ + note (not yet reached the end of the list) cout << ","; } return OS;} Listiterator In addition to the empty condition of the null function, there is no change to the template <typename type>class listiterator{public:listiterator (const mylist<type> &_list): List (_list), CurrentNode ((_list.first)->next) {}//overloaded *operator con    St Type &operator* () const throw (Std::out_of_range);    Type &operator* () throw (Std::out_of_range);    Overload->operator Const node<type> *operator-> () const throw (Std::out_of_range);    Node<type> *operator-> () throw (Std::out_of_range);    Overload ++operator Listiterator &operator++ () throw (Std::out_of_range);    Note: The value returned here is not reference listiterator operator++ (int) throw (std::out_of_range); BOOL IsEmpty () Const;private:coNST mylist<type> &list; Node<type> *currentnode;}; Template <typename type>bool listiterator<type>::isempty () const{//+ + NOTE: The empty condition is changed to List.first if (currentn    Ode = = List.first) return true; return false;}    Template <typename type>const Type &listiterator<type>::operator* () Constthrow (Std::out_of_range) {    if (IsEmpty ()) Throw Std::out_of_range ("iterator is out of range"); Returns the content pointed to by the current pointer return currentnode->data; Template <typename Type>type &listiterator<type>::operator* () throw (Std::out_of_range) {//First for *    This adds a const property,//To call the const version of the function,//And then uses const_case,//To remove the Non-const version of//operator-> () from the Const property that the function call takes with this similar Return Const_cast<type &> (static_cast<const listiterator<type> &> (*this). ope Rator* ());} Template <typename type>const node<type> *listiterator<type>::operator-> () Constthrow (std::out _of_range) {if (IsEmpty ()) Throw Std::out_of_range ("iterator is out of range"); Return directly to the pointer return currentnode;} Template <typename type>node<type> *listiterator<type>::operator-> () throw (Std::out_of_range) {//See return const_cast<node<type> *> (Static_cast<const listiterator<type&gt ; > (*this) .operator-> ());} Template <typename type>listiterator<type> &listiterator<type>::operator++ () throw (std::out_    Of_range) {if (IsEmpty ()) Throw Std::out_of_range ("iterator is out of range");    Pointer move forward CurrentNode = currentnode->next; return *this;} Template <typename type>listiterator<type> listiterator<type>::operator++ (int) throw (std::out_of    _range) {listiterator tmp (*this); + + (*this); Call the previous + + version of return TMP;} #endif//mylist_h_included

Attached-Test code:

int main () {    mylist<int> imylist;    for (int i = 0; i < ++i)    //1 2 3 4 5 6 7 8 9        Imylist.insert (i+1, i+1);    for (int i = 0; i < 5; ++i)     //40 0 1 2 3 4 5 6 7 8 9        Imylist.insertfront (i*10);    Imylist.insertfront (+);//100 0 1 2 3 4 5 6 7 8 9    Imylist.remove (ten);      0 1 2 3 4 5 6 7 8 9    imylist.remove (8);       0 1 2 3 4 5 6 7 9    cout << "------------MyList------------" << Endl;    For (listiterator<int> iter (imylist);            ! (Iter.isempty ());            + + iter)        cout << *iter << ';    cout << Endl;    cout << "Test: \n\t" << imylist << Endl;    return 0;}

Data Structure Foundation (11)--design and implementation of circular chain 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.