| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 516 人关注过本帖
标题:麻烦帮忙改一下
只看楼主 加入收藏
lovejj23
Rank: 2
等 级:论坛游民
帖 子:20
专家分:41
注 册:2011-4-17
结帖率:100%
收藏
已结贴  问题点数:0 回复次数:2 
麻烦帮忙改一下
#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
}
以上是个程序意思很简单,就是用类建立一个链表并把它输出来,唯一要求是链表的头指针是类中的静态成员变量。
我编译时出错了,但是找不出来麻烦高手帮看看吧!
搜索更多相关主题的帖子: include public 
2011-06-29 20:17
ToBeStronger
Rank: 4
等 级:业余侠客
帖 子:61
专家分:239
注 册:2011-4-8
收藏
得分:5 
你那行for(p)是什么东西??
2011-06-30 02:05
lovejj23
Rank: 2
等 级:论坛游民
帖 子:20
专家分:41
注 册:2011-4-17
收藏
得分:0 
回复 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();
}
我自己改好了 谢了!
2011-06-30 19:00
快速回复:麻烦帮忙改一下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018853 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved