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;
            }
        }
    是都没用到吗?
  • 相关阅读:
    poj 1579(动态规划初探之记忆化搜索)
    hdu 1133(卡特兰数变形)
    CodeForces 625A Guest From the Past
    CodeForces 625D Finals in arithmetic
    CDOJ 1268 Open the lightings
    HDU 4008 Parent and son
    HDU 4044 GeoDefense
    HDU 4169 UVALive 5741 Wealthy Family
    HDU 3452 Bonsai
    HDU 3586 Information Disturbing
  • 原文地址:https://www.cnblogs.com/qbmiller/p/3810875.html
Copyright © 2011-2022 走看看