Directly on the code, the note can be read:
Building a linked list with static members
#include <iostream. H> class Node {public:node (int val, node* next): Val (Val), next (next) {}//~node () {cout<< "Del" <<
Val<<endl;} static void ShowAll ();//print value of all nodes static void inserthead (int);//header static void Inserttail (int);//trailing static void de
Lhead ()//delete header static void Deltail ()//delete tail static void clear ();//Empty protected:int Val;
Node *next;
Static Node *head;
Private:};
node* node::head = 0;
void Node::showall () {//print the value of all nodes node *p = head;
while (p) {cout<<p->val<< "";
p = p->next;
} cout<<endl;
} void Node::inserthead (int val) {//header Node *p = new Node (val, head);
head = p;
} void Node::inserttail (int val) {//Endnote node *p = new Node (val, 0);
if (!head) {head = p;
Return
Node *q = head;
while (q->next) {q = q->next;
} q->next = P;
void node::d elhead () {//delete header Node *p = head; if (head) {head = head->next;
Delete p;
} void Node::d eltail () {//delete tail if (!head) {return; } if (! (
Head->next)) {delete (head);
head = NULL;
Return
Node *p = head;
while (p->next->next) {p = p->next;
} delete (P->next);
P->next = NULL;
} void Node::clear () {//empty Node *p = head;
Node *q = 0;
Head = 0;
while (p) {q = p;
p = p->next;
Delete q;
} void Main () {Node::d elhead ();
Node::d eltail ();
Node::inserttail (2);
Node::d eltail ();
for (int i = 0; i < i++) {Node::inserttail (i + 1);
Node::d eltail ();
Node::showall ();
}
building a linked list using a class template
This is somewhat similar to list<>:
#include <iostream> #include <string> using namespace std;
Template<class t> class node//Create a class template, a linked list that can be put into any type of node {Public:node (T val, node* next): Val (Val), next (next) {} static void ShowAll ();//print value of all nodes static void Inserthead (T),//header static void Inserttail (T),//trailing static void Delh
EAD ()//delete header static void Deltail ()//delete tail static void clear ();//Empty protected:t Val;
Node *next;
Static Node *head;
Private:};
Template<class t> node<t>* node<t>::head = 0;
Template<class t> void Node<t>::showall () {//print the value of all nodes node *p = head;
while (p) {cout<<p->val<< "";
p = p->next;
} cout<<endl;
} template<class t> void Node<t>::inserthead (T val) {//header Node *p = new Node (val, head);
head = p;
} template<class t> void Node<t>::inserttail (T val) {//Endnote node *p = new Node (val, 0);
if (!head) {head = p;
Return
}Node *q = head;
while (q->next) {q = q->next;
} q->next = P;
Template<class t> void Node<t>::d elhead () {//delete Node *p = head;
if (head) {head = head->next;
Delete p;
} template<class t> void node<t>::d eltail () {//delete tail if (!head) {return; } if (! (
Head->next)) {delete (head);
head = NULL;
Return
Node *p = head;
while (p->next->next) {p = p->next;
} delete (P->next);
P->next = NULL;
} template<class t> void Node<t>::clear () {//empty Node *p = head;
Node *q = 0;
Head = 0;
while (p) {q = p;
p = p->next;
Delete q;
Class student//Create a custom student class {Public:student (string name, int age,char sex): Name (name), age (age), sex (sex) {}
void Showinfo () {cout<< "name:" <<name<< "Age:" <<age<< "Sex:" <<sex<<endl; } protected:string NamE
int age;
char sex;
Private:};
void Node<student>::showall () {//Student class node is different from other basic data types and cannot be directly used << output, so overload showall () node *p = head;
while (p) {p->val.showinfo ();
p = p->next; } void Main () {for (int i = 1; i < i++) {node<int>::inserttail (i);//Then node<int> is called a class
Template-Generated template class Node<float>::inserttail (i/10.0f);
Node<double>::inserttail (i/10.00);
Node<student>::inserttail (Student ("Stu", I, ' F '));
} node<int>::showall ();
Node<float>::showall ();
Node<double>::showall ();
Node<student>::showall ();
}