标题:求教,帮忙看看这程序,这错误是什么意思
只看楼主
花开有期
Rank: 1
等 级:新手上路
帖 子:9
专家分:7
注 册:2012-7-2
结帖率:100%
已结贴  问题点数:20 回复次数:6 
求教,帮忙看看这程序,这错误是什么意思
#include <iostream>
using namespace std;

class Chain;

class Node{
    friend Chain;
private:
    int data;
    Node* next;
};

class Chain{
private:
    Node *first;
    Node *rear;
public:
    Chain() {first=rear=0;}
    ~Chain();
    bool IsEmpty() const {return 0==first ;}
    int Length() const;
    bool Find(int k,int& x) const;
    int Search(const int& x) const;
    Chain& Delete(int k,int& x);
    Chain& Insert(int k,const int& x);
    void Output(ostream& out) const;
    Chain& Sort();
    Chain& reverse();
    int mid() ;
//    friend ostream& operator<< (ostream& out,const Chain& p);
};
Chain::~Chain()
{
    Node* p;
    while (first)
    {
        p=first->next;
        delete first;
        first=p;
    }
}
int Chain::Length() const
{
    if (!first) return 0;
    int num=1;
    Node* p=first->next;
    while (p != first)
    {
        num++;
        p=p->next;
    }
    return num;
}
bool Chain::Find(int k,int& x) const
{
    if ((k >0) && (k <= Length()))
    {
        int index=1;
        Node* p=first;
        while (index < k)
        {
            index++;
            p=p->next;
        }
        if (p)
        {
            x=p->data;
            return true;
        }
    }
    return false;
}
int Chain::Search(const int& x) const
{
    if (x == first->data)
    {
        return 1;
    }
    int index=1;
    Node* p=first->next;
    while (p != first)
    {
        index++;
        if (x == p->data)
            return index;
        p=p->next;
    }
    return 0;
}
Chain& Chain::Delete(int k,int& x)
{
    if ((k > 0) && (k <= Length()))
    {
        if (1 == k)
        {
            Node* p=first;
            first=first->next;
            delete p;
            rear->next=first;
        }
        
        else
        {
            Node *p=first;
            Node* q;
            int index=1;
            while (index < k)
            {
                q=p;
                p=p->next;
                index++;
            }

            q->next=p->next;
            x=p->data;
            if (k == Length())
                rear=q;
            delete p;
        }
    }
    return *this;
}
Chain& Chain::Insert(int k,const int& x)
{
    if (k > 0)
    {
        if (1 == k)
        {
            Node* p=new Node;
            p->data=x;
            p->next=first;
            first=p;
            //rear->next=first;
            if (!rear)
            {
                rear=first;
                rear->next = first;
            }
//             else
//                 rear->next=first;
        }
        else if (k > Length())
        {
            Node *p=new Node;
            p->data=x;
            rear->next=p;
            p->next=first;
            rear=p;
        }
        else
        {
            int index=1;
            Node *p=first;
            Node* q;
            while (index < k-1)
            {
                index++;
                p=p->next;
            }
            
                q=new Node;
                q->data=x;
                q->next=p->next;
                p->next=q;
               
        }
    }
    return *this;
}

Chain& Chain::Sort()
{
    int len=Length();
    int temp;
    Node* p;
    for (int i=1;i<len;i++)
    {
        p=first;
        for (int j=1;j <= len-i;j++)
        {
            if (p->data > p->next->data)
            {
                temp=p->data;
                p->data = p->next->data;
                p->next->data =temp;
            }
            p = p->next;
        }
    }
    return *this;
}

Chain& Chain::reverse()
{
    int len=Length();
    int temp;
    Node* p;
    for (int i=1;i < len;i++)
    {
        p=first;
        for (int j=1;j <= len - i;j++)
        {
            temp=p->data;
            p->data = p->next->data;
            p->next->data=temp;
            p=p->next;
        }
    }
   
//     for (int i=1;i<len;i++)
//     {
//         p=first;
//         for (int j=1;j <= len-i;j++)
//         {
//         //    if (p->data > p->next->data)
//         //    {
//                 temp=p->data;
//                 p->data = p->next->data;
//                 p->next->data =temp;
//         //    }
//             p = p->next;
//         }
//     }
    return *this;
}

int Chain::mid()
{
    Node* p=first;
    Node* q=first;

    while ((p->next != first) && (p->next->next != first))
    {
        q = q->next;
        p= p->next->next;

    }
    int i=q->data;
    return i;
}

void Chain::Output(ostream& out) const
{
    if ( first)
    {
        out<<first->data<<"   ";
        Node* current=first->next;

        for (;current != first;current=current->next)
        {
            out<<current->data<<"   ";
        }
    }
}

ostream& operator<<(ostream& out,const Chain& v)
{
    v.Output(out);
    return out;
}



int main()
{
    Chain aa;
    int x;
    cout<<aa.IsEmpty()<<endl;
     aa.Insert(1,1);
     aa.Insert(2,2);




    return 0;
}

运行后,结果是有的,但会弹出一个错误如图:

搜索更多相关主题的帖子: next void private include public 
2012-07-02 20:24
花开有期
Rank: 1
等 级:新手上路
帖 子:9
专家分:7
注 册:2012-7-2
得分:0 
额,图片没粘上去,补上:
2012-07-02 20:26
花开有期
Rank: 1
等 级:新手上路
帖 子:9
专家分:7
注 册:2012-7-2
得分:0 
怎么没人啊,这错误是执行完Main函数弹出来的
2012-07-02 20:56
lxqlyld
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:116
专家分:125
注 册:2012-6-18
得分:8 
你的程序我编译没有错误,可以运行,也没弹出你说的错误,我用的是C++ Builder 6.0编译器
2012-07-03 13:37
jiantiewen
Rank: 5Rank: 5
等 级:职业侠客
威 望:2
帖 子:61
专家分:307
注 册:2012-6-22
得分:12 
应该就是释构函数有问题。你的链表中,尾节点的下一节点是指向头节点,而不是0。释构函数在删除节点时,删除了最后一个节点后,又指向了一开始就被删掉的头节点的内存地址,于是报错了。
2012-07-04 03:22
花开有期
Rank: 1
等 级:新手上路
帖 子:9
专家分:7
注 册:2012-7-2
得分:0 
回复 3楼 花开有期
我的也是6.0版本啊,调试无数次还是有,不知道原因
2012-07-04 09:22
花开有期
Rank: 1
等 级:新手上路
帖 子:9
专家分:7
注 册:2012-7-2
得分:0 
回复 4楼 lxqlyld
谢谢哈,是这原因,调试通过了,呵呵
2012-07-04 10:00



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-372675-1-1.html




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

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