主要是为了熟悉链表的使用,写出来的。
废话不多说,直接上源码:
#include#include struct student { int id; char name; char sex; char age; struct student *next; }; typedef struct student st; void show(); int get_choose(); void show_all_students(st *head); int sum_student(st *head); void insert_student(st *head); void del_student(st *head); void select_student(st *head); st *p_head; int main() { int choose; p_head = (st *) malloc(sizeof(st)); p_head -> next = NULL; while(1) { show(); choose = get_choose(); switch (choose) { case 1:show_all_students(p_head);break; case 2:insert_student(p_head);break; case 3:del_student(p_head);break; case 4:select_student(p_head);break; case 5:break; default:break; } if(choose == 5) { break; } } return 0; } void show() { puts("请选择你的操作:n1.显示是所有学生信息n2.插入学生信息n3.删除学习信息n4.搜索学生信息n5.结束系统n"); } int get_choose() { int choose_item = 0; printf("----------------------你选择的操作是:----------------------n"); scanf("%d",&choose_item); if(choose_item>=1 && choose_item<=5) { return choose_item; } else { puts("----------------------输入有误,退出程序----------------------n"); return (7); } } void show_all_students(st *head) { st *p; p = head -> next; int count = 0; while(p) { if(count == 0) { puts("账号t姓名t性别t年龄n"); } printf("id:%dtname:%ctsex:%ctage:%dn", p->id, p->name, p->sex,p ->age); count ++; p = p -> next; } if(count == 0) { puts("----------------------无学生信息----------------------n"); return; } puts("----------------------以上是所有学生信息----------------------"); } int sum_student(st *head) { int count; st *p; p = head -> next; count = 0; while(p) { count++; p = p->next; } return count; } void insert_student(st *head) { int pos,count; count = sum_student(head)+1; puts("----------------------输入你想要插入的位置----------------------n"); printf("----------------------范围在1~%d----------------------n",count); scanf("%d",&pos); if(pos< 1 || pos>count) { puts("----------------------输入错误,请重新输入!----------------------n"); } else { st *new; new = (st *) malloc(sizeof(st)); if (new != NULL) { puts("----------------------请输入学生信息----------------------"); scanf("%d %c %c %s", &new->id, &new->name, &new->sex, &new->age); } st *p_front; p_front = head; for (int i = 1; i < pos; ++i) { p_front = p_front->next; } st *temp; temp = p_front->next; p_front->next = new; new->next = temp; } } void del_student(st *head) { int count,pos; count = sum_student(head); if(count == 0) { puts("----------------------无学生信息,操作无效----------------------n"); } else { printf("目前数据库内存有的学生资料一共有:%dn",count); puts("请输入你要删除的学生资料:"); scanf("%d",&pos); st *p_front; p_front = head; for (int i = 1; i < pos; ++i) { p_front = p_front -> next; } st *temp; temp = p_front -> next; p_front -> next = temp -> next; free(temp); puts("----------------------删除成功----------------------"); } } void select_student(st *head) { int pos,id,a = 0; pos = sum_student(head); if(pos == 0) { puts("----------------------无学生信息,操作无效----------------------n"); } else { printf("目前数据库一共有:%d样本。n请输入你要搜索的学生id:", pos); scanf("%d", &id); st *p; p = head->next; while (p) { if (id == p->id) { printf("id:%dtname:%ctsex:%ctage:%dn", p->id, p->name, p->sex, p->age); a = 1; puts("----------------------以上是所有学生信息----------------------n"); break; } p = p -> next; } if(a == 0) { puts("----------------------数据库内查无此人。----------------------n"); } } }



