#include
#include
#include
using namespace std;
#define TRUE 1
#define FALSE 0
typedef struct Student
{
char num[10];
char name[10];
char grade[10];
}Student;
typedef struct StuList
{
Student data;
StuList *next;
}StuList, *lpStuList;
lpStuList stuList = NULL;//全局的学生信息链表
lpStuList head = NULL; //全局的链表头节点
void menu()
{
cout << "选择(1:输入数据 2:输出数据 3:按姓名查找数据 其他:退出):" << endl;
}
int addStu()
{
int num;
cout << "输入数据" << endl << "学生人数:";
cin >> num;
if (stuList == NULL)
{
stuList = (lpStuList)malloc(sizeof(Student));
if (stuList != NULL)
{
stuList->next = NULL;
}
}
lpStuList cur = NULL;
for (int i = 1; i <= num; ++i)
{
Student student;
cur = NULL;
cur = (lpStuList)malloc(sizeof(Student));
cout << "第" << i << "个学生(学号 姓名 成绩):";
cin >> student.num >> student.name >> student.grade;
cur->data = student;
cur->next = NULL;
if (head == NULL)
{
head = cur;
}
else stuList->next = cur;
stuList = cur;
}
return TRUE;
}
int display()
{
cout << "输出数据" << endl;
lpStuList cur = NULL;
cur = head;
if (cur == NULL)
{
cout << "没有任何学生信息哦" << endl;
return FALSE;
}
else
{
cout << "学号\t\t姓名\t\t成绩" << endl;
while (cur != NULL)
{
cout << cur->data.num << "\t\t" << cur->data.name << "\t\t" << cur->data.grade << endl;
cur = cur->next;
}
return TRUE;
}
}
void search()
{
char name[10];
cout << "按姓名查找数据" << endl << "请输入姓名:";
cin >> name;
lpStuList cur = NULL;
cur = head;
int ok = 0;
while (cur != NULL)
{
if (strstr(cur->data.name, name) != NULL)
{
if (ok == 0)
{
cout << "学号\t\t姓名\t\t成绩" << endl;
ok = 1;
}
cout << cur->data.num << "\t\t" << cur->data.name << "\t\t" << cur->data.grade << endl;
}
cur = cur->next;
}
if (ok == 0)
{
cout << "未找到符合的信息哦" << endl;
}
}
void exitApp()
{
if (head != NULL)
{
lpStuList cur = NULL;
while (head != NULL)
{
cur = head;
head = head->next;
free(cur); // 运行到这里总会崩溃
cur = NULL;
}
}
}
int main()
{
int start = 1;
while (start)
{
menu();
int num;
cin >> num;
switch (num)
{
case 1: {addStu(); break; }
case 2: {display(); break; }
case 3: {search(); break; }
default: {exitApp(); start = 0; }
}
}
system("pause");
}