注册 登录
编程论坛 C++教室

麻烦帮忙改一下

lovejj23 发布于 2011-06-29 20:17, 516 次点击
#include <iostream>
using namespace std;
class Link
{
public:
    int x;
    Link *next;
public:
    static Link *head;
    Link(int a)
    {
        Link *p=this;
        p->x=a;
        p->next=head->next;
        head->next=p;
    }
};
Link* Link::head=NULL;
void main()
{
    Link s1(1);
    Link s2(2);   
    Link s3(3);
    Link *p=Link::head;
    for(p)
    {
        cout<<p->x;
        p=p->next;
    }
        //p
}
以上是个程序意思很简单,就是用类建立一个链表并把它输出来,唯一要求是链表的头指针是类中的静态成员变量。
我编译时出错了,但是找不出来麻烦高手帮看看吧!
2 回复
#2
ToBeStronger2011-06-30 02:05
你那行for(p)是什么东西??
#3
lovejj232011-06-30 19:00
回复 2楼 ToBeStronger
#include <iostream>
using namespace std;
class Link
{
private:
    int x;
    Link *next;
public:
    static Link *head;
    Link(int a)
    {
        x=a;
        this->next=head;
        head=this;
    }
    static void display()
    {
        Link *p=head;
        while(p)
        {
            cout<<p->x<<endl;
            p=p->next;
        }
    }
    ~Link()
    {
        Link *p;
        p=head;
        while(p->next!=NULL)
        {
            if(p->next==this)
            {
                cout<<this->x<<endl;
                p->next=this->next;
                cout<<"找到了该链表!"<<endl;
                break;
            }
            if(p==this)
            {
                cout<<"找到了该链表!"<<endl;
                cout<<this->x<<endl;
                head=this->next;
                break;
            }
            p=p->next;
            
        }
        if(p->next==NULL)
        {
            cout<<"找到了该链表!"<<endl;
            cout<<this->x<<endl;
        }
        cout<<"析构成功!"<<endl;
    }

};
Link* Link::head=NULL;

void check()
{
    Link s(23);
    cout <<"Test:" << endl;
    Link::display();
}

void main()
{
    Link s1(1);
    Link s2(2);   
    Link s3(3);
    check();
    cout << "main:" << endl;
    s1.display();
   
    //s3.display();
}
我自己改好了 谢了!
1