Design and implementation of single chain List of C + +

Source: Internet
Author: User

Single-linked lists are a common data structure, and C + + differs from C's language features as encapsulation, inheritance, and polymorphism. To implement a single-linked list, first we want to clarify what is a single-linked list, linked list is composed of one or more nodes, to implement the data structure of the linked list, we first want to be clear what is the node.

A node is made up of data + pointers of that node type, as follows:

Class Seqnode{public:friend class seqlist;//defines a friend class that enables the list class to access the private member of the Node class Seqnode (Datetype data): _data (data), _next (NULL) {} private:seqnode* _next;datetype _data;};

the formation of the linked list is node---node->.....->null;

Therefore, novices often do not write linked lists, explaining the data structure without explicit nodes. With a node we can make it a linked list by modifying the _next pointer in the node, where we create the list class and declare the basic operations of some linked list classes:

Class Seqlist{public:seqlist ()//When creating a linked list, there are no nodes in the list, the head-tail pointer is empty: _head (null), _tail (null) {}~seqlist () {_clear ()} void pushback (Datetype x), void Popback (), void Pushfront (Datetype x), void Popfront (), void Erase (Datetype x), void Insert ( Datetype x,size_t Pos);p rivate:void _clear ()//function to release each node of the list, encapsulate it to implement some later operations {while (_head) {seqnode* _cur = _head;_ head = _head->_next;delete _cur;}} seqnode*_head;//head pointer seqnode*_tail;//tail pointer};

Here I did not write these two classes of copy construction and the overload of the assignment operator, the copy of the linked list can be implemented in two ways, the copy of the node needs to look at the type of datetype and the stored content to decide, for example, each node has a string opened up on the heap, In this case, I store the default as shaping, so I don't have to deal with the issue of dark copy.

The rest of the declared functions are implemented:

#include "SList.h" #include <cassert>void seqlist::P ushback (datetype x) {if  (_head ==  null) {_tail=_head = new seqnode (x);} Else{_tail->_next = new seqnode (x); _tail = _tail->_next;}} Void seqlist::P opback () {assert (_head);if  (_head == _tail) {delete _head;_head =  _tail = null;} else{seqnode*cur = _head;while  (cur->_next != _tail) {Cur = cur->_next ;} Cur->_next = null;delete _tail;_tail = cur;}} Void seqlist::P ushfront (datetype x) {if  (_head == null) {_tail = _head =  new seqnode (x);} Else{seqnode* cur = new seqnode (x); cur->_next = _head;_head = cur;}} Void seqlist::P Opfront () {assert (_head);if  (_head == _tail) {delete _head;_head =  _tail = null;} Else{seqnode*cur = _head;_hEad = _head->_next;delete cur;}} Void seqlist::insert (Datetype x, size_t pos) {if  (pos == 0| | _head == null) {Pushfront (x);} else{seqnode*cur = _head; Seqnode*tmp = new seqnode (x);while  (--pos) {cur = cur->_next;} Tmp->_next = cur->_next;cur->_next = tmp;}} Void seqlist::erase (datetype x) {while  (_head->_data == x) {SeqNode*tmp =  _head;_head = _head->_next;delete tmp;} if  (_head->_next != null) {seqnode*prev = _head; seqnode*cur = prev->_next;while  (cur) {if  (cur->_data == x) {SeqNode*tmp  = cur;cur = cur->_next;prev->_next = cur;delete tmp;} Prev = prev->_next;cur = prev->_next;}}}

Test Case:

#include <iostream>using namespace std; #include "SList.h" void Test1 () {seqlist l;l.pushback (1); L.pushback (2); L. Pushback (3); L.pushback (4); L.pushback (5); L.popback (); L.popback (); L.popback (); L.popback (); L.popback (); L. Pushfront (5); L.pushfront (4); L.pushfront (3); L.pushfront (2); L.pushfront (1); L.popfront (); L.popfront (); L.popfront ( ); L.popfront (); L.popfront (); L.popfront ();} void Test2 () {seqlist l;l.insert (); L.pushback (1); L.pushback (2); L.pushback (3); L.pushback (4); L.pushback (1); L. Pushback (6); L.erase (1); L.insert (5, 3);} int main () {Test2 (); return 0;}

If there is any deficiency, I hope to correct my mistakes.

This article is from the "Pawnsir It Road" blog, so be sure to keep this source

Design and implementation of single chain List of C + +

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: 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.