The blog introduces the C language, as well as C + + 's one-way list. So the two-way list we're introducing today, as the name implies, is that the data itself has two-way pointers to the left and right. The two-way linked list compares one-way linked list, mainly has the following several characteristics:
(1) A bidirectional pointer in the data structure
(2) When inserting the data, you need to consider the direction of the front and rear operation
(3) Similarly, the deletion of data is also necessary to consider the operation of the front and back direction
So what should a non-circular doubly linked list do? We can try it ourselves:
Source:
#include <iostream>using namespace Std;typedef int Datatype;class Linknode{friend class List;public:linknode ( Const datatype& x): _data (x), _next (null), _prev (null) {}~linknode () {}protected:datatype _data;//data private: linknode* _prev;//precursor linknode* _next;//successor};class list{public:list (): _head (null), _tail (null) {}~list () {}list (const list& l) {}list& operator= (const list& L);p ublic:void Print () {Linknode *begin=_head;while (begin) {cout< <begin->_data<< "Begin=begin->_next"; cout<< "NULL" <<ENDL;} void Pushback (const datatype& x) {///1. Empty list//2. One or more nodes if (_head = = NULL) {_head=new linknode (x); _tail=_head;} Else{_tail->_next=new Linknode (x); _tail->_next->_prev=_tail;_tail=_tail->_next;}} void Popback () {//1. Empty list//2. One node//3. Two and above nodes if (_head ==null) {return;} if (_head = = _tail) {Delete _head;_head = Null;_tail = NULL;} else{linknode* del=_tail;_tail=_tail->_prev;_tail->_next=null;//_tail->_prev=_tail->_prev; This sentence function is included in _ tail=_tail-≫_prev, Middle!! Delete Del;/*tail=_tail->_prev;delete _tail->_next;_tail->_next=null;*/}}void PushFront (const DataType & x) {//1. Empty list//2. Non-empty list if (_head = = NULL) {_head=new linknode (x); _tail=_head;} Else{linknode *tmp = new Linknode (x); tmp->_next=_head;_head->_prev=tmp;tmp->_prev = NULL;_head=tmp;}} void Popfront () {//1. Empty list//2. One node//3. Two and above nodes if (_head = = NULL) {return;} if (_head = = _tail) {Delete _head;_head = Null;_tail = NULL;} if (_head! = _tail)//list is emptied after the judgment, otherwise a memory access violation {Linknode *del = (_head) is present; _head=_head->_next;_head->_prev=null;delete del;}} Insert void Insert (linknode* pos, const datatype& x) {//1. Empty list//2. Trailing insert//3. Middle Insert if (_head = = NULL) {_head=new linknode (x); _ Tail=_head;} if (pos = = _tail) {pushback (x);} Else{linknode *tmp=new Linknode (x);p os->_prev->_next=tmp;pos->_next->_prev=tmp;tmp->_prev=pos- >_prev;tmp->_next=pos->_next;}} Find linknode* Find (const datatype& x) {if (_head = = null) {return null;} linknode* begin = _head;while (begin = NULL) {if (beGin->_data = = x) {return begin;} Else{begin = Begin->_next;}} Return null;cout<< "did not find the node" <<ENDL;} Delete void Erase (Linknode* del) {//1. Empty list if (_head = = NULL) {return;} else {//2. A node and is a delete node if ((_head = = _tail) && (_head = = del)) {delete _head;_head=null;_tail=null;} else {//3. Multiple nodes//del as header if (_head = = del) {Popfront ();/*_head=_head->_next;_head->_prev=null;delete _head->_ Prev;*/}else{//del in the tail if (del = = _tail) {popback ();/*del->_prev->_next=null;_tail=del->_prev;delete del;*/}/ /del in Else{del->_prev->_next=del->_next;del->_next->_prev=del->_prev;delete del;}}}} void Remove (DataType * x) {Linknode *ret=find (*x); Erase (ret);} inverse void Reverse () {linknode* tmp1 = _head; linknode* TMP2 = _tail;while (tmp1! = TMP2 && Tmp2->_next! = tmp1) {DataType a = Tmp1->_data; tmp1->_data = Tmp2->_data; tmp2->_data = A;TMP1 = tmp1->_next; tmp2 = Tmp2->_prev;}} private:linknode* _head; Linknode* _tail;}; void Test1 () {List l1;l1. Pushback (1); L1. Pushback (2); L1. Pushback (3); L1. Pushback (4); L1. Print (); linknode* ret = L1. Find (4);//cout<< "RET:" <<ret->_data<<endl;//test Failed "Linknode::_data": Unable to access protected member (in " Linknode "class declared in"//l1. Popback ();//l1. Popback ();//l1. Popback ();//l1. Popback ();//l1. Popback ();//l1. Insert (s,3);//l1. Popfront ();//l1. Popfront ();//l1. Popfront ();//l1. Popfront ();//l1. Popfront ();/*l1. Pushfront (0); L1. Print (); *///l1. Erase (ret);//l1. Reverse ();/*datatype x=3;l1.remove (&x); */l1. Print ();} void Test2 () {List l1;l1. Pushfront (1); L1. Pushfront (2); L1. Pushfront (3); L1. Pushfront (4); L1. Print (); L1. Popfront (); L1. Popfront (); L1. Popfront (); L1. Popfront (); L1. Popfront (); L1. Print ();} int main () {Test1 ();//test2 (); GetChar (); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"C + +" doubly linked list