1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| #include #include #include typedef struct stu { int num; char name[21]; struct stu *next; }stu;
stu* creat(int id,char str[]); void add(stu *p,int id,char str[]); int query_value(stu *p,char name[]); stu* query_id(stu *p,int id); int del_list(stu *p,int id); void menu(stu *p); void menu_add(stu *p); void menu_edit(stu *p);
int main(){ stu *p=NULL; p=creat(0,"NULL"); menu(p); while(p!=NULL){ printf("name=%s,id=%d\n",p->name,p->num); p=p->next; } }
stu* creat(int id,char str[]){ stu *new; if((new=(stu *)malloc(sizeof(stu)))==NULL){ printf("不能分配内存空间!"); exit(0); } new->num=id; strcpy(new->name,str); new->next=NULL; return new; }
void add(stu *p,int id,char str[]){ stu *new=NULL; new=creat(id,str); while(p->next!=NULL){ p=p->next; } p->next=new; } int query_value(stu *p,char name[]){ int i=0; while((p!=NULL)){ if (strcmp(p->name,name)==0){ return i; } p=p->next; i=i+1; } return 0; }
stu* query_id(stu *p,int id){ for (int i = 0; i < id; ++i){ p=p->next; } return p; }
int del_list(stu *p,int id){ stu *p1,*s; p1=query_id(p,id-1); if (p1==NULL){ return -1; } else{ if (p->next==NULL){ return 0; } else{ s=p->next; p->next=s->next; free(s); return 1; } }
} void menu(stu *p){ int input; printf("欢迎使用学生学籍管理系统\n"); printf("1.插入学生\n"); printf("2.修改学生\n"); printf("3.输出列表\n"); printf("请输入选项:"); scanf("%d",&input); switch(input){ case 1:{ menu_add(p); menu(p); break; } case 2:{ menu_edit(p); menu(p); break; } case 3:break; } } void menu_add(stu *p){ int id; char name[21]; printf("请输入要添加的姓名:\n"); scanf("%s",name); printf("请输入学号:\n"); scanf("%d",&id); add(p,id,name); }
void menu_edit(stu *p){ int i; stu *p1; char name[21]; printf("请输入要修改的姓名:\n"); scanf("%s",name); i=query_value(p,name); p1=query_id(p,i); printf("请输入新的姓名:\n"); scanf("%s",p1->name);
}
|