#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct teacher { char name[32]; int math; int english; int data; struct Node *next; }SLIST; int Creat_SList(); int SList_Print(); int SList_SelectPrint(SLIST *pHead); int Creat_SList(SLIST **handle) { char name[32] = {0}; int flag = 0; int math = 0; int english = 0; int ret = 0; SLIST *pHead = NULL, *pCur = NULL, *pM = NULL; //先分配内存 pHead = (SLIST *)malloc(sizeof(SLIST)); if (pHead == NULL) { ret = -1; printf("func Creat_SList err ret=%d", ret); return ret; } pHead->data = 0; pHead->next = NULL; //这里让当前节点等于头节点 pCur = pHead; while (flag != -1) { pM = (SLIST *)malloc(sizeof(SLIST)); if (pM == NULL) { //SList_Destory(pHead); ret = -2; printf("func Creat_SList() err:%d malloc err", ret); return ret; } //char name[32] = { 0 }; printf("请输入学生的姓名 "); scanf("%s",name); strcpy(pM->name, name); //pM->name[] = name; printf("请输入学生数学成绩"); scanf("%d", &math); pM->math = math; printf("请输入学生英语成绩"); scanf("%d", &english); pM->english = english; printf("假设完毕当前学生的编辑请输入 -1 若要继续输入学生信息 则输入 1 "); scanf("%d",&flag); pM->next = NULL; //让pM接在pCur的后面也就是说明pHead中是没有存储数据的而是一个 空的头结点 pCur->next = pM; pCur = pM; } *handle = pHead; return ret; //END: } int SList_Print(SLIST *pHead) { int ret = 0; SLIST *p = NULL; p = pHead->next;//bug if (pHead == NULL) { return -1; } printf(" Begin "); //p = p->next; while (p) { //printf("%d ", p->data); printf("学生的姓名 "); printf("%s ", p->name); printf("学生数学成绩"); printf("%d ", p->math); printf("学生英语成绩"); printf("%d ", p->english); p = p->next; system("pause"); } printf("End "); return ret; } //对查询进行输出 int SList_SelectPrint(SLIST *pHead) { int ret = 0; SLIST *p = NULL; p = pHead; if (pHead == NULL) { return -1; } printf(" Begin "); { printf("您要查找的同学信息例如以下"); printf("学生的姓名 "); printf("%s ", p->name); printf("学生数学成绩"); printf("%d ", p->math); printf("学生英语成绩"); printf("%d ", p->english); } printf("End "); return ret; } //对学生信息进行查找 须要打印查找结果 int SList_Select(SLIST *pHead,char *name) { int ret = 0; int flag = 0; char myname[32] = { 0 }; SLIST *pCur; strcpy(myname, name); if (pHead == NULL) { int ret = -1; printf("SList_Insert err"); return ret; } //pCur = pHead->next; pCur = pHead->next; while (pCur) { //name 进行比較 第一个人能够进行正常查询可是不能 正常输出 if (strcmp(pCur->name, myname) == 0) { //这里推断是正确的 // SList_Print(pCur); SList_SelectPrint(pCur); flag = 1; break; } pCur = pCur->next; } if (flag == 0) { printf("查无此人查无此人查无此人查无此人"); } return ret; } //在x的出现位置插入y int SList_NodeInsert(SLIST *pHead, int x, int y); int SList_NodeInsert(SLIST *pHead, int x, int y) { int ret = 0; SLIST *pPre, *pCur, *pM; if (pHead == NULL) { int ret = -1; printf("SList_Insert err"); return ret; } pPre = pHead; pCur = pHead->next; //不断的malloc新节点数据域赋值 pM = (SLIST *)malloc(sizeof(SLIST)); pM->data = y; pM->next = NULL; while (pCur) { if (pCur->data == x) { break; } pPre = pCur; pCur = pCur->next; } if (pM == NULL) { ret = -2; printf("SList_Insert err"); return ret; } pM->next = pCur;//pPre->next; pPre->next = pM;//pPre = pM; return ret; } //删除 找到y并将它删除 int SList_NodeDel(SLIST *pHead, int y) { int ret = 0; SLIST *pPre, *pCur; if (pHead == NULL) { int ret = -1; printf("SList_Insert err"); return ret; } pPre = pHead; pCur = pHead->next; while (pCur) { if (pCur->data == y) { break; } pPre = pCur; pCur = pCur->next; } if (pCur == NULL) { printf("没有找到节点 y:%d", y); return -2; } pPre->next = pCur->next;//pTemp = pPre->next; //pPre = pTemp; return ret; } void main() { SLIST * pHead = NULL; int flag = 0; printf("------------------------学生管理系统------------------------- "); for (;;) { printf("假设要输入学生信息请输入: 1 "); printf("假设要查询学生信息请输入: 2 "); printf("假设要查询全部学生请输入: 3 "); scanf("%d", &flag); if (flag == 1) { Creat_SList(&pHead); } if (flag == 2) { printf("请输入要查询的人的名字"); char name[32] = { 0 }; scanf("%s", name); printf("s%",name); //进行查询而且打印结果 SList_Select(pHead,name); } if (flag == 3) { SList_Print(pHead); } printf("----------------------------------------------------------"); system("pause"); } //printf(" "); //SList_Select(pHead, "han"); //system("pause"); }