Two-way linked list with leading tail node

Source: Internet
Author: User

#include <iostream> #include <assert.h>using namespace std;typedef int datatype;/ /doubly linked list, headless node struct linknode{//struct default is the public Access qualifier Public:linknode (const datatype& x): _data (x),  _prev (NULL),  _next (null) {}~linknode () {}public:datatype _data; linknode* _prev; linknode* _next;}; Class list{public:list (): _head (null),  _tail (null) {}~list () {}void pushback (const datatype & x) {linknode* tmp = new linknode (x);if  (null == _tail) {_head  = _tail=tmp;} Else{tmp->_prev = _tail;_tail->_next = tmp;_tail = _tail->_next;}} Void  popback () {linknode* cur = _head;if  (_tail == null) {return;} elseif  (_head == _tail) {delete _head;_head = _tail = null;} else{linknode* del = _tail;_tail->_prev->_next = null;_tail = _tail- >_prEv;delete del;}} Void pushfront (const datatype& x) {linknode* tmp = new linknode (x);// If there is no node if  (null== _tail) {_tail=_head = tmp;} Else {tmp->_next = _head;_head->_prev = tmp;_head = tmp;}} Void popfront () {if  (_head == null) return;else if  (_head==_tail) {free (_head); _ Head = _tail=null;} else{linknode* del = _head;_head = _head->_next;_head->_prev = null; Delete del;}} Linknode* find (const datatype& x) {linknode* cur = _head;while  (cur) {if   (cur->_data == x) Return cur;cur = cur->_next;} Return null;} Void erase (Linknode* pos) {assert (POS);//Processing POS is header, tail or head and tail linknode* del = null;if  ( Pos == _head) {Del = pos;_head = _head->_next;_head->_prev = null ;} if  (Pos==_taIL) {del = pos;_tail = _tail->_prev;_tail->_next = null;} if  (del == null) {linknode* prev = pos->_prev; Linknode* next = pos->_next;prev->_next = next;next->_prev = prev;} Delete del;} Void insert (linknode* pos, const datatype& x) {assert (POS); Linknode* tmp = new linknode (x);if  (pos == _tail) {_tail->_next =  tmp;tmp->_prev = _tail;_tail = tmp;} else{linknode* next = pos->_next;tmp->_next = next;next->_prev =  Tmp;pos->_next = tmp;tmp->_prev = pos;}} Void  display () {linknode* cur = _head;while  (cur) {cout <<  (cur- >_data)  <<  ", Cur = cur->_next;} cout <<  "NULL" &NBSP;&LT;&LT;&NBSP;ENDL;} private:linknode* _head; Linknode* _tail;}; Void test1 () {list l1;l1. Pushback (1); L1. Pushback (2); L1. Pushback (3); L1. Pushback (4); L1. Pushback (5); L1. Pushback (6); L1. DisPlay (); L1. Popback (); L1. DisPlay (); L1. Popback (); L1. Popback (); L1. Popback (); L1. Popback (); L1. Popback (); L1. Popback (); L1. Popback (); L1. Popback (); L1. DisPlay ();} Void test2 () {list l2;l2. Pushfront (1); L2. Pushfront (2); L2. Pushfront (3); L2. Pushfront (4); L2. Pushfront (5); L2. Pushfront (6); L2. DisPlay (); L2. Popfront (); L2. Popfront (); L2. Popfront (); L2. Popfront (); L2. DisPlay (); L2. Popfront (); L2. Popfront (); L2. Popfront (); L2. Popfront (); L2. DisPlay ();} Void test3 () {list l2;l2. Pushfront (1); L2. Pushfront (2); L2. Pushfront (3); L2. Pushfront (4); L2. Pushfront (5); L2. Pushfront (6); L2. DisPlay (); Linknode* ret = l2. Find (6);//l2. Erase (ret); L2. Insert (ret, 7); L2. DisPlay ();} Int main () {Test3 (); 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/1717933

Two-way linked list with leading 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.