C++應用靜態成員或類模板構建鏈表的辦法講授。本站提示廣大學習愛好者:(C++應用靜態成員或類模板構建鏈表的辦法講授)文章只能為提供參考,不一定能成為您想要的結果。以下是C++應用靜態成員或類模板構建鏈表的辦法講授正文
直接上代碼了,解釋看正文便可以:
應用靜態成員構建鏈表
#include <IOSTREAM.H>
class Node
{
public:
Node(int val, Node* next):val(val),next(next){}
//~Node(){cout<<"del "<<val<<endl;}
static void showAll();//打印全體節點的值
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(){//打印全體節點的值
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();
}
應用類模板構建鏈表
這有點相似於list<>:
#include <iostream>
#include <string>
using namespace std;
template<class T> class Node//創立一個類模板,一個可以放入任何類型節點的鏈表
{
public:
Node(T val, Node* next):val(val),next(next){}
static void showAll();//打印全體節點的值
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(){//打印全體節點的值
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//創立一個自界說的先生類
{
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(){//先生類節點和其他根本數據類型分歧,不克不及直接用<<輸入,所以重載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);//這時候Node<int>稱為一個用類模板生成的模板類
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();
}