Because the class template does not support decoupled compilation, we can put the declaration and definition of a template class member function in a. hpp file
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/63/wKioL1b-DTfhVzcnAAAKFtUh58k269.png "title=" QQ picture 20160319195210.png "alt=" Wkiol1b-dtfhvzcnaaakftuh58k269.png "/>
Slist.hpp
#pragma once#include<iostream>using namespace std; #include <assert.h>template< class t>struct linknode //node class (suggested notation) {Linknode (Const T &NBSP;X); Data linknode<t>* _next; //for t _data; //nodes Point to the next node of the node};template<class t>class slist{public:slist (); //Constructor SList (const slist& s); // Copy construction slist &operator= (slist s); //assignment operator Overload ~slist (); // Single-linked list of specific operations Void reverse (); //Flip Void swap (slist& s); Void printslist (); //Print List void pushback (const t& x); //inserts a node void at the tail clear (); //-linked table Empty void popback (); //Delete Tail node Void pushfront (t x); //Head plug Void popfront (); //Delete First node void insert (linknode<t>* pos, t x);//fixed position insertion of a node void erase (linknode<t>* POS); //Delete a node linknode<t>* find (T x); //finds the node and returns the address of the node Int amount (); //calculates the number of nodes in the list void Remove (t x); //Find a node and delete Void removeall (t x); //Delete all xprivate:linknode<t>* _head; //in a linked list Point to head node linknode<t>* _tail; //point to Tail node};template<class t>linknode<t>::linknode (const t x): _data (x), _next (NULL) {}template<class t>slist<t>::slist () //constructor: _head ( NULL), _tail (null) {}TEMPLATE<CLASS&NBsp T>slist<t>::slist (const slist<t>& s) //Copy construction: _head (NULL), _tail (null) {if (s._head == null) {return;} Linknode<t>* tmp = s._head;do{pushback (tmp->_data); tmp = tmp->_next;} while (Tmp != s._head);} Template<class t>slist<t>& slist<t>::operator= (SList<T> s) Overloaded re-optimization of the //assignment operator (recommended notation) {if (this != &s) {swap (_head, s._head) ; swap (_tail, s._tail);} Return *this;} Template<class t>slist<t>::~slist () //destructor {Clear ();} Template<class t>void slist<t>::reverse () //() The inverse of the list (the method of inserting a new node with the head) {if (_ head == null | | _head->_next == _tail) {return;} Int ret = amount ();_tail = new linknode<t> (_head->_data); linknode<t>* begin = null; linknode<t>* tmp = _tail;while (--ret) {Linknode<t>* del = _head _head = _head->_next;delete del; //here do not forget to do cleanup work, otherwise memory leaks begin = new LinkNode<T> (_head->_data);begin->_next = tmp;_tail->_next = Begin;tmp = begin;} _head = begin;} Template<class t>void slist<t>::P rintslist ()//Print List {//head node is empty, no printed list if (_head == null) {cout << "this slist is empty !" << endl;return;} else{linknode<t>* tmp = _head;do{cout << tmp->_data << "--"; tmp = tmp->_next;} while (tmp != _head); Cout << endl;}} Template<class t>void slist<t>::P ushback (const t& x) //in the rear insertA node {//If the list is empty, only one node is inserted after the node, at which point _head=_tailif (_head == null) {_head = new linknode <T> (x); _tail = _head;_tail->_next = _head;} Else{_tail->_next = new linknode<t> (x); _tail = _tail->_next;_tail->_ Next = _head;}} Template<class t>void slist<t>::clear () //chain {linknode<t>* begin = _head;while (begin != _tail) {_head = _head->_next;delete begin;begin = _head;} _head = null;_tail = null;} Template<class t>void slist<t>::P opback () //-tail Delete {if (_head == null) {cout << "this slist is empty !" << endl;} else if (_head == _tail) {delete _head;_head = null;_tail = null;} Else{linknode<t>* cur = _head;while (cur->_next != _tail) {cur = cur->_next;} Delete _tail;_tail = cur;_tail->_next = _head;}} Template<class t>void slist<t>::P ushfront (t x) //Head plug {if (_head == null) {pushback (x);} Else{linknode<t>* tmp = _head;_head = new linknode<t> (x); _head->_ Next = tmp;_tail->_next = _head;}} Template<class t>void slist<t>::P opfront () //Delete the first node {if (_head == null) {cout << "this slist is empty !" << endl;return;} Linknode<t>* tmp = _head;_head = _head->_next;_tail->_next = _ Head;delete tmp;} A fixed position inserts a node (this function is used in conjunction with the Find function)//The Find function is used to locate the location where the new node needs to be inserted//(The return value of the Find function is passed to the parameter pos of the Insert function), and then the new node is inserted after the POS node xtemplate <class t>void slist<t>::inseRT (Linknode<t>* pos, t x) {assert (POS);if (pos == _tail) {pushback (x);} Else{linknode<t>* tmp = new linknode<t> (x); tmp->_next = pos->_ Next;pos->_next = tmp;}} Delete a node, again, to find the node and pass the parameter to the Erase function template<class t>void slist<t>::erase (linknode<t>* pos) {assert (POS);if (pos == _tail) {popback ();} if (pos == _head) {Popfront ();} else{linknode<t>* prev = _head;while (prev->_next != pos) {prev = prev->_next;} Prev->_next = pos->_next;delete pos;}} Template<class t>linknode<t>* slist<t>::find (t x) //finds the node and returns the address of this node {if (_head == null) {cout << "This SList is empty ! " << endl;return null;} else{linknode<t>* tmp = _head;do{if (tmp->_data == x) {return tmp;} Tmp = tmp->_next;} while (tmp != _head); return null;}} Template<class t>int slist<t>::amount () //calculates the number of nodes in the list {if (_head == null) {return 0;} else{int count = 0; linknode<t>* cur = _head;while (cur != _tail) {count++;cur = cur- >_next;} Return ++count;}} Template<class t>void slist<t>::remove (t x) // Find a node and delete {if (_head == null) {cout << "this slist is empty !" << endl;} Else{linknode* tmp = find (x);if (tmp != null) {Erase (TMP);}}} Template<class t>void slist<t>::removeall (t x) //Delete all x{if (_head == null) {cout << "this slist in the list is empty ! " << endl;return;} If the list is not empty, set the left and right front and back pointers, and iterate through the nodes of the delete node with Data x linknode<t>* left = _tail; Linknode<t>* right = _head;int count = amount ();while (count--) {// When the node to be deleted is the head node, pay attention to the head node to point to its next node//when the node to be deleted is the tail node, you need to be aware of the tail node to point to its previous node//when left and right point to the same piece of node to be deleted, the list is empty if (right- >_data == x) {if (_head == right) {_head = _head->_next;} if (_tail == right) {_tail = left;} if (right == left) {_head = null;_tail = null;return;} Linknode<t>* tmp = right;right = right->_next;delete tmp;left->_next = right;} Else{left = right;right = right->_next;}}}
Test.cpp
Use case test # include "SLIST.HPP" Void test () {slist<int> list1;list1. Pushback (1); List1. Pushback (2); List1. Pushback (3); List1. Pushback (4); List1. Pushback (5);cout << "slist 1: "; List1. Printslist (); slist<int> list2 = list1;cout << " slist 2: "; List2. Printslist (); slist<int> list3 (List1);cout << "slist 3: "; list3. Printslist (); slist<int> list4;list4 = list1;cout << "SList 4: "; list4. Printslist (); Cout << endl;list1. RemoveAll (2);cout << "slist 1: "; List1. Printslist (); list2. Reverse ();cout << "slist 2: "; List2. Printslist (); List3. Popback ();cout << "slist 3: "; list3. Printslist (); List4. Clear ();cout << "slist 4: "; list4. Printslist (); cout << endl;list1.Erase (List1. Find (4));cout << "slist 1: "; List1. Printslist (); List1. Popfront ();cout << "slist 1: "; List1. Printslist (); List1. Pushfront (0);cout << "slist 1: "; List1. Printslist (); List1. Insert (List1. Find (3), 0);cout << "slist 1: "; List1. Printslist (); List1. RemoveAll (0);cout << "slist 1: "; List1. Printslist ();} Int main () {Test (); System ("Pause");}
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/67/wKiom1b-DGWwSeNJAAAHqYpbNgM164.png "title=" QQ picture 20160401135229.png "alt=" Wkiom1b-dgwwsenjaaahqypbngm164.png "/>
This article from "Yan Anyang" blog, declined reproduced!
Template implementation single-linked list