注册 登录
编程论坛 VC++/MFC

指针链表类

ou1111 发布于 2010-10-29 12:37, 605 次点击
麻烦各位帮忙看看,哪错了,该怎么修改


#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 编辑 ]
4 回复
#2
shafeilong2010-10-29 12:44
你~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();
}
#3
ou11112010-10-29 12:52
回复 2楼 shafeilong
失误,不小心把一括号删了,这里的问题是运行不对,调试中显示          cout<<q->data<<endl;
这句有问题,该怎么改呀

[ 本帖最后由 ou1111 于 2010-10-29 12:56 编辑 ]
#4
m21wo2010-10-29 18:23
程序代码:
#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();
}
好多错误!!帮你改下了!对照看下!
#5
ou11112010-10-29 21:10
回复 4楼 m21wo
非常感谢,我刚学C++,懂得好少
1