C++链表编译导致内存报错

来源:百度知道 编辑:UC知道 时间:2024/06/08 00:38:01
#include<iostream.h>
class A
{
public:
int a;
int b;
A *next;
};

static int n=0;

A *head=0;

A *creat()
{
A *p1,*p2;
p1=new A;
p1->b =0;
head=p1;
p2=p1;
cout<<"Please Input a number:";
cin>>p1->a;
if(p1->a==0)
{
delete p1;p2=0;p2->next=0;head=0;return head;
}
p1->b=++n;
while(p1->a!=0)
{
p2=p1;
p1=new A;
cout<<"Please Input a number:";
cin>>p1->a;
p1->b=++n;
p2->next=p1;
}
delete p1;
p2->next=0;
return head;
}

void show(A*head)
{
while(head)
{
cout<<"a("<<head->b<<")="<<head->a<<endl;
head=head->next;
}

}

void deletell(A*head,int num

if(p1->a==0)
{
delete p1;p2=0;p2->next=0;head=0;return head;
}
p2 = 0; 然后 p2->next =0;

0->next 能让你写么

我也试过,慢慢检查吧

//代码中搜索:Lazy_sleeping

#include<iostream.h>
class A
{
public:
int a;
int b;
A *next;
};

static int n=0;

A *head=0;

A *creat()
{
A *p1,*p2;
p1=new A;
p1->b =0;
head=p1;
p2=p1;
cout<<"Please Input a number:";
cin>>p1->a;
if(p1->a==0)
{
delete p1;
//being mofidy by Lazy_sleeping
p2->next=0;
p2=0;
//end mofidy by Lazy_sleeping
head=0;
return head;
}
p1->b=++n;
while(p1->a!=0)
{
p2=p1;
p1=new A;
cout<<"Please Input a number:";
cin>>p1->a;
p1->b=++n;
p2->next=p1;