Pay attention to the test case selection!!
#include <iostream>using namespace std; #include <string>template<class t>struct linknode{linknode (const t& x): _data (x), _prev (null), _next (null) {}t _data; linknode<t>* _prev; linknode<t>* _next;}; Template<class t>class list{public:list (): _head (null), _tail (null) {}list (const List <T>&&NBSP;L): _head (null), _tail (null) {linknode<t>* cur = l._head;if ( Cur == null) {return;} (*this). Pushback (Cur->_data);while (cur&&cur->_next!=l._head) {cur = cur->_next; (*this) . Pushback (cur->_data);}} List<t>& operator= (list<t> l) {swap (_head, l._head); swap (_tail, l._tail);} Void pushback (const t& x) {if (_head == null) {_head = new Linknode<t> (x); _tail= _head;} Else{linknode<t>* tmp = new linknode<t> (x); _tail->_next = tmp;tmp->_prev = _tail;_tail = _tail->_next;_tail- >_next = _head;_head->_prev = _tail;}} Void popback () {destory ();} Void print () {linknode<t>* cur = _head;if (cur == null) {return;} cout << (Cur->_data) << "-";while (cur&&cur->_next!= _head) {cur = cur->_next;cout << (cur->_data) << ",";} cout<< "NULL" &NBSP;<<&NBSP;ENDL;} ~list () {while (_head!=null) { destory ();}} Linknode<t>* find (const t& x) {linknode<t>* cur = _head;if ( Cur == null) {return null;} if (cur->_data == x) {return cur;} while (Cur->_next != _head) {cur = cur->_next;if (cur->_data ==& nbsp; x) return cur;} Return null;} Void insert (linknode<t>* pos, const t& x) {if (pos==null) return; Linknode<t>* tmp = new linknode<t> (x); tmp->_prev = pos;tmp->_ Next = pos->_next;pos->_next = tmp;} Void erase (Linknode<t>* pos) {if (pos == null) return;else if (pos == _tail) {_tail = _tail->_prev;} else if (pos == _head) {_head = _head->_next;} linknode<t>* del = pos; linknode <t>* next = pos->_next; Linknode<t>* prev = pos->_prev;if (prev) prev->_next = next;if (next) next- >_prev =prev;delete del;} Protected:void destory () {if (_head == null) {return;} Else if (_head == _tail) {delete _head;_head = _tail = null;} else{linknode<t>* del = _head;_head = _head->_next;_head->_prev = del->_prev;del->_prev- >_next = _head;delete del;}} protected:linknode<t>* _head; linknode<t>* _tail;}; Void test1 () {list<int> l;l.pushback (1); L.pushback (2); L.pushback (3); L.pushback (4); L.PushBack (5) ; L.print (); LIST<INT>&NBSP;L1 (l); L1. Print ();} Void test2 () {list<int> l;l.pushback (1); L.pushback (2); L.pushback (3); L.pushback (4); L.PushBack (5) ; L.print (); LIST<INT>&NBSP;L1 (l); L1. Print (); List<int> l2;l2. Pushback (6); L2. Pushback (7); L2. Pushback (8); L2. Pushback (9); L2. Pushback (L2); Print ();} Void test3 () {list<string> l1;l1. Pushback ("abc"); L1. Pushback ("EDF"); L1. Pushback ("th"); L1. Pushback ("Ik"); L1. Print (); LIST<STRING>&NBSP;L2 (L1); L2. Print (); L2. Popback (); L2. Popback (); L2. Print (); L2. Popback (); L2. Popback (); L2. Popback (); L2. Popback (); L2. Popback (); L2. Print ();} Void test4 () {list<int> l;l.pushback (1); L.Pushback (2); L.pushback (3); L.pushback (4); L.pushback (5); L.insert (L.find (2), 8), L.print (); L.erase (L.find (1)) ; L.print ();} Void test5 () {list<string> l;l.pushback ("abc"), L.pushback ("Def"), L.erase (L.find ("abc")); L.print ();} Int main () {Test5 (); System ("pause"); return 0;}
This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1725253
Two-way loop linked list with head tail node