C + + implements doubly linked list

Source: Internet
Author: User

Doubly linked list

#include <iostream>using namespace std;typedef int datatype;struct node{node ( CONST&NBSP;DATATYPE&AMP;D): _data (d),  _next (null),  _prev (null) {}node*_next; node*_prev;datatype _data;}; class dlist{friend ostream&operator<< (ostream&os, dlist& d);p ublic:dlist (): _head (null),  _tail (null) {}~dlist () {node*cur=_head;while  (cur) {node*del = cur;cur =  cur->_next;delete del;}} Public:void pushback (const datatype&d);//tail plug void popback ();//tail-delete Void pushfront (const &NBSP;DATATYPE&AMP;D);//Head inserted Void popfront ();//head-cut 2private:node*_head; Node*_tail;};o stream&operator<< (ostream&os, dlist& d) {Node* cur = d._head;while   (cur) {cout << cur->_data <<  "<=>"; cur = cur->_next ;} cout <<  "Over" &NBSP;&LT;&LT;&NBSP;ENDL;RETURN&NBSP;OS;} Void dlist::P ushback (const D atatype&d)//tail plug {node*newnode = new node (d);if  (_head == null) {_head =  newnode;_tail = newnode;} Else{_tail->_next = newnode;newnode->_prev = _tail;_tail = newnode;}} Void dlist::P opback ()//tail Delete {if  (_head == null)//No node {return;} if  (_head == _tail)//a node {delete _tail;_head = null;_tail = null; return;} Multi-node _tail = _tail->_prev;delete _tail->_next;_tail->_next = null;} Void dlist::P ushfront (const datatype&d)//head Insert {node*newnode = new node (d);if  (_ Head == null) {_head = newnode;_tail = newnode;} Else{newnode->_next = _head;_head->_prev = newnode;_head = newnode;}} Void dlist::P Opfront ()//First delete {if  (_head == null) {return;} if  (_head == _tail) {delete _tail;_tail = null;_head = null;return;} Node*del = _head;_head = _head->_next;_head->_prev = null;delete del;} Node*dlist::find (CONST&NBSP;DATATYPE&AMP;D)//Find {node*cur = _head;while  (cur) {if  (cur->_ DATA&NBSP;==&NBSP;D) {return cur;} Cur = cur->_next;} Return null;} Void dlist:: insert (NODE*&NBSP;POS,&NBSP;CONST&NBSP;DATATYPE&AMP;D)//Insert {Node*newNode = new  node (d);if  (pos == _tail) {_tail->_next = newnode;newnode->_prev =  _tail;_tail = newnode;} Else{newnode->_next = pos->_next;pos->_next->_prev = newnode;newnode->_prev  = pos;pos->_next = newnode;}}

This article is from the "incomparable Warm yang" blog, please be sure to keep this source http://10797127.blog.51cto.com/10787127/1752034

C + + implements 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.