Implementation of "C + +" single-linked list

Source: Internet
Author: User

List concept-linked list is a linear table, but not sequential storage, but each node is stored in the next node pointer, the storage data elements of the data string chain up.

Single-strand performance illustration:

#include <iostream>using namespace Std;typedef int DataType; struct Linknode{datatype _data; linknode* _next;//Default public linknode (const datatype& x): _data (x), _next (NULL) {}};class slist{public:slist (): _head ( NULL), _tail (null) {}~slist () {}slist (const slist& s): _head (null), _tail (null) {if (S._head = null) return; linknode* begin = S._head;do{this->pushback (begin->_data); begin = Begin->_next;} while (begin! = S._head);} slist&operator= (const slist& s) {if (This! = &s) {destory (); linknode* begin = S._head;do{this->pushback (begin->_data); begin = Begin->_next;}        while (begin! = S._head); }}void Destory () {while (_head) {Popfront ();}} Public:void print ()//printing {if (_head = = null) {cout<< "linked list is empty" &LT;&LT;ENDL;} Else{linknode *begin = _head;while (begin! = _tail) {cout<<begin->_data<< "-"; begin = Begin->_next ;} Cout<<begin->_data<<endl;cout<<endl;}} void Pushback (const datatype& x)//tail plug {//1. Empty list//2. There is one or more nodes if (_head = = NULL) {_head = new Linknode (x); _tail = _head;_tail->_next = _head;} Else{_tail->_next = new Linknode (x); _tail = _tail->_next;_tail->_next = _head;}} void Popback ()//tail Delete {//no nodes//one node//two + nodes if (_head = = null) {cout<< "linked list is empty" &LT;&LT;ENDL;} else if (_head = = _tail) {Delete _head;_head = Null;_tail = NULL;}            else{linknode* tmp = _head;while (tmp->_next!=_tail) {tmp = Tmp->_next;} Delete _tail;_tail = Tmp;_tail->_next = _head;}} void Popfront ()//Header Delete {//no nodes//one node//multiple nodes if (_head = = null) {cout<< "linked list is empty" &LT;&LT;ENDL;} else if (_head = = _tail) {Delete _head;_head = Null;_tail = NULL;} else{linknode* tmp = _head;_head = _head->_next;_tail->_next = _head;delete tmp;}} void Pushfront (const datatype& x)//head interpolation {if (_head = = NULL) {_head = new Linknode (x); _tail = _head;_tail->_next = _head; }else{linknode* tmp = _head;//delete _head;_head = new Linknode (x); _head->_next = Tmp;_tail->_next = _head;}} Linknode *find (DataType x) {if (_head = = null) {cout<< "linked list is empty" <<Endl;return NULL;} linknode* begin = _head;do{if (Begin->_data ==x) {return begin;} Else{begin = Begin->_next;}} while (Begin!=_tail->_next); return NULL; }bool Remove (linknode* N) {//_head is empty//n is empty//Only one node//two or more if (_head = null) {cout<< "linked list is empty, cannot be deleted" <<endl; return false;} else if (n = = null) {cout<< "Delete node is empty, re-enter" <<endl;return false;} else if (_tail = = _head)//Only one node {if (n = = _head) {Delete _head;_head = Null;_tail = Null;return true;} return false;} else//2 nodes and above {linknode* begin = _head;while (Begin->_next! = N) {begin = Begin->_next; if (begin = = _head)//not found for this node {R Eturn false;}} if (n = = _head) {_head = _head->_next;_tail->_next = _head;} else if (n = = _tail) {_tail = Begin;_tail->_next = _head;} Else{begin->_next = N->_next;} Delete N;return true;}} void Insert (linknode* N, DataType x) {//list is empty//node not found///Only one node//head plug//tail plug//middle plug if (_head = = null) {cout<< "linked list is empty, new linked list is being created for you "<<endl;_head = new Linknode (x); _tail = _head;_tail->_next = _head;} else if (n = = NULL){cout<< "The node could not be found, re-enter the node insert" <<endl;return;} else if (_head = = _tail) {if (n = = _head | | n = = _tail) {Linknode *tmp = _head;_head = new Linknode (x); _head->_next = tmp; _tail = Tmp;_tail->_next = _head;} else{cout<< "The node could not be found, re-enter the node insert" <<endl;return;} else//more than two nodes {linknode* begin = _head;while (Begin->_next! = N) {begin = Begin->_next; if (begin = = _head)//not found for this node { cout<< "The node could not be found, re-enter the node to insert" <<endl;return;}} if (n = = _head) {Linknode *tmp = _head;_head = new Linknode (x); _head->_next = Tmp;_tail->_next = _head;} else if (n = = _tail) {Linknode *tmp = _tail;_tail = new Linknode (x); tmp->_next = _tail;_tail->_next = _head;} else{Linknode *tmp = new Linknode (x); begin->_next = tmp; tmp->_next = n;}}} void Erase (DataType x)//delete specified element {Linknode *begin = _head; Linknode *tmp = Begin;while (Begin->_data! = x) {tmp = Begin;begin = Begin->_next; if (begin = = _head) {cout<< "Without this element, cannot delete "<<endl;}} if (begin = = _head) {_head = _head->_next;_tail->_Next = _head;} else if (begin = = _tail) {_tail = Tmp;_tail->_next = _head;}    Else{tmp->_next = Begin->_next;} Delete Tmp;//delete begin;} void Reverse () {_tail = _head; linknode* newhead = _head; linknode* begin = _head->_next; while (begin) {linknode* TMP = begin;tmp->_next = Newhead;newhead = Tmp;begin = Begin    ->_next;} _head = Newhead;} private:linknode* _head;//Pointer to the list header linknode* _tail;//pointer to the end of the chain};void Test1 () {SList s1;s1. Pushfront (1); S1. Pushfront (2); S1. Pushfront (3); S1. Pushfront (4); S1. Print (); SList S2 (s1); S2. Print (); s2 = s1;s2. Print ();//s1. Reverse ();//s1. Print ();//s1. Erase (4);//s1. Print ();/*linknode* Ret1 = S1. Find (2); S1.    Insert (ret1,5); S1. Print (); *///s1. Popfront ();//s1. Print ();//s1. Popback ();//s1. Print ();/*s1. Pushfront (5); S1. Pushfront (6); S1. Pushfront (7); S1. Pushfront (8); S1. Print (); S1. Popfront (); S1. Popfront (); S1. Popfront (); S1. Popfront (); S1. Popfront (); S1. Popfront (); S1. Popfront (); S1. Popfront (); S1. Print (); *///linknode* ret = S1.    Find (1); cout<<ret-&Gt;_data<<endl;/*s1.    Remove (ret); S1. Print (); linknode* Ret1 = S1. Find (2); S1.    Remove (RET1); S1. Print (); linknode* Ret2 = S1. Find (3); S1.    Remove (Ret2); S1. Print (); linknode* Ret3 = S1. Find (4); S1.    Remove (RET3); S1. Print (); */}int main () {Test1 (); GetChar (); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Implementation of "C + +" single-linked list

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.