The underlying operation of a doubly linked list (C + + implementation)

Source: Internet
Author: User

★c++ implementing the underlying operation of a doubly linked list (implementation of the Class)



#include <iostream> #include <cassert>using namespace std;typedef int DataType; class double_link_list{                         //defines a doubly linked list class, including bidirectional precursors and successors, and the initialization of objects to public: Friend class listnode;double_link_list (datatype x = 0): _data (x), _next (NULL), _prev ( NULL) {}private:double_link_list* _prev;double_link_list* _next;datatype _data;}; class listnode{public:        //Note the Friend keyword in the previous category, friend function, Otherwise, the class cannot access the private member of its class ListNode ()      //constructor: _head (null), _tail (null) {}~listnode ()//destructor { Clear ();//Purge function}listnode (const listnode& list)         // Copy constructor {double_link_list *cur = list._head;while  (cur) {pushback (cur->_data);}} listnode operator =  (listnode list)     //assignment operator overloaded function {double_link_list *cur = list._head;while  (cur) {PushBack (cur- >_data);}} public://Void pushback (const datatype& x);//tail delete void popback ();//head plug Void pushfront (const datatype& x);//First delete void popfront ();//Specify location to insert node Void insert (double_link_list*  POS,&NBSP;CONST&NBSP;DATATYPE&AMP;&NBSP;X);//Find node function Double_link_list* find (const datatype& x );//delete node function void erase (const datatype& x);//Inverse list function void reverse ();//Output function Void print () ;p rivate:void clear () {while  (_head) {double_link_list* cur = _head;_head = _ Head->_next;delete cur;}} double_link_list *_head;double_link_list *_tail;};/ /tail plug Void listnode::P ushback (const datatype&x)     //Note the addition of domain modifiers, otherwise you have to define {if in the class body   (_head == null) {_tail = _head = new double_link_list (x); _head->_ Next = nuLl;_head->_prev = null;} Else{_tail->_next = new double_link_list (x); _tail->_next->_prev = _tail;_ Tail = _tail->_next;}} Head plug Void listnode::P ushfront (const datatype&x) {double_link_list* cur = new  Double_link_list (x);if  (_head == null) {_tail = _head = cur;} Else{cur->_next = _head;cur->_next->_prev = cur;_head = cur;}} Tail Delete void listnode::P opback () {if  (_tail) {double_link_list *cur = _tail;_tail =  _tail->_prev;_tail->_next = null;delete cur;}} Head deletion void listnode::P opfront () {if  (_head) {double_link_list *cur = _head;_head =  _head->_next;_head->_prev = null;delete cur;}} Specifies the location of the insertion node Void listnode::insert (double_link_list* pos, const datatype& x) {assert ( _head);d ouble_link_list *cur = _head;while  (cur) {if  (cur == pos) {if  (cur == _tail) {PushBack (x);} Else{double_link_list *tmp = new double_link_list (x); tmp->_next = cur->_ Next;tmp->_prev = cur;tmp->_next->_prev = tmp;cur->_next = tmp;} return;} Cur = cur->_next;}} Finds the specified node Double_link_list* listnode::find (const datatype& x) {double_link_list* cur  = _head;while  (cur) {if  (cur->_data == x) return cur;cur = cur- >_next;} Return null;} Delete a node void listnode::erase (const datatype &x) {double_link_list *cur = _ head;while  (cur) {if  (cur->_data == x) {double_link_list* tmp = cur;cur- >_prev->_next = cur->_next;cur->_next->_prev = cur->_prev;cur =  cur->_next;delete tmp;}}} Inverse doubly linked list void listnode::reverse () {STD:: Swap (_head, _tail);d ouble_link_list* cur = _head;while  (cur) {Std::swap (cur->_ Next, cur->_prev); cur = cur->_next;}} Output doubly linked list Void listnode::P rint () {double_link_list* cur = _head;while  (cur) {cout  << cur->_data <<  "Cur = cur->_next"; cout <<  "NULL" &NBSP;&LT;&LT;&NBSP;ENDL;}



This article from "Warm Smile" blog, declined reprint!

The underlying operation of a doubly linked list (C + + implementation)

Related Article

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.