Linear table-Single linked list (C + +)

Source: Internet
Author: User

Single-strand performance illustration:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/93/wKiom1cEtCnCDEqvAAAaiTio6Bk130.png "title=" QQ picture 20160406145946.png "alt=" Wkiom1cetcncdeqvaaaaitio6bk130.png "/>

Single-linked list structure:

struct Node{node (const datatype& D)//constructor of node: _data (d), _next (NULL) {}datatype _data;    data struct Node *_next; Pointer to the next node};

Single-linked list of leading nodes and tails:


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/90/wKioL1cEuviB6TrWAAAdy6zgrSk382.png "title=" QQ picture 20160406152633.png "alt=" Wkiol1ceuvib6trwaaady6zgrsk382.png "/>

One of the advantages of a tail pointer is that it is convenient to find the tail of the list, to facilitate insertion of an element in the tail.


Below we use the class to implement a single-linked list:

class slist{friend ostream& operator<< (ostream& os, const slist & s);   //output operator overload (friend) Public:slist ()                             // Constructor: _head (null), _tail (null) {}slist (const slist& s)               //Copy construction: _head (NULL),  _tail (null) {node *cur = _head; while  (cur) {pushback (cur->_data ); cur = cur->_next;} _tail = cur;} ~slist ()                             //destructor {if  (_head == null) return; node *cur = _head;if  (cur != null) {node *del = cur;cur =  cur->_next;delete del;} _tAil = null;_head = null;} Slist& operator= (slist s)            // Assignment operator overload {Swap (_head, s._head); swap (_tail, s._tail); return *this;}


The four most basic functions of a single-linked list:

Void slist::P ushback (const datatype& d)    //tail plug {node *newnode =  new node (d);       //build a new node if  (_head == null) {_head  = newnode;_tail = newnode;} Else{_tail->_next = newnode;_tail = newnode;}} Void slist::P ushfront (const datatype& d)    //head Insert {node *newnode =  new node (d);if  (_head == null) {_head = newnode;_tail = newnode;} Else{newnode->_next = _head;_head = newnode;}} Void slist::P opback ()                       //tail {if  (_head == null) return;else if  (_ Head == _tail) {delete _tail;_tail = null;_head = null;} else{node *cur = _head;while  (cur->_next !=  _tail) {cur = cur->_next;} Delete _tail;_tail = cur;_tail->_next = null;}} Void slist::P opfront ()                    //Head Delete {if  (_head == null) return;else if  (_head ==  _tail) {delete _tail;_tail = null;_head = null;} Else{node *del = _head;_head = _head->_next;delete del;}}


Gives a data that returns the node if it is found, and returns null if it is not found

node* slist::find (const datatype& d) {Node *cur = _head;while (cur! = NULL) {if (Cur->_data = = d) return cur;cur = cur ->_next;} return NULL;}

Given a node, insert a new node after the node

void Slist::insert (node* pos, const datatype& D) {node *newnode = new Node (d); if (pos = = _tail)//IF The fixed node is the tail node, where you can call the tail plug {_tail->_next = Newnode;_tail = NewNode;} Else{newnode->_next = Pos->_next;pos->_next = NewNode;}}

Reverse order of a list: Three pointers are used here

void Slist::reverse () {Node *p1 = NULL; Node *p2 = _head; Node *newhead = Null;while (p2) {p1 = P2;p2 = P2->_next;p1->_next = Newhead;newhead = p1;} _head = Newhead;}

Sort of list: using bubble sort

void Slist::sort () {Node *cur = _head;  Node *end = null;while (cur! = end) {while (Cur->_next! = end) {if (Cur->_data > Cur->_next->_data) {DataType          TMP = Cur->_data;cur->_data = Cur->_next->_data;cur->_next->_data = tmp;}  cur = cur->_next;}      end = cur; cur = _head;}}

Delete a node (given a data, delete the first node with the data equal to it)

void Slist::remove (const datatype& D) {node *cur = _head;while (cur! = NULL) {if (Cur->_data = = d) {node *del = CUR-&G T;_next;datatype tmp = Cur->_data;cur->_data = Cur->_next->_data;cur->_next->_data = Tmp;cur->_ Next = Cur->_next->_next;delete Del;return;} cur = cur->_next;}}

Delete some nodes (given one data, delete each node with equal data)

void Slist::removeall (const datatype& D) {node *cur = _head;while (cur! = NULL) {if (Cur->_data = = d) {node *del = cur ->_next;datatype tmp = Cur->_data;cur->_data = Cur->_next->_data;cur->_next->_data = tmp;cur- >_next = Cur->_next->_next;delete del;} cur = cur->_next;} return;}

Delete Non-tailed nodes

void Slist::earsenottail (node *pos) {node *del = pos; Node *cur = _head;while (cur->_next!=pos)//Find the node's previous node {cur = cur->_next;}     Cur->_next = pos->_next; Let its _next point to the _nextdelete del where the node is to be deleted;

Find the middle node

node* Slist::findminnode ()//fast pointer problem {//two pointers pointing to the head node node *cur = _hea                 D                  A quick walk two steps, slow one step at a time node *fast = cur; When the fast pointer goes to the end, the slow pointer points to the middle node *slow = cur;while (fast) {fast = Fast->_next->_next;slow = Slow->_next;} return slow;}

Delete the Count K nodes

void SList::D elknode (int k) {node *cur = _head;int i = k-1;while (i)//Let cur point to positive number k Node {cur = cur->_n Ext;i = i-1;;}            Node *p1 = _head;     Node *tmp = null;while (cur->_next)//Let a pointer to the head node go with the cur {tmp = P1;P1 = P1->_next;cur = cur->_next; When cur points to the tail node, the pointer points to the inverted k node}node *del = P1;tmp->_next = P1->_next;d elete p1;}

Detects if a ring is being carried

Detects if a ring int slist::checkcycle (const slist& s)//slow pointer problem {Node *fast = _head; Node *slow = _head;while (slow) {if (slow = = fast) {return 1;} Fast = Fast->_next->_next;slow = Slow->_next;} return 0;}

Get the entry point of the ring

node* Slist::getcycleeorynode () {Node *cur = _head;while (cur) {if (cur = = _tail) {return cur;} cur = cur->_next;} return NULL;}

Determine if intersect

int Slist::checkcross (slist& L1, slist& L2) {int count1 = L1. Lengthoflist (L1); int count2 = L2. Lengthoflist (L2), if (Count1 > Count2) {Node *cur = l1._head;while (cur) {if (L2._tail = = cur) return 1;cur = Cur->_next ;}} Else{node *cur = l2._head;while (cur) {if (L1._tail = = cur) return 1;cur = Cur->_next;}} return 0;}

Merge two linked lists

int Slist::checkcross (slist& L1, slist& L2) {int count1 = L1. Lengthoflist (L1); int count2 = L2. Lengthoflist (L2), if (Count1 > Count2) {Node *cur = l1._head;while (cur) {if (L2._tail = = cur) return 1;cur = Cur->_next ;}} Else{node *cur = l2._head;while (cur) {if (L1._tail = = cur) return 1;cur = Cur->_next;}} return 0;}


Find the intersection of two linked lists

node* Slist::getlinkcross (slist& L1, slist& L2) {int count1 = L1. Lengthoflist (L1); int count2 = L2. Lengthoflist (L2); Node *cur1 = L1._head; Node *CUR2 = l2._head;if (Count1 > Count2) {node *cur1 = L1._head; Node *CUR2 = L2._head;while (CUR2) {if (Cur2->_next = = Cur1->_next) return cur1;else{cur1 = Cur1->_next;cur2 = CU R2->_next;}}} Else{node *cur1 = L1._head; Node *CUR2 = L2._head;while (cur1) {if (Cur2->_next = = Cur1->_next) return cur1;else{cur1 = CUR1->_NEXT;CUR2 = Cur2 ->_next;}}} return NULL;}

To find the chain table length

int slist::lengthoflist (const slist& s)

{

int length = 0;

Node *cur = _head;

while (cur)

{

length++;

Cur = cur->_next;

}

return length;

}

Later there will be improved version, hope that we support 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0028.gif "alt=" J_0028.gif "/>

Linear table-Single linked list (C + +)

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.