#include <iostream> #include <string.h> using namespace std; class Student { public: Student (char *name); ~Student(); public: char name[30]; Student *next; static Student *point; }; Student::Student (char *name) { strcpy(Student::name,name); this->next=point; point=this; } Student::~Student ()//析构过程就是节点的脱离过程 { cout<<"析构:"<<name<<endl; if(point==this) { point=this->next; cin.get(); return; } for(Student *ps=point;ps;ps=ps->next) { if(ps->next==this) { cout<<ps->next<<"|"<<this->next<<endl; ps->next=next;//=next也可以写成this->next; cin.get(); return; } } cin.get(); } Student* Student::point=NULL; int main() { Student *c = new Student("marry"); //注意这里用的*c Student a("colin"); Student b("jamesji"); delete c; //所以才能用delete Student *fp=Student::point; while(fp!=NULL) { cout<<fp->name<<endl; fp=fp->next; } cin.get(); }
this替代了以前的current指针。 全局指针在这里被类的静态成员指针所替代(类的静态成员完全可以替代全局变量
是等链表都输出完后才开始析构吗?
析构函数
for(Student *ps=point;ps;ps=ps->next)
{
if(ps->next==this)
{
cout<<ps->next<<"|"<<this->next<<endl;
ps->next=next;//=next也可以写成this->next;
cin.get();
return;
}
}
是都没用到吗?