这个程序运行的时候为什么会出错
这个程序错在哪里了,望大虾帮忙找找 ,多谢了
#include <iostream>
#include<ctime>
using namespace std;
struct listnode
{int data;
listnode *next;
} ;
class list
{
private:
listnode *first;
listnode *last;
int size;
public:
list();
~list();
friend ostream & operator<<(ostream & os,const list & x)
{
listnode *p=new listnode;
p=x.first;
while(p!=0)
{
os<<p->data<<" ";
p=p->next;
}
os<<endl;
return os;
}
void insert(int x);
};
list::list(): first(0),last(0),size(0){}
list::~list()
{
listnode *p=new listnode;
listnode *q=new listnode;
p=first;
while(q->next!=0)
{
q=p;
delete p;
p=q->next;
}
}
void list::insert(int x)
{
listnode *p=new listnode;
p->data=x;
p->next=0;
if (first==0)
{
first=last=p;
}
else
{
last->next=p;
last=p;
}
size++;
}
int main()
{
list list1;
srand((unsigned int)time(NULL));
for(int i=0;i<10;i++)
{
list1.insert(rand() % 10);
}
cout<<list1;
return 0;
}