C + + in the two-way list, the main implementation (adding and deleting, list inverse, constructors, operator overloading, etc.)
Creating a header file SList.h
#pragma oncetypedef int datatype;class listnode{friend class list;//friend function public: ListNode (const datatype x): _data (x), _prev (null), _next (null) {}private:datatype _data ; listnode* _prev; listnode* _next;}; Class list{public:list (): _head (null), _tail (null) {}//deep copy List (const list& s): _head (null ), _tail (NULL) {listnode* tmp = s._head;while (TMP) {this->pushback (tmp->_data); Tmp = tmp->_next;}} Modern notation list& operator= (list& s) {swap (_head, s._head); swap (_tail, s._tail); return *this;} Public:void clear (); Void printlist (); Void pushback (datatype x); Void popback (); void pushfront (datatype x); Void popfront (); Void insert (listnode* pos, datatype x); void erase (Listnode* pos); Listnode* find (datatype x);//void reverse (); List reverse ();p rivate:listnode* _head; Listnode* _tail;};
implementation of each function
#include <iostream>using namespace std; #include "List.h" #include <assert.h>void list:: Clear ()//Clears doubly linked list {listnode* cur = _head;while (cur) {listnode* del = cur;cur = cur->_next;delete del;del = null;}} Void list::P rintlist ()//print doubly-linked list {listnode* cur=_head;while (cur) {cout << cur->_ data << "--"; cur = cur->_next;} cout << "NULL" &NBSP;<<&NBSP;ENDL;} Void list::P ushback (datatype x)//tail plug {if (null == _head) {_head = new ListNode (x); _tail = _head;} Else{listnode* tmp = new listnode (x); _tail->_next = tmp;tmp->_prev = _tail;tmp->_next = null;_tail = tmp;}} Void list::P opback ()//tail Delete {if (null == _head) {cout << "List is empty! " << endl;} else if (_heAd == _tail) {delete _head;_head = _tail = null;} else{//is easier to find the previous node of the tail node than a single-linked list Listnode* cur = _tail;_tail = cur->_prev;_tail->_next = null;delete cur;cur = null;}} Void list::P ushfront (datatype x)//head Insert {listnode* tmp = _head;_head = new listnode (x); _head->_prev = null;_head->_next = tmp;} Void list::P Opfront ()//head Delete {if (null == _head) {cout << "SList is empty! " << endl;} else if (null == _head->_next) {delete _head;_head = null;} Else{listnode* tmp = _head->_next;delete _head;_head = tmp;tmp->_prev = null;}} Listnode* list::find (datatype x)//Find x{listnode* cur = _head;while (cur) {if (X == cur->_data) Return cur;cur = cur->_next;} return NULL;} Void list::insert (listnode* pos, datatype x)//Insert X{assert (POS);if at the specified location (NULL == pos->_next) List::P ushback (x);else if (_head == pos) List::P Ushfront (x); else{ Listnode* cur = new listnode (x); listnode* prev = pos->_prev;prev->_next = cur;cur->_prev = prev; Cur->_next = pos;pos->_prev = cur;}} Void list::erase (Listnode* pos)//Delete node Pos{assert (POS);if (Null == pos->_next) List:: Popback ();else if (_head == pos) List::P Opfront (); else{listnode* prev = pos- >_prev; listnode* next = pos->_next;next->_prev = prev;prev->_next = next; delete pos;pos = null;}} Inverted doubly linked list//through two pointers, moving from both sides to the middle, clearing the savings content//void list::reverse ()//{//listnode* begin = _head;// listnode* end = _tail;////odd number of nodes when two pointers areAn equal-time end loop, and an even number of nodes when two pointers intersect at the end of the Loop//while (begin != end && begin->_prev != end)//{//swap (begin->_data, end->_data);//begin = begin->_next;//end = end->_prev;//}//}//Exchange the tail-end pointer, exchanging the precursor of each node and subsequent//void list::reverse ()//{//swap (_head, _tail);// listnode* cur = _head;//while (cur)//{//swap (cur->_prev,cur->_next);//cur = cur->_next;//}//}//Create a new linked list and implement List list::reverse () {if (null == _head) {cout by the head interpolation method << "slist is empty!" << endl;} Else if (Null != _head->_next) {list newlist; listnode* cur = _head->_next; listnode* tmp = _head;//Save the head pointer, and after the head is inserted, make its _next pointer to the empty while (cur) {This->pushfront (cur->_data ); cur = cur->_next;} Tmp->_next = null;return newlist;} Return *this;}
This article is from the "Materfer" blog, make sure to keep this source http://10741357.blog.51cto.com/10731357/1748592
C + + doubly linked list "constructors, operator overloading, destructors, additions and deletions, and inversion, etc."