Two-way loop linked list with head tail node

Source: Internet
Author: User

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 &LT;T&GT;&AMP;&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;&LT;&LT;&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&LT;INT&GT;&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&LT;INT&GT;&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&LT;STRING&GT;&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

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.