#include <iostream>using namespace std;typedef int datetype;struct listnode {DateType _date; ListNode* _next; //precursor pointer listnode* _prev; //rear drive pointer ListNode (DATETYPE&NBSP;X) //initialization of the node: _date (x), _next (null), _prev (null) {}};class list{public: list () //constructor: _head (null), _tail (null) {}~List ( ) //destructor {Clear ();} Void pushback (datetype x) //tail interpolation {if (_head == null) {_head = _tail = new listnode (x);} Else{listnode* tmp = new listnode (x); _tail->_next = tmp;tmp->_prev = _tail;_tail = tmp;}} Void clear () //empty linked list {Listnode* cur = _head;while (cur) {listnode* d El = cur;cur = cur->_next;delete del;}} Void printflist () //Print List {listnode* cur = _head;while ( cur) {cout << cur->_date << "--"; cur = cur->_next;} cout << "null " &NBSP;<<&NBSP;ENDL;} Void popback () //tail {if (_head==null) {return;} else if (_head->_next==null) { delete _head; _head = _tail = NULL;} else { listnode* cur = _tail->_prev; delete _tail; _tail = cur; cur->_next = null; }}void pushfront (DateType x) //head interpolation method {if (_head == null) {_head = _tail = new listnode (x);} Else{listnode* tmp = new listnode (x);tmp->_next = _head;_head->_prev = tmp;_head =&nbSp;tmp;}} Void popfront () //head departure {if (_head == _tail) {if (_head) { Delete _head;_head = _tail = null;}} Else{listnode* cur = _head->_next;delete _head;_head = cur;cur->_prev = null;}} Void insert (listnode* pos, datetype x) //inserts a number {if (_ Head == _tail) {if (_head) {listnode* tmp = new listnode (x);p Os->_next = tmp;tmp->_prev = pos;} Else{_head = _tail = new listnode (x);}} else if (pos->_next == null) {listnode* tmp = new listnode (x); tmp- >_next = null;tmp->_prev = pos;pos->_next = tmp;} Else{listnode* tmp = new listnode (x); tmp->_next = Pos->_next;pos->_next->_prev = tmp;tmp->_prev&nbSp;= pos;pos->_next = tmp;}} Listnode* find (datetype x) //looks for a number and returns the address of that node {listnode* cur = _head; while ( (cur->_date != x) && (cur != null) ) {cur = cur->_next;} Return cur;} Void erase (listnode* pos ) //Delete a node {if (!pos) {return;} else if (pos->_prev == null) {if (pos->_next) {listnode* next = Pos->_next; delete pos;next->_prev = null;_head = next;} Else{pos->_next = null;pos->_prev = null;delete pos;}} else if (pos->_next == null) {listnode* prev = pos->_prev;delete Pos;prev->_next = null;} else{listnode* prev = pos->_prev; Listnode* next = pos->_next;delete pos;prev->_next = next;next->_Prev = prev;} }private:ListNode* _head; listnode* _tail;};
This article is from the "0-point Time" blog, please make sure to keep this source http://10741764.blog.51cto.com/10731764/1747567
Implementation of basic functions in doubly linked list