C + + implements common functions of doubly linked list

Source: Internet
Author: User

struct definition Struct node{node (const datatype& d): _next (null), _prev (null), _data (d) {}public:d atatype _data; node* _prev; node* _next;};/ /bidirectional linked list class definition class doulist{friend ostream& operator<< (ostream& os, doulist &AMP;&NBSP;L);p ublic:doulist ()//constructor: _head (null), _tail (null) {}doulist (const doulist& l)//copy construct {if   (l._head == null) {return;} node *cur = l._head; Node *newnode = new node (Cur->_data);_head = newnode;_tail = _head; cur = cur->_next;while  (cur) {newnode = new node (cur->_data); _tail->_ next = newnode;newnode->_prev = _tail;_tail = newnode;cur = cur- >_next;}} Overloads of the Doulist& operator= (doulist l)//= operator {swap (_head,l._head); swap (_tail, l._tail); return  *this;} ~doulist ()//destructor {node* cur = _head;while  (cur) {node* del =&Nbsp;cur;cur = cur->_next;delete del;} _head = null;_tail = null;} Public:void pushback (const datatype& d);//post-insertion void popback ();//After Void pushfront ( Const datatype& d Void popfront ()//Pre-node* find (const datatype& d) ;//Find Void insert (NODE*&NBSP;POS,&NBSP;CONST&NBSP;DATATYPE&AMP;&NBSP;D);//Insert Void bubbsort () in the specified position;// Bubble sort void reverse ();//Reverse Void remove (CONST&NBSP;DATATYPE&AMP;&NBSP;D);//delete the specified element Void removeall ( CONST&NBSP;DATATYPE&AMP;&NBSP;D);//delete all specified elements void erase (node* pos);//delete the specified node Private:node* _head ;//head pointer node* _tail;//tail pointer};ostream& operator<< (ostream& os, doulist&  L)//Overload {node* cur = l._head;while  (cur) of the output operator {os << cur->_data < <  "<=>"; cur = cur->_next;} cout <<  "over"; return os;} Void doulist::P ushback (CONST&NBSp;datatype& d) {Node* newnode = new node (d);if  (_head == NULL) {_ Head = newnode;_tail = _head;} Else{_tail->_next = newnode;newnode->_prev = _tail;_tail = newnode;}} Void doulist::P opback () {if  (_head == null) {return;} if  (_head == _tail) {Delete _head;_head = null;_tail = null;return;} Node* del = _tail;_tail = _tail->_prev;_tail->_next = null;delete  del;} Void doulist::P Ushfront (const datatype& d) {node* newnode = new node (d); if  (_head == null) {_head = newnode;_tail = _head;} Else{newnode->_next = _head;_head->_prev = newnode;_head = newnode;}} Void doulist::P Opfront () {if  (_head == null) {return;} if  (_head == _tail) {delete _head;_head =  Null;_tail = null;return;} Node* del = _head;_head = _head->_next;_head->_prev = null;delete  del;} Node* doulist::find (const datatype& d) {node* cur = _head;while  (cur) {if   (cur->_data == d) {return cur;} Cur = cur->_next;} Return null;} Void doulist::insert (node* pos, const datatype& d) {if  (Pos == NULL ) {return;} Node* newnode = new node (d);if  (Pos != _tail)//Non-tailed node insert {newNode->_next  = pos->_next;newnode->_prev = pos;pos->_next = newnode;} else//tail node Insert {_tail->_next = newnode;newnode->_prev = _tail;_tail = newnode;}} Void doulist::bubbsort () {node* cur = _head; node* end = null;bool flag = true;while  (cur->_next != end  && flag) {FLag = false;while  (cur->_next != end && cur) {if  (cur->_ Data > cur->_next->_data) {Flag = true;swap (cur->_data, cur->_next-> _data);} Cur = cur->_next;} End = cur;cur = _head;}} Void doulist::reverse () {node* cur = _head; node* newhead = null; node* tmp = null;while  (cur) {tmp = cur->_next;cur->_next =  newhead;if  (newhead) {newhead->_prev = cur;} Newhead = cur;newhead->_prev = null;cur = tmp;} Swap (_head, _tail);} Void doulist::remove (const datatype& d) {if  (_head == null)//No node {return;} node* cur = _head;while  (cur) {if  (cur->_data == d) {if  (cur ==  _head)//First equal {Popfront (); return;} else if  (cur == _tail&& _head != _tail)//Last node equal {popback (); return;} else//Other Nodes {Cur->_prev->_next = cur->_next;cur->_next->_prev = cur->_prev ;d Elete cur;return;}} Cur = cur->_next;}} Void doulist::removeall (const datatype& d) {Node* cur = _head;bool flag  = true;while  (cur) {flag = true;if  (cur->_data == d) {flag =  false;cur = cur->_next; Remove (d); if  (flag) {Cur = cur->_next;}}} Void doulist::erase (Node* pos) {if  (Pos == null)//{return;} if  (pos == _head)//Delete first node {popfront (); return;} if  (pos == _tail)//Delete last node {popback (); return;} Deletion of non-tailed nodes pos->_next->_prev = pos->_prev;pos->_prev->_next = pos->_next;}


C + + implements common functions of doubly linked 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.