c++程序出现“程序产生一个访问违例(段异常)”

来源:百度知道 编辑:UC知道 时间:2024/09/24 09:02:55
/*
创建一个链表,每个结点是一个student结构对象,
Struct student{
Name; char[]类型
Hobby; string类型
Age;
};
至少包含3个元素。
输出这个链表的所有元素。
*/
//这个题我不小心做成前插法。。。所以先输入的是最后的
#include<iostream>
#include<string>
using namespace std;
//创建链表
struct Student
{
char Name[30];
string Hobby;
int Age;
Student *next;
};
int main()
{

int i=1;//用来输出学生的个数
//创建链表
Student *head=new Student;//头结点
Student *s=new Student;//想要删除的学生的信息
Student *r=new Student;//用来记录想要删除的结点的前一个结点
int k=0;//控制是否找到该学生的信息
head = NULL;
//输入录入的学生的信息
while(1)
{
Student *p=new Student;//用来装输入的没一个学生的信息
cout<<"请输入学生的名字:";
cin.getline(p->Name,30);
if(strcmp(p->Name,"NULL")==0)
break;<

while(1) //这个不是死循环吗? 这样后while后面的还能执行吗?
{
Student *p=new Student;//用来装输入的没一个学生的信息
cout<<"请输入学生的名字:";
cin.getline(p->Name,30);
if(strcmp(p->Name,"NULL")==0)
break;
else
{
cout<<"请输入该学生的爱好:";
cin>>p->Hobby;
cout<<"请输入该学生的年龄:";
cin>>p->Age;
cin.get();
p->next = head;
head=p;
}
delete p;
}