| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 605 人关注过本帖
标题:指针链表类
只看楼主 加入收藏
ou1111
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:87
专家分:162
注 册:2010-10-26
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
指针链表类
麻烦各位帮忙看看,哪错了,该怎么修改


#include <iostream>
using namespace std;
struct node
{
 int data;
 struct node *next;
};

class list
{
private:
    node *head;
    node *tail;
    int length;
public:
    list()
    {head=NULL;tail=NULL;}

    void insert(int x)
    {
        
      if(head=new node)  cout<<"this is a new list"<<endl;
      
      node *p;
      p=new node;
      p->next=NULL;
      p->data=x;
      if(p->data==NULL) cout<<"error"<<endl;
     if(head->next==NULL)
        head=p;
     else

         tail->next=p;
     tail=p;
     length++;
     }

    void put()
    {
        node *q;
        q=new node;
        q->next=NULL;
        if(head->next=NULL)  cout<<"error"<<endl;
        while(!head->next)
        {
          cout<<q->data<<endl;

          q=q->next;
        }
          cout<<length<<endl;
     }
      ~list()
      {  node *q=NULL;
        while(head)
        {
           q=head;
           head=head->next;
           delete q;        
        }
        head=NULL;
        tail=NULL;            
      }   

};

void main()
{
   list obj1;
   int x;
   cout<<"print x"<<endl;
   cin>>x;
   obj1.insert(x);
   obj1.put();
}

[ 本帖最后由 ou1111 于 2010-10-29 12:58 编辑 ]
搜索更多相关主题的帖子: 指针 链表 
2010-10-29 12:37
shafeilong
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:4
帖 子:236
专家分:1434
注 册:2009-3-21
收藏
得分:5 
你~list()怎么可以放在put()里面??



#include <iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};

class list
{
private:
    node *head;
    node *tail;
    int length;
public:
    list()
    {head=NULL;tail=NULL;}


    ~list()
    {  
        node *q=NULL;
        while(head)
        {
            q=head;
            head=head->next;
            delete q;        
        }
        head=NULL;
        tail=NULL;            
    }  

    void insert(int x)
    {
        
      if(head=new node)  cout<<"this is a new list"<<endl;
      
      node *p;
      p=new node;
      p->next=NULL;
      p->data=x;
      if(p->data==NULL) cout<<"error"<<endl;
      if(head->next==NULL)
          head=p;
      else
         
          tail->next=p;
      tail=p;
      length++;
    }
   
    void put()
    {
        node *q;
        q=new node;
        q->next=NULL;
        if(head->next=NULL)  cout<<"error"<<endl;
        while(!head->next)
        {
            cout<<q->data<<endl;
            
            q=q->next;
        }
        cout<<length<<endl;
    }

        
};
   
    void main()
    {
        list obj1;
        int x;
        cout<<"print x"<<endl;
        cin>>x;
        obj1.insert(x);
        obj1.put();
}
2010-10-29 12:44
ou1111
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:87
专家分:162
注 册:2010-10-26
收藏
得分:0 
回复 2楼 shafeilong
失误,不小心把一括号删了,这里的问题是运行不对,调试中显示          cout<<q->data<<endl;
这句有问题,该怎么改呀

[ 本帖最后由 ou1111 于 2010-10-29 12:56 编辑 ]
2010-10-29 12:52
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:15 
程序代码:
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};

class list
{
private:
    node *head;
    node *tail;
    int length;
public:
    list()
    {
        head=NULL;
        tail=NULL;
        length=0;
    }

    void insert(int x)
    {
      
      node *p;
      p=new node;
      p->next=NULL;
      p->data=x;
      if(p->data==NULL) cout<<"error"<<endl;
     if(head==tail)
     {
        cout<<"this is a new list"<<endl;
        head=new node;
        tail=new node;
        head=p;
     }
     else
        tail->next=p;
     tail=p;
     length++;
     }

    void put()
    {
        node *q;
        q=new node;
        //q->next=NULL;
        q=head;
        if(head==NULL)  cout<<"error"<<endl;
        while(q)
        {
          cout<<q->data<<endl;
          q=q->next;
        }
          cout<<length<<endl;
     }
      ~list()
      {  node *q=NULL;
        while(head)
        {
           q=head;
           head=head->next;
           delete q;       
        }
        head=NULL;
        tail=NULL;           
      }    

};

void main()
{
   list obj1;
   int x;
   cout<<"print x"<<endl;
   cin>>x;
   obj1.insert(x);
   obj1.put();
}
好多错误!!帮你改下了!对照看下!

If You Want Something, Go Get It, Period.
2010-10-29 18:23
ou1111
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:87
专家分:162
注 册:2010-10-26
收藏
得分:0 
回复 4楼 m21wo
非常感谢,我刚学C++,懂得好少
2010-10-29 21:10
快速回复:指针链表类
数据加载中...
 
   



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

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