求大神帮忙,怎么用c语言顺序表做一个健康表
健康表可以插入、删除姓名学号身体状况
程序代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define NAMElEN 20 #define STATUELEN 10 typedef struct tag_student { char name[NAMElEN]; int num; char status[STATUELEN]; struct tag_student *next; } STUDENT, *student; student head = NULL; void Build()//建立一个带头结点的单链表 { head = (student)malloc(sizeof(STUDENT)); memset(head, 0, sizeof(STUDENT)); } void Print(student L)// { printf("学生姓名:%20s 学号%4d, 健康状况:%10s\n", L->name, L->num, L->status); } void FindWithName(char *name)// { student p; if (head == NULL) { Build(); printf("没找到!!\n\n"); return; } p = head; while (p && strcmp(p->name, name)) p = p->next; if (p) Print(p); else printf("没找到!!\n\n"); } void FindWithNum(int num)// { student p; if (head == NULL) { Build(); printf("没找到!!\n\n"); return; } p = head; while (p && p->num != num) p = p->next; if (p) Print(p); else printf("没找到!!\n\n"); } void ShowAll()// { student p; if (head == NULL) { Build(); printf("没找到!!\n"); return; } p = head; while (p) { Print(p); p = p->next; } } void DeleteWithName(char *name)// { student p, q; if (head == NULL) { Build(); printf("没有找到该生%s信息\n", name); return; } p = head; if (0 == strcmp(p->name, name)) { head = head->next; free(p); printf("删除成功!!\n\n"); return; } while (p->next &&strcmp(p->next->name, name)) p = p->next; if (p->next) { q = p->next; p->next = q->next; free(q); printf("删除成功!!\n\n"); } else printf("没有找到该生%s信息\n", name); } void DeleteWithNum(int num)// { student p, q; if (head == NULL) { Build(); printf("没有找到该生%d信息\n", num); return; } p = head; if (p->num == num) { head = head->next; free(p); printf("删除成功!!\n\n"); return; } while (p->next &&p->next->num != num) p = p->next; if (p->next) { q = p->next; p->next = q->next; free(q); printf("删除成功!!\n"); } else printf("没有找到该学号%d对应信息\n", num); } void Insert(student p)// { student s,q; if (head == NULL) { Build(); } s = head; if (head->name[0] == 0) { strcpy(head->name, p->name); head->num = p->num; strcpy(head->status, p->status); return; } while (s->next) s = s->next; q = (student)malloc(sizeof(STUDENT)); memset(q, 0, sizeof(STUDENT)); strcpy(q->name, p->name); q->num = p->num; strcpy(q->status, p->status); s->next = q; } int main() { int op, x; STUDENT st; Build(); while (1) { printf("请选择你要做的操作\n"); printf(" 1.插入一个学生健康表\n"); printf(" 2.删除一个学生健康表\n"); printf(" 3.查找一个学生健康表\n"); printf(" 4.退出系统\n"); scanf("%d", &op); switch (op) { case 1: printf("请输入学生信息,用空格隔开:姓名 学号 健康状况\n"); scanf("%s %d %s", st.name, &st.num, st.status); Insert(&st); printf("操作完成!\n\n"); break; case 2: printf("请输入删除方式\n"); printf(" 1.按姓名删除\n"); printf(" 2.按学号删除\n"); scanf("%d", &x); switch (x) { case 1: printf("请输入学生姓名\n"); scanf("%s", st.name); DeleteWithName(st.name); break; case 2: printf("请输入学生学号\n"); scanf("%d", &st.num); DeleteWithNum(st.num); break; default: printf("输入错误!\n\n"); break; } break; case 3: printf("请输入查找方式\n"); printf(" 1.按姓名查找\n"); printf(" 2.按学号查找\n"); printf(" 3.显示所有学生信息\n"); scanf("%d", &x); switch (x) { case 1: printf("请输入学生姓名\n"); scanf("%s", st.name); FindWithName(st.name); break; case 2: printf("请输入学生学号\n"); scanf("%d", &st.num); FindWithNum(st.num); break; case 3: ShowAll(); break; default: printf("输入错误!\n\n"); break; } break; case 4: return; default: printf("输入错误!\n\n"); break; } } return 0; }