C + + in the form of one-way list: the implementation of additions and deletions, constructors, operator overloading, destructor, and so on.
Creating a header file SList.h
#pragma oncetypedef int datatype;//slist to access the Slistnode, the friend function can be implemented by the friend function in the class being accessed Class slistnode {friend class slist;//friend function Public:slistnode (const datatype x): _data (x), _next (NULL) {} private:slistnode* _next;datatype _data;}; Class slist{public:slist (): _head (null), _tail (null) {}//deep copy SList (const slist& s): _head ( NULL), _tail (null) {slistnode* cur = s._head;while (cur) {this->pushback (cur->_ data); cur = cur->_next;}} Deep copy of the traditional notation//slist& operator= (const slist& s)//{//if (this != &s)// {//clear ();//slistnode* cur = s._head;//while (cur)//{//this->pushback (cur->_data); /cur = cur->_next;//}//}//return *this;//}//deep copy of the modern Slist& operator= (SList& s) {swap (_head, s._head); swap (_tail, s._tail); return *this;} ~slist () {Clear ();} Public:void clear (); Void pushback (DatatYPE X); Void popback (); Void pushfront (datatype x); Void popfront ();//void Insert ( SIZE_T POS,DATATYPE X); Void insert (slistnode* pos, datatype x); Void Erase ( Slistnode* pos); Slistnode* find (datatype x), void printslist ();p rivate:slistnode* _head; slistnode* _tail;};
implementation of each function
#include <iostream>using namespace std; #include "SList.h" #include <assert.h>void slist :: Clear () {slistnode* cur = _head;while (cur) {slistnode* del = cur;cur = cur->_next;delete del; del = null;}} Void slist::P rintslist ()//Print List {slistnode* cur = _head;while (cur) {cout << cur->_data << "Cur = cur->_next"; cout << "NULL" &NBSP;<<&NBSP;ENDL;} Void slist::P ushback (datatype x)//tail plug {if (null == _head) {_head = new Slistnode (x);//Open a new node with a value of x _tail = _head;} else{//slistnode* cur;//cur->_data = x; //_tail->_next = cur;//_tail = cur;_tail->_next= new slistnode (x); _tail = _tail->_ Next;}} Void slist::P opback ()//tail Delete {if (null == _head) {cout << "slist is empty!" << endl;} else if (_head == _tail) {delete _head;_head = _tail = null;} else{slistnode* cur = _head;//find the previous node to delete the tail node curwhile (cur->_next->_next) {cur = cur->_next;} Delete cur->_next;cur->_next = null;_tail = cur;}} Void slist::P ushfront (datatype x)//head Insert {Slistnode* tmp = _head;_head=new slistnode (x); _head->_next = tmp;} Void slist::P Opfront ()//head Delete {if (null == _head) {cout << "SList is empty! " << endl;} else if (null == _head->_next) {Delete _head;_head = null;//delete After you set the pointer to empty , otherwise produce wild pointer}else{slistnode* tmp = _head->_next;delete _head; _head = tmp;}} Void slist::insert (size_t pos, datatype x)//{//assert (POS);//slistnode* tmp = _head;//pos -= 1;//while (--pos)//{//tmp = tmp->_next;//}//if (null == tmp)//slist::P ushback (x);//else//{//slistnode* next = tmp->_next;// Slistnode* cur = new slistnode (x);//tmp->_next = cur;//cur->_next = next;//}//}void slist::insert (slistnode* pos, datatype x)////inserts X{assert (POS) at the specified position; slistnode* tmp = _head;while (TMP) {if (Null == tmp->_next) SList:: Pushfront (x);else if (pos == tmp->_next) {slistnode* cur = new Slistnode (x); cur->_next= tmp->_next;tmp->_next = cur;return;//Note End Loop}tmp = Tmp->_next;}} Void slist::erase (Slistnode* pos) {assert (POS); slistnode* tmp = _head;while (TMP) {if (Null == tmp->_next) SList:: Popfront ();else if (pos == tmp->_next) {slistnode*&Nbsp;cur = tmp->_next->_next;delete tmp->_next;tmp->_next = null;tmp-> _next = cur;return;//note End loop}tmp = tmp->_next;}} Slistnode* slist::find (datatype x) {slistnode* cur = _head;while (cur) {if (x == cur->_data) {return cur;} Cur = cur->_next;} Return null;}
Test cases for each action
void test1 () {//tail insert End Delete slist s; S.pushback (1); S.pushback (2); S.pushback (3); S.pushback (4); S.printslist (); S.popback (); S.printslist ();//s.popback ();//s.popback ();//s.printslist ();//s.popback ();//s.popback ();//s.popback (); SLIST&NBSP;S1 (S); S1. Printslist (); slist s2; s2 = s; S2. Printslist ();} Void test2 () {//head plug Delete slist s; S.pushfront (1); S.pushfront (2); S.pushfront (3); S.pushfront (4); S.printslist (); S.popfront (); S.printslist (); S.popfront (); S.popfront (); S.popfront (); S.printslist (); S.popfront ();} Void test3 () {//Specify the location to insert a number to find a number slist s; S.pushback (1); S.pushback (2); S.pushback (4); S.pushback (5); S.printslist ();//s.insert (3, 3); Slistnode* p = s.find (4); S.insert (p, 3); S.printslist ();} Void test4 () {//delete a node slist s; S.pushback (1); S.pushback (2); S.pushback (3); S.pushback (10); S.pushback (4); S.pushback (5); S.printslist (); Slistnode* p = s.find (10); S.erase (P); S.printslist ();}
Friend function
reduce system overhead and increase efficiency when data sharing between classes is implemented. If a function in Class A accesses a member in Class B (for example, the implementation of a smart pointer Class), then the function in Class A is a friend function of Class B. Specifically, in order to enable member functions of other classes to access the private variables of the class directly. That is, allow the outside class or function to access the class's private and protection variables, so that two classes share the same function.
In fact, there are about two situations in which you need to use a friend function:
(1) A friend is required for some situations where operator overloading is used.
(2) Two classes to share data.
1.1 Advantages and disadvantages of using friend functions
Advantages: Can improve efficiency, express simple, clear.
Disadvantage: The friend function breaks the encapsulation mechanism, tries not to use the member function, unless the unavoidable situation makes the UF meta-function.
1.2 Arguments to the friend function
Because the friend function does not have this pointer, there are three conditions for the parameter:
(1) to access non-static members, the object is required to do the parameters;
(2) if you want to access static members or global variables, you do not need the object to do parameters;
(3) If the object that makes the parameter is the global object, then the object does not need to do the parameter;
1.3 The location of the friend function
Because a friend function is a function outside the class, its declaration can be placed in the private or public segment of the class without distinction.
1.4 Call of the friend function
You can directly adjust the cell function, do not need to pass through the object or pointer
The difference between a friend function and a member function of a class
The member function has this pointer, and the friend function does not have the this pointer.
A friend function cannot be inherited, just as a father's friends may not be friends of a son.
This article is from the "Materfer" blog, make sure to keep this source http://10741357.blog.51cto.com/10731357/1748574
C + + single-linked list "constructors, operator overloading, destructors, additions and deletions, etc."