#include <iostream>using namespace Std;template<class t>class list{struct node{t data; Node* Next; Node () {next=null;}}; node* Head;public:list () {head = new Node;} List (list<t>& that)//copy construction {head = new Node; node* node = that.head->next;while (node!=null) {push_back (node->data); node=node->next;}} List operator = (list<t>& that)//assignment construct {head = new Node; List<t> list (that); head = List.head;} void Push_back (T data)//Add an element at the end of the list {node* node = new Node;node->data = Data;back ()->next = Node;} void Pop_back ()//delete last element {node* Node = head;while (node->next->next!=null) Node=node->next;delete node-> Next;node->next = NULL;} node* Front ()//returns the first element {node* node = head->next;return node;} node* back ()//returns the last element {node* node = head;while (Node->next!=null) Node=node->next;return node;} bool Empty ()//Determine if the list is empty {return head->next==null;} void reserve ()//Reverse list {if (head->next==null| | Head->next->next==null) return; node* PrevNode = NULL; node*Curnode = Head->next;while (curnode!=null) {node* NextNode = Curnode->next;curnode->next = PrevNode;prevNode = Curnode;curnode = NextNode;} Head->next = PrevNode;} void Remove (T val)//delete element from list {node* Node = Head->next;while (node!=null) {if (Node->next!=null && node- >next->data = = val) {node* no = Node->next;node->next = Node->next->next;delete No;} else node = Node->next;}} void merge (list<t> List)//merge two list{node* Node = List.head->next;while (node!=null) {push_back (Node->data) ; node=node->next;}} void clear ()//delete all elements {node* Node = Head;while (node!=null) {node* temp = Node;node = Node->next;delete temp;}} void swap (list<t>& List)//Exchange two x list{node* node = list.head;list.head = Head;head = node;} int size ()//Returns the number of elements in list {node* Node = head->next;int count = 0;while (node!=null) {count++;node=node->next;} return count;} void sort ()//Sort the list {int n = size (); T T[n]; node* node = head->next;for (int i = 0; i < n; ++i) {T[i] = Node->d ata;node = Node->next;} for (int i = 0, i < n-1; ++i) for (int j = i+1; j < n; ++j) if (T[i]>t[j]) {t a = t[i];t[i] = t[j];t[j] = A;} Clear (); head = new Node;for (int i = 0; i < n; ++i) push_back (T[i]);} Friend ostream& operator << (ostream& os,list& List)//overloaded output << operator {node* Node=list.head->next ; while (node = NULL) {os << node->data << ""; node = Node->next;} return OS;} ~list ()//destructor {clear ();}}; int main () {list<int> List; list<int> li;for (int i=0;i<10;i++) list.push_back (i); for (int i = ten; i >; i) li.push_back (i); List.swap ( LI); cout << list << endl;cout << li << endl;list.merge (LI); cout << list << Endl;list.s ORT (); List.reserve (); cout << list << Endl;}
C + + Template implementation single-necklace list