C + + bidirectional cyclic link list implementation

Source: Internet
Author: User

bidirectional cyclic linked list C + + implementation

1. Single Linked list:

Structure diagram:

2. Doubly linked list:

3. Two-way loop linked list:

For this program, it is given a _head head node, not a pointer, because it is more convenient to avoid some empty judgment problems

/*Copyright information: Wolf file name: BidCirList.h file ID: File Summary: Use C + + to achieve a simple two-way list function. increase, delete, check, change//too annoying. I give a direct to the table//swap counter switch is already a force-giving method. Just try current version: 1.1 Author: Wolf Finish time: 2015-12-13*/#ifndef _bidcirlist_h#define_bidcirlist_h#include"afxstd.h"typedefintDatatype;typedefstructlistnode{ListNode (DataType x=0): _data (x)//default initialization is self-loop instead of NULL, _prev ( This), _late ( This) {} DataType _data; structlistnode*_prev; structlistnode*_late;} ListNode;classbidcirlist{ Public: Bidcirlist (): _head (0) {} bidcirlist (DataType*array, size_t n =0): _head (0) {size_t i=0;  while(n--) {Insertater (array[i++]); }} bidcirlist (Bidcirlist&list): _head () {ListNode* cur =List._head._prev;  while(cur) {insertater (cur-_data); Cur= cur->_prev; if(cur = = &list._head) Break; }    }    ~bidcirlist () {destoty (); } bidcirlistoperator+ (bidcirlist&list) {bidcirlist tmp (* This); ListNode* cur =List._head._prev;  while(Cur! = &list._head) {tmp. Insertater (cur-_data); Cur= cur->_prev; }        returntmp; } bidcirlist&operator= (bidcirlist&list) {        if( This! = &list)            {bidcirlist S (list);        Swap (S); }        return* This; }    //Empty Space    voidDestoty () {ListNode*cur = &_head;  while(Cur->_prev! = &_head)        {Delprev (); }    }    //deletes the node before the node. Default is Header    voidDelprev (ListNode *del =NULL) {        if(_head._prev = = &_head)return; if(Del = =NULL) {            //before deleting a header_head._prev = _head._prev->_prev; Delete_head._prev->_late; _head._prev->_late = &_head; }        Else{del->_prev = del->_prev->_prev; DeleteDel->_prev->_late; Del->_prev->_late =del; }    }    //after deleting a node, the default is the header .    voidDellate (ListNode *del =NULL) {        if(_head._prev = = &_head)return; if(Del = =NULL) {_head._late= _head._late->_late; Delete_head._late->_prev; _head._late->_prev = &_head; }        Else{del->_late = del->_late->_late; DeleteDel->_late->_prev; Del->_late->_prev =del; }    }    //Insert before node, default to Header    voidInsertater (DataType x, listnode* ins=NULL) {ListNode* TMP =NewListNode (x); if(INS = =NULL) {tmp->_prev = &_head; TMP->_late =_head._late; TMP->_late->_prev =tmp; TMP->_prev->_late =tmp; }        Else{tmp->_prev =ins; TMP->_late = ins->_late; TMP->_late->_prev =tmp; TMP->_prev->_late =tmp; }} ListNode*Find (DataType x) {ListNode* cur =_head._prev;  while(cur) {if(cur = = &_head)returnNULL; if(Cur->_data = =x) {returncur; } cur= cur->_prev; }    }    voidErase (ListNode *node) {        if(node = = &_head) {            return; }        Else{ListNode* TMP =node; Node->_prev->_late = node->_late; Node->_late->_prev = node->_prev; Deletetmp; TMP=NULL; }    }    //Reverse Print    voidPrintprev () {ListNode* cur =_head._prev;  while(cur) {if(cur = = &_head) Break; cout<< Cur->_data <<" -"; Cur= cur->_prev; } cout<<"over!"<<Endl; }    //Forward Printing    voidprintlate () {ListNode* cur =_head._late;  while(cur) {if(cur = = &_head) Break; cout<< Cur->_data <<" -"; Cur= cur->_late; } cout<<"over!"<<Endl; }    voidSwap (Bidcirlist &list) {:: Swap (_head._prev->_late, list._head._prev->_late);        :: Swap (_head._prev, List._head._prev); :: Swap (_head._late->_prev, list._head._late->_prev);    :: Swap (_head._late, list._head._late); }    Private: ListNode _head;};#endif

C + + bidirectional cyclic link list implementation

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.