求教,帮忙看看这程序,这错误是什么意思
#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;
}
运行后,结果是有的,但会弹出一个错误如图: