老大帮下我
请高手指点下小弟刚学C++昨天写了一点链表方面的东西, 可运行时老是出现内存错误,但也可运行
哪位朋友给指点下,谢谢了,下面上传的有工作文件
代码如下:
#include<iostream.h>
#include<string.h>
struct student{
char name[20];
unsigned long no;
student *next;};
void main ()
{cout<<" 学生学号登记系统\n ××××********************××××\n";
cout<<"依次输入学生姓名、学号,以姓名“over”作为结束输入标志\n请输入:\n";
student *head,*n;
char name[20] ;
unsigned long no;
for(;;)
{cin>>name;
if(!strcmp(name,"over")) break;
cin>>no;
n=new student;
strcpy(n->name,name);
n->no=no;
if(head==NULL)
{head=n;
n->next=NULL;}
else {
n->next=head;
head=n;}
}
cout<<"*******************************\n所有学生信息如下:\n";
for(student *p=head;p!=NULL;p=p->next)
cout<<p->name<<'\t'<<p->no<<endl;
student *q;
while(head)
{
q=head;head=head->next;delete q;
}
} //将HEAD初始化为NULL
#include<iostream.h>
#include<string.h>
struct student
{
char name[20];
unsigned long no;
student *next;
};
void main ()
{
cout<<" 学生学号登记系统\n ××××********************××××\n";
cout<<"依次输入学生姓名、学号,以姓名“over”作为结束输入标志\n请输入:\n";
student *head=NULL,*n;
char name[20] ;
unsigned long no;
for(;;)
{
cin>>name;
if(!strcmp(name,"over")) break;
cin>>no;
n=new student;
strcpy(n->name,name);
n->no=no;
if(head==NULL)
{
head=n;
n->next=NULL;
}
else
{
n->next=head;
head=n;
}
}
cout<<"*******************************\n所有学生信息如下:\n";
for(student *p=head;p!=NULL;p=p->next)
cout<<p->name<<'\t'<<p->no<<endl;
student *q;
while(head)
{
q=head;head=head->next;delete q;
}
} 太感谢了,
页:
[1]
