删除链表第一个结点后,为什么会出现乱码和第二结点和后面的东西
#include <iostream>#include<cstring>
#include <fstream>
using namespace std;
struct Student
{
char sex[10]; char ID[25];
int age = 0;
char name[50];
double scoreE;
double scoreM;
double scoreC;
Student *next;
}; Student *p, *q, *head;
void input(Student *&head)
{
char ch;
cout << "输入数据(y/n):";
cin >> ch;
if (ch == 'y'||ch=='Y')
{
p = new Student;//动态分配空间
cout << "input ID:" << endl;
cin >> p->ID; ///将学号录入p指针指向的结构体里的ID变量
cout << " input sex(m/w):" << endl;
cin >> p->sex;//将性别录入p指针指向的结构体里的sex变量
cout << " input age:" << endl;
cin >> p->age;//将年龄录入p指针指向的结构体里的age变量
cout << "输入学生的名字:" << endl;
cin >> p->name;//将姓名录入p指针指向的结构体里的name变量
cout << " 输入学生的英语、数学、程序设计的成绩:" << endl;
cin >> p->scoreE >> p->scoreM >> p->scoreC;
}
else goto L0; //若输入n,则返回菜单栏
while (ch == 'y' || ch == 'Y') //输入下一个同学的信息
{
if (head == NULL) head = p;
else
q->next = p;
q = p; cout << "输入数据(y/n):" << endl;
cin >> ch;
if (ch == 'y' || ch == 'Y')
{
p = new Student;
cout << " input ID:" << endl; cin >> p->ID; //将学号录入p指针指向的结构体里的ID变量
cout << " input sex:" << endl; cin >> p->sex; //将性别录入p指针指向的结构体里的sex变量
cout << "input age:" << endl; cin >> p->age; //将年龄录入p指针指向的结构体里的age变量
cout << "输入学生的名字:" << endl; cin >> p->name; //将姓名录入p指针指向的结构体里的name变量
cout << " 输入学生的英语、数学、程序设计的成绩:" << endl;
cin >> p->scoreE >> p->scoreM >> p->scoreC; //将各科成绩录入p指针指向的结构体里的scoreE、scoreM、scoreC变量
}
}
q->next = NULL;
L0:return;
}
void show(Student * head)
{
if (!head) { cout << "\t空链表!" << endl; goto L1; }
cout << "\t链表中的数据是: \n";
while (head)
{
cout << "student's ID:\t" << head->ID << endl;
cout << "student's sex:\t" << head->sex << endl;
cout << "student's age:\t" << head->age << endl;
cout << "student's name':" << head->name <<endl;
cout << " 输入学生的英语、数学、程序设计的成绩:" << endl;
cout << head->scoreE << '\t' << head->scoreM << '\t' << head->scoreC<<endl;
head = head->next; //head指针往下一个结构体指
}
cout << endl;
L1:return;
}
//**************************************************************删除函数,问题所在,求救
void deleting(Student *head)
{
char sname[20];
cout << "input the deleted student's name" << endl; cin >> sname;/*输入要删除的学生的姓名*/
Student *p;
if (!head)
{
cout << "List null!\n"; return;
}
if (strcmp(head->name, sname) == 0)/*如果链表头的学生信息即为所求
则将p指向链表头,head指针指向链表第二个结构体,使其成为新的链表头
然后释放原来链表头的空*/
{
p = head; head = head->next; delete p; p = NULL;
cout << sname << "'s'information have been deleted.\n"; return;
}
for (Student *pg = head; pg->next; pg = pg->next)
{
if (strcmp(pg->next->name, sname) == 0) //找到了对应的姓名
{
/*将p指向对应姓名的结构体,同时将该结构体的上一个结构体的next指针指向该结构体的下一个结构体
将该结构体的空间释放,则该学生的信息已被删除 */
p = pg->next; pg->next = p->next; delete p; p = NULL;
cout << sname << "'s information have been deleted.\n"; return;
}
}
cout << sname << " She/He is not in this class:" << endl; return;
}
void search(Student*head) {
char sname[20];
cout << "input the deleted student's name" << endl; cin >> sname;//输入想要查询的学生的姓名
Student *p;
if (!head) {
cout << "List nulll!\n"; return;
}
if (strcmp(head->name, sname) == 0)//若链表头的学生信息即为所求,则直接显示出该学生的信息即可
{
cout << "student's ID:\t" << head->ID << endl;
cout << "student's sex:\t" << head->sex << endl;
cout << "student's age:\t" << head->age << endl;
cout << "student's name':" << head->name<<endl;
cout << " 输入学生的英语、数学、程序设计的成绩:" << endl;
cout << head->scoreE << '\t' << head->scoreM << '\t' << head->scoreC<<endl;
return;
}
for (Student *pg = head; pg->next; pg = pg->next)
{
if (strcmp(pg->next->name, sname) == 0) //若pg指向的结构体的下一个结构体的信息即为所求,直接显示即可,
{
cout << "student's ID:\t" << head->ID << endl;
cout << "student's sex:\t" << head->sex << endl;
cout << "student's age:\t" << head->age << endl;
cout << "student's name':" << head->name<<endl;
cout << " 输入学生的英语、数学、程序设计的成绩:" << endl;
cout << head->scoreE << '\t' << head->scoreM << '\t' << head->scoreC<<endl;
return;
}
}
cout << sname << " :She/He is not in this class:" << endl; return;
}
void reserve(Student*head)//保存在文本文件 ,文本文件是顺序存取文件,以默认的方式打开
{
char fileName[30];
if (!head) { cout << "\t空链表!" << endl; return; }
ofstream outstuf; //建立输出文件流对象
cout << "Please input the name of the students'file:" << endl; cin >> fileName;
//输入文件名格式,例如: d:\student.txt
outstuf.open(fileName, ios::out);//连接文件,指定打开方式为写方式
if (!outstuf) {//调用重载算符函数测试流,判断文件是否能打开
cerr << "File couldn't be open!" << endl; abort(); return;
}
outstuf << "\t链表中的数据是: \n";//写入一个标题
while (head)
{ //把链表的信息按顺序写入流中
outstuf << "student's ID:\t" << head->ID << endl;
outstuf << "student's sex:\t" << head->sex << endl;
outstuf << "student's age:\t" << head->age << endl;
outstuf << "student's name':" << head->name<<endl;
outstuf << " 输入学生的英语、数学、程序设计的成绩:" << endl;
outstuf << head->scoreE << '\t' << head->scoreM << '\t' << head->scoreC<<endl;
head = head->next; //head指针往下一个结构体指 针
}
outstuf << endl;
outstuf.close();//关闭文件
return;
}
int main()
{
Student *head = NULL; int key;
int choice, bh;
L:
cout << "\n\n\n\n\n" << endl;
cout << "\t\t|\t==========student==========\t|\n" << endl;
cout << "\t\t|\t 1. insert information \t|\n" << endl;
cout << "\t\t|\t 2. demonstrate information\t|\n" << endl;
cout << "\t\t|\t 3. delete information \t|\n" << endl;
cout << "\t\t|\t 4. search information \t|\n" << endl;
cout << "\t\t|\t 5. conserve information \t|\n" << endl;
cout << "\t\t|\t 0. exit \t|\n" << endl;
cout << "\t\t|\t===========================\t|\n\n" << endl;
cout << "\t\t select(0~5):";
cin >> choice;
switch (choice)
{
case 1: input(head); goto L;
case 2: show(head); goto L;
case 3:
deleting(head);
goto L;
case 4:search(head); goto L;
case 5:reserve(head); goto L;
case 0: cout << " \t退出程序的运行!\n" << endl; break;
default: cout << "\t输入错误,请重新输入!\n" << endl; goto L;
}
}