标签:include
#include<stdio.h> #include<stdlib.h> #include<string.h> struct stu_node{ char num[15]; int score; struct stu_node *next; }; struct stu_node *stu_create() { struct stu_node *head, *newN, *tail; char num[15]; int score; printf("请输入学生的学号和分数(学号为0表示结束):\n"); scanf("%s%d",num,&score); newN = (struct stu_node *)malloc(sizeof(struct stu_node)); strcpy(newN->num,num); newN->score = score; newN->next = NULL; head =newN; tail = newN; while(1) { printf("请输入学生的学号和分数(学号为0表示结束):\n"); scanf("%s%d",num,&score); if(strcmp(num,"0")==0) break; else { newN = (struct stu_node *)malloc(sizeof(struct stu_node)); strcpy(newN->num,num); newN->next = NULL; tail->next = newN; tail = newN; } } return head; } void stu_print(struct stu_node *head) { struct stu_node *p = head; if(p==NULL) { printf("学生信息为空:"); return; } printf("学号\t分数\t\n"); while(p!=NULL) { printf("%s\t%d\n",p->num,p->score); p=p->next; } } void stu_modify(struct stu_node *head) { char num[15]; struct stu_node *p=head; if(head==NULL) { printf("学生信息为空:"); return; } printf("请输入要修改的学生的学号:"); scanf("%s",num); while(p!=NULL && strcmp(p->num, num)!=0) p=p->next; if(p!=NULL) { printf("请输入要修改的学生的分数"); scanf("%d",&p->score) ; } else printf("未找到该学号的学生!"); } struct stu_node *stu_delete (struct stu_node *head) { char num[15]; struct stu_node *p=head, *p1; if(p==NULL) { printf("学生信息为空:"); return head; } printf("请输入要删除的学生的学号:"); scanf("%s",num); while(p!=NULL && strcmp(p->num,num)!=0) { p1=p; p=p->next; } if(p!=NULL) { if(p=head) head=p->next; else p1->next = p->next; free(p) ; } else printf("未找到该学号的学生!\n"); return head; } void main() { int choice; struct stu_node *head =NULL; printf("欢迎使用学生信息管理系统\n"); printf("\t-------------------------------------------\n"); printf("\t1.创建学生信息链表 2.显示学生信息\n"); printf("\t3.修改学生信息 4.删除学生信息\n"); printf("\t5.退出学生信息\n") ; printf("\t-------------------------------------------\n"); while(1) { printf("\t 请选择功能模块,输入数字1-5:"); scanf("%d",&choice) ; switch(choice) { case 1: head = stu_create();break; case 2: stu_print(head);break; case 3: stu_modify(head);break; case 4: head = stu_delete(head);break; case 5: exit(0); default: printf("输入错误!\n") ; } } }
标签:include
原文地址:http://9815936.blog.51cto.com/9805936/1639282