#pragma once//header file Using namespace std;typedef int datatype;class listnode{friend class DoubleList;private:DataType _data; listnode* _next; Listnode* _prev;public:listnode ();}; class doublelist{private:listnode* _phead; Listnode* _ptail;public:doublelist ();D oublelist (const doublelist& list); ~DoubleList (); void pushback (const datatype x); Void popback (); Void insert ( listnode* pos,const &NBSP;DATATYPE&NBSP;X); Void erase (Listnode* pos); Listnode* find (const datatype x); Void reverse (); Void getdata (); Void Destroy (); Void operator = (const doublelist& list);}; #include <iostream>//function file # include "DoubleList.h" #include <assert.h>using namespace std; Listnode::listnode (): _next (null), _prev (null) {}doublelist::D oublelist (): _phead (New listnode), _pTail ( _phead) {_PHEAD->_NEXT&NBSP;=&NBSP;_PHEAD;_PHEAD->_prev = _phead;} Doublelist::D oublelist (const doublelist& list) {_phead = new listnode; listnode* head = _phead;_ptail = _phead; listnode *tmp = list._phead->_next;while (tmp != list._phead) {Head->_next = new ListNode;head->_next->_data = tmp->_data;_pTail = head-> _next;_ptail->_prev = head;_ptail->_next = _phead;_phead->_prev = _ Ptail;head = head->_next;tmp = tmp->_next;}} Doublelist::~doublelist () {while (_ptail!=_phead) {listnode* del = _ptail;_ptail = _ptail->_prev;delete del;} Delete _ptail;} Void doublelist::P ushback (const datatype x) {listnode* tmp= new listnode;tmp-> _prev = _ptail;_ptail->_next= tmp;tmp->_data = x;_ptail = tmp;tmp- >_next = _phead;_phEad->_prev = tmp;} Void doublelist::P opback () {listnode* del = _ptail;if (_ptail!=_phead) {_pTail = _ptail->_prev;_ptail->_next = _phead;} Delete del;} Void doublelist::insert ( listnode* pos, const datatype x) {assert (POS); listnode* tmp = new listnode;tmp->_prev = pos;tmp->_next = pos- >_next;pos->_next = tmp;tmp->_next->_prev = tmp;tmp->_data = x;_ Ptail = _phead->_prev;} Void doublelist::erase (Listnode* pos) {assert (POS); listnode *del = pos;if (_ptail != _phead) {pos->_next->_prev = Pos->_prev;pos->_prev->_next = pos->_next;} Delete del;} Listnode* doublelist::find (const datatype x) {listnode* tmp = _phead->_next; while (Tmp!=_phead) {if (tmp->_data == x) {RETURN&NBsp;tmp;} Tmp = tmp->_next;} Return null;} Void doublelist:: getdata () {listnode* tmp = _phead->_next;while (Tmp!=_pHead) {cout << tmp->_data << "--"; tmp = tmp->_next;} cout << "NULL" &NBSP;<<&NBSP;ENDL;} Void doublelist::reverse () {listnode* tmp = _phead->_next; Listnode* newhead = _phead;do{newhead->_next = newhead->_prev;newhead->_prev = tmp;newhead = tmp;tmp = newhead->_next;} while (newhead != _phead); _ptail = _phead->_prev;} Void doublelist::D Estroy () {this->~doublelist ();} void doublelist:: operator = (const doublelist& list) {_pHead = new ListNode; listnode* head = _phead;_ptail = _phead; listnode *tmp = list._phead->_next;while (tmp != list._pHead) {Head->_next = new listnode;head->_next->_data = tmp->_data;_ptail = head->_next;_ptail->_prev = head;_ptail->_next = _phead;_phead->_ Prev = _ptail;head = head->_next;tmp = tmp->_next;}} #include <iostream>//main function test file # include "DoubleList.h" using namespace std;void Test1 () {doublelist s1;s1. Pushback (1); S1. Pushback (2); S1. Pushback (3); S1. Pushback (4); S1. GetData ();cout << (S1. Find (1)) << endl;s1. Insert ((S1. Find (4)), 5); S1. GetData ();D oublelist s2;s2. GetData (); S1. Reverse (); S1. GetData (); s2 = s1;s2. GetData ();} Int main () {test1 (); return 0;}
C + +, two-way circular linked list