直接上代碼了,說(shuō)明看注釋就可以:
利用靜態(tài)成員構(gòu)建鏈表
#include <IOSTREAM.H>  class Node { public:   Node(int val, Node* next):val(val),next(next){}   //~Node(){cout<<"del "<<val<<endl;}   static void showAll();//打印全部節(jié)點(diǎn)的值   static void insertHead(int);//頭插   static void insertTail(int);//尾插   static void delHead();//刪頭   static void delTail();//刪尾   static void clear();//清空 protected:   int val;   Node *next;   static Node *head; private: };  Node* Node::head = 0;  void Node::showAll(){//打印全部節(jié)點(diǎn)的值   Node *p = head;   while (p)   {     cout<<p->val<<" ";     p = p->next;   }   cout<<endl; }  void Node::insertHead(int val){//頭插   Node *p = new Node(val, head);   head = p; }  void Node::insertTail(int val){//尾插   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::delHead(){//刪頭   Node *p = head;   if (head)   {     head = head->next;     delete p;   } }  void Node::delTail(){//刪尾   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(){//清空   Node *p = head;   Node *q = 0;   head = 0;   while (p)   {     q = p;     p = p->next;     delete q;   } }  void main(){   Node::delHead();   Node::delTail();   Node::insertTail(2);   Node::delTail();   for (int i = 0; i < 10; i++)   {     Node::insertTail(i + 1);   }   Node::delTail();   Node::showAll(); } 利用類(lèi)模板構(gòu)建鏈表
這有點(diǎn)類(lèi)似于list<>:
#include <iostream> #include <string> using namespace std;  template<class T> class Node//創(chuàng)建一個(gè)類(lèi)模板,一個(gè)可以放入任何類(lèi)型節(jié)點(diǎn)的鏈表 { public:   Node(T val, Node* next):val(val),next(next){}   static void showAll();//打印全部節(jié)點(diǎn)的值   static void insertHead(T);//頭插   static void insertTail(T);//尾插   static void delHead();//刪頭   static void delTail();//刪尾   static void clear();//清空 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(){//打印全部節(jié)點(diǎn)的值   Node *p = head;   while (p)   {     cout<<p->val<<" ";     p = p->next;   }   cout<<endl; }  template<class T> void Node<T>::insertHead(T val){//頭插   Node *p = new Node(val, head);   head = p; }  template<class T> void Node<T>::insertTail(T val){//尾插   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>::delHead(){//刪頭   Node *p = head;   if (head)   {     head = head->next;     delete p;   } }  template<class T> void Node<T>::delTail(){//刪尾   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(){//清空   Node *p = head;   Node *q = 0;   head = 0;   while (p)   {     q = p;     p = p->next;     delete q;   } }  class Student//創(chuàng)建一個(gè)自定義的學(xué)生類(lèi) { public:   Student(string name, int age,char sex):name(name), age(age), sex(sex){}   void showInfo(){     cout<<"姓名:"<<name<<" 年齡:"<<age<<" 性別:"<<sex<<endl;   } protected:   string name;   int age;   char sex; private: };  void Node<Student>::showAll(){//學(xué)生類(lèi)節(jié)點(diǎn)和其他基本數(shù)據(jù)類(lèi)型不同,不能直接用<<輸出,所以重載showAll()   Node *p = head;   while (p)   {     p->val.showInfo();     p = p->next;   } }  void main(){   for (int i = 1; i < 10; i++)   {     Node<int>::insertTail(i);//這時(shí)Node<int>稱(chēng)為一個(gè)用類(lèi)模板生成的模板類(lèi)     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(); } 
新聞熱點(diǎn)
疑難解答