zoukankan      html  css  js  c++  java
  • C++链表

    #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;
            }
        }
    是都没用到吗?
  • 相关阅读:
    css自适应
    css居中
    js生成签名
    javascript与Android、IOS交互
    js截取路径参数
    js date对象
    js判断设备、浏览器类型
    live555实践
    关于django
    mysql的基本知识
  • 原文地址:https://www.cnblogs.com/qbmiller/p/3810875.html
Copyright © 2011-2022 走看看