读书人

一个小程序 为什么不能运行啊该如何解

发布时间: 2012-04-05 12:42:40 作者: rapoo

一个小程序 为什么不能运行啊?
#include <iostream.h>
#include <stdio.h>
class ListNode
{
friend class List;


double value;

ListNode * next;

};

class List

{

int nCount;

ListNode * head;

public:

List() //无参构造函数

{nCount=0;
head->next=NULL;

}

/* List(List &l) //拷贝构造函数

{int i;
for(i=l.nCount;i>=0;i--)
List->value=l->value;

}*/

~List() //析构函数

{
delete head;
}

List & operator = (List & l) //重载等号运算符

{ nCount=l.nCount;
head=l.head;
return *this;

}

void Add(double d) //添加一个元素

{ ListNode *p,*q;
p=new ListNode;
q=head;
while(q->next!=NULL)
{q=q->next;
}
q->next=p;
p->value=d;
nCount++;
}



void Remove(double d) //删除一个元素

{ListNode *p=head->next;
ListNode *s=head;
while(p->value!=d)
{p=p->next;
s++;
}
s->next=p->next;
delete(p);
nCount--;
}

void Print()

{

if( head==NULL) return;

cout << "LIST(" << nCount <<") =" << head->value;

ListNode * p=head->next;

while(p!=NULL)

{

cout << ", "<< p->value;

p=p->next;

}

cout <<endl;

}

};





void main()
{
List list1;
double d;
int n;
cin >>n;
while (n>0)
{
cin >>d;
list1.Add(d);
n--;
}
list1.Print();
List list2;
list2.Add(2.4);
list2.Add(6.1);
list2.Print();
list2=list1;
cin >>d;
list2.Remove(d);
cin >>d;
list2.Remove(d);
list2.Print();
}




[解决办法]
无参构造函数 List() 里 head->next=NULL;会出错,因为head未指向一个结构体实例,应该先
head=new ListNode();head->next=NULL;这也许只是其中一个错误,,其它的自己再找找
[解决办法]

C/C++ code
#include <iostream>using namespace std;#include <stdio.h>class ListNode{    friend class List;    double value;    ListNode * next;};class List{    int nCount;    ListNode * head;public:    List() //无参构造函数    {        nCount=0;        head=NULL;    }    ~List() //析构函数    {        ListNode* p = head,*q;        while(p)        {            q = p->next;            delete p;            p = q;        }    }    List & operator = (List & l) //重载等号运算符    {         nCount=l.nCount;        head=l.head;        return *this;    }    void Add(double d) //添加一个元素    {        if (head == NULL)        {            head = new ListNode;            head->value = d;            head->next = NULL;            return;        }        ListNode *p,*q;        p=new ListNode;        p->next = NULL;        q=head;        while(q->next!=NULL)        {            q=q->next;        }        q->next=p;        p->value=d;        nCount++;    }    void Remove(double d) //删除一个元素    {        ListNode *p;        if(head->value==d)        {            p = head;            head = head->next;            delete p;            return;        }        p=head->next;        ListNode *s=head;        while(p->value!=d)        {            p=p->next;            s=s->next;        }        s->next=p->next;        delete(p);        nCount--;    }    void Print()    {        if( head==NULL) return;        cout << "LIST(" << nCount <<") =" << head->value;        ListNode * p=head->next;        while(p!=NULL)        {            cout << ", "<< p->value;            p=p->next;        }        cout <<endl;    }};void main(){    List list1;    double d;    int n;    cin >>n;    while (n>0)    {        cin >>d;        list1.Add(d);        n--;    }    list1.Print();    List list2;    list2.Add(2.4);    list2.Add(6.1);    list2.Print();    list2=list1;    cin >>d;    list2.Remove(d);    cin >>d;    list2.Remove(d);    list2.Print();} 


[解决办法]

C/C++ code
#include <iostream.h>#include <stdio.h>class ListNode{    friend class List;    double value;    ListNode * next;};class List{    int nCount;    ListNode * head;public:    List() //无参构造函数    {        nCount=0;        head = new ListNode;//<----------------------------------here        head->next=NULL;    }  /* List(List &l) //拷贝构造函数  {int i;for(i=l.nCount;i>=0;i--)List->value=l->value;  }*/~List() //析构函数{    RemoveAll();}void RemoveAll()//<-----------------------------增加一个函数{    ListNode *temp = NULL;    while (head!=NULL)    {        temp=head;        head=head->next;        delete temp;    }}List & operator = (List & l) //重载等号运算符{    ListNode *p, *s;        if (&l != this)    {        nCount=l.nCount;        this->RemoveAll();    }    head=new ListNode;    head->next=NULL;    p=head;    s=l.head;    while (s->next)    {        s=s->next;        p->next=new ListNode;        p->next->value=s->value;        p=p->next;    }      p->next=NULL;      return *this;}void Add(double d) //添加一个元素{    ListNode *p,*q;    p=new ListNode;    q=head;    while(q->next!=NULL)    {        q=q->next;    }    q->next=p;    p->value=d;    p->next=NULL;//<---------------------------------------------------here    nCount++;}    void Remove(double d) //删除一个元素{    ListNode *p=head->next;    ListNode *s=head;    while(p->value!=d)    {        p=p->next;        s=s->next;//<----------------------------here    }    s->next=p->next;    delete(p);    nCount--;  }void Print(){    if( head==NULL) return;    cout << "LIST(" << nCount <<") =";//<-----------------------------here    ListNode * p=head->next;    while(p->next!=NULL)// here    {        cout << p->value<<", ";        p=p->next;    }    cout<<p->value;//<--------------------------------here    cout <<endl;  }};void main(){     List list1;     double d;     int n;     cin >>n;     while (n>0)     {          cin >>d;          list1.Add(d);          n--;     }     list1.Print();     List list2;     list2.Add(2.4);     list2.Add(6.1);     list2.Print();     list2=list1;     list2.Print();//<-------------------here     cin >>d;     list2.Remove(d);     cin >>d;     list2.Remove(d);     list2.Print();}
[解决办法]
list2=list1;//你把1给2,那2原来的所有节点不就全丢了?为什么要这样做?
此外,声明一个新的对象list2,当赋给它链表时,两个指针指向了同一链表,当运行结束,调用析构函数释放内存时就会出现释放"悬挂指针"的内存错误

operator=重载问题,用智能指针吧
[解决办法]
改动了你的函数
C/C++ code
void Remove(double d) //删除一个元素    {        ListNode *p = head, *q = head;        if (head == NULL)//表空退出        {            return;        }        while (p && p->value != d)//寻寻觅觅        {            q = p;            p=p->next;        }        if (p == NULL)//众里寻他千百度,蓦然回首TMD的没有        {            return;        }        //找到了,那就让它滚蛋,而且快快的        if (q == head)//第一个结点——这里头结点你存数据了,只能特殊时期,特殊对待        {            head = head ->next;            delete q;            nCount--;        }        else        {            q->next = p->next;            delete(p);            nCount--;        }    } 

读书人网 >C++

热点推荐