C + + in the two-way list, the main implementation (adding and deleting, list inverse, constructors, operator overloading, etc.)
The main content of this article
1) Introduce the way of the bidirectional linked list in C + +.
2) Realization of the data in the doubly linked list, delete, check, change, linked list reverse, linked list output
3) Description of the constructor, destructor, and operator overloaded functions in the class
Next, we introduce the member functions of the doubly linked list: This write function is placed in the header file for easy writing
#pragma once#include<iostream>using namespace std; #include <assert.h>typedef int datatype;class listnode/ /Node {public:listnode (DataType x): _data (x), _next (null), _prev (null) {}listnode*_next; Listnode*_prev;datatype _data;}; Class List//doubly linked list {public:list (); List (List&s); list&operator= (const list&s); ~list (); 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 (), void Printlist (), void Clear (), void Printreverselist ();p rivate:listnode*_ Head Listnode* _tail;};
Next, we introduce the functions,
1) Constructor (one is written here, because only this one is used)
List::list (): _head (null), _tail (null) {}
2) Copy construction in the constructor
List::list (list&s): _head (null), _tail (null) {listnode*cur = S._head;while (cur) {pushback (cur->_data); cur = Cur->_next;}}
3) Assignment operator overloading
list& list::operator= (const list&s) {_head = Null;_tail = NULL; Listnode*cur = S._head;while (cur) {pushback (cur->_data); cur = cur->_next;} return *this;}
4) destructor
List::~list () {Clear ();} void List::clear () {cout << "~clear ()"; Listnode*cur = _head;while (cur) {Listnode*del = Cur;cur = Cur->_next;delete del;} cout << "Clear is NULL" << Endl;}
5) Increase the function of the node (three, pre-increment, post-increment, insert after the given node)
void List::P ushfront (DataType x) {if (_head = = NULL) {_head = new ListNode (x); _tail = _head;} Else{listnode*temp = new ListNode (x); _head->_prev = Temp;temp->_next = _head;_head = temp;}} void List::P ushback (DataType x)//increment {if (_head = = NULL) {_head = new ListNode (x); _tail = _head;} Else{listnode*temp = new ListNode (x); _tail->_next = Temp;temp->_prev = _tail;_tail = temp;}} void List::insert (ListNode *pos, DataType x) {assert (POS); if (pos = = _tail) {pushback (x);} Else{listnode*cur = pos->_next; Listnode*temp = new ListNode (x); temp->_next = Cur;temp->_prev = Pos;pos->_next = Temp;cur->_prev = temp;}}
6) Delete node (three kinds, before deleting, deleting, deleting a given node)
Void list::P Opfront () {if (_head == null) {cout << "EMPTY list! " << endl;return;} else{if (_head->_next) {listnode *temp = _head;_head = _head->_next;_head- >_prev = null;delete temp;} Else{delete _head;_head = _tail = null;}}} Void list::P opback () {if (_head == null) {cout << "EMPTY list! " << endl;return;} else{if (_tail->_prev) {listnode *temp = _tail;_tail = _tail->_prev;_tail- >_next = null;delete temp;} Else{delete _tail;_head = _tail = null;}}} Void list::erase (Listnode*pos) {assert (POS);if (pos == _head) {Popfront ();} else if (pos == _tail) {popback ();} else{listnode*temp = pos->_prev; Listnode*next = pos->_next;temp->_next = next;next->_prev = temp;delete pos;}}
7) Finding nodes
listnode* List::find (DataType x) {if (_head = = null) {cout << "empty list! "<< Endl;return NULL;} Listnode*cur = _head;while (cur) {if (Cur->_data = = x) return cur;cur = Cur->_next;}}
8) Two-way list inverse (two ways)
Method One: Exchange the precursors and successors of each node.
Method Two: Create a new linked list, pick a node from the far-linked list and fill it up on the new list
Void list::reverse () //method a//{//if (_head == null)//{//cout << "EMPTY list! " << endl;//return;//}//else if (_head == _tail)//return;//else//{//swap (_ Tail,_head);//listnode *cur = _head;//while (cur)//{//swap (cur->_prev, cur->_ Next);//cur = cur->_next;//}//}//}void list::reverse ()//Method two {if (_head == null ) {cout << "EMPTY list! " << endl;return;} else if (_head == _tail) return;else{listnode*cur = _tail; listnode *newhead = null;while (cur) {if (newhead == null) {newhead = new listnode (cur->_data); _head = newhead;} Else{listnode*temp = new listnode (cur->_data); newhead->_next = temp;temp->_ Prev = newhead;newhead = temp;_tail = temp;} Listnode* del = cur;cUr = cur->_prev;delete del;}}}
9) Print List (two types, sequential print, reverse print)
void List::P rintlist ()//sequential print {listnode*cur = _head;while (cur) {cout << cur->_data << "--"; cur = cur- >_next;} cout << "NULL" << Endl;} void List::P rintreverselist ()//reverse Print {cout << "from backward Forward:"; Listnode*cur = _tail;while (cur) {cout << cur->_data << '; cur = cur->_prev;} cout << "NULL" << Endl;}
And of course the main function (test case)
#include "List.h" void Test3 () {cout << Endl; List L1;L1. Pushfront (1); L1. Pushback (2); L1. Pushback (3); L1. Pushback (4); L1. Pushback (5); L1. Pushback (6);//l1. Pushfront (4);//l1. Popfront ();////l1 before deletion. Popback ();//after deleting//listnode *pos = L1. Find (3);//l1. Insert (POS, 5);//l1. Erase (POS); cout << "L1:"; L1. Printlist (); L1. Printreverselist (); cout << "reverse L1:"; L1. Reverse (); L1. Printlist (); L1. Printreverselist (); cout << "Copy Construction-" L2: "; List L2 (L1); L2. Printlist (); cout << "assignment operator overloading-" L3: "; List l3;l3 = L2;l3. Printlist ();} int main () {Test3 (); System ("pause"); return 0;}
The above functions are summed up in the process of learning, there will be some omissions or errors, so please the great God's criticism, thank you
This article is from the "Sharing Progress" blog, be sure to keep this source http://xmwen1.blog.51cto.com/10730069/1748216
C + + in the two-way list, the main implementation (adding and deleting, list inverse, constructors, operator overloading, etc.)