今儿,有同僚讨论一个源程序,是关于“学生分数的录入的”。
其中用到了链表,涉及到的就是链表的增删改查功能。
我在原来的基础上修改了一些,基本功能已经调试通过。
功能如下:
1、输入type类型
2、type为1,是增加一个学生
为2,删除输入学号的学生
为3,删除输入成绩的学生(暂时为实现)
为4,修改输入的学号的学生分数
为5,查找目标学号的学生成绩
为0, 直接打印所有学生的学号和成绩
源代码如下:
#include #include #include #define FLUSH \ do{\ int c;\ while( (c = getchar()) != '\n' && c != EOF)\ ;\ }while(0) typedef struct _student{ int num; float score; struct _student *next; }*studentList, studentNode; studentList createListNode() { studentList p = NULL; p = (studentList)malloc(sizeof(studentNode)); p->num = 0; p->score = 0; p->next = NULL; return p; } int listLenGet(studentList list) { int len = 0; studentList p = list; while (p->next) len++; return len; } int insertNode(studentList list, studentNode* student) { studentList p = list; while (p->next) p = p->next; p->next = student; return 0; } int delNumNode(studentList list, int num) { studentList p = list; studentList q = NULL; int deleteCount = 0; while (p->next){ if (p->next->num != num) p = p->next; else { q = p->next; p->next = q->next; free(q); deleteCount++; } } return deleteCount; } float delScoreNode(studentList list, float score) { studentList p = list; studentList q = p->next; while (p->next){ if (q->score== score){ p->next = q->next; free(q); q = p->next; continue; } else p = p->next; } return 0; } int changeNumNodeScore(studentList list, int num, float score) { studentList p = list; studentList q = p->next; while (p->next){ if (q->num == num){ q->score = score; return 0; } p = p->next; q = p; } return 0; } int checkStudentNum(studentList list, int num) { studentList p = list; studentList q = p->next; while (p->next){ if (q->num == num) return -1; p = p->next; q = p->next; } return 0; } float numNodeScoreGet(studentList list, int num) { studentList p = list; studentList q = p->next; while (p->next){ if (q->num == num) return q->score; } return -1; } void printList(studentList list) { studentList p = list; studentList q = p->next; //system("clear"); printf("****************************************\n"); while (p->next){ printf("student num = %d, score = %f\n", q->num, q->score); p = p->next; q = p->next; } printf("****************************************\n"); } void printfInfo() { //printf("") } void doInsertStudent(studentList list) { int num = -1; float score = 0; studentNode* student = createListNode(); printf("student num = "); scanf("%d", &num); printf("score = "); scanf("%f", &score); if (checkStudentNum(list, num)){ printf("We have the same num\n"); return ; } student->num = num; student->score = score; insertNode(list, student); } void doDelStudentByNum(studentList list) { int num = -1; int ret = -1; printf("num = "); scanf("%d", &num); ret = delNumNode(list, num); if (ret <= 0) printf("Not found the num\n"); } void doDelStudentByScore(studentList list) { } void doChangeStudentScore(studentList list) { int num = -1; float score = 0; printf("change score\n"); printf("num = "); scanf("%d", &num); printf("score = "); scanf("%f", &score); if (checkStudentNum(list, num)) changeNumNodeScore(list, num, score); else printf("student dose not exist\n"); } void doFindStudentScore(studentList list) { int num = -1; float score = 0; printf("change score\n"); printf("num = "); scanf("%d", &num); if (checkStudentNum(list , num)) score = numNodeScoreGet(list, num); else printf("student dose not exist\n"); printf("student[%d]'s score is %f\n", num, score); } int main() { int type = -1; studentList list = createListNode(); while (1){ printf("Please input type \n"); scanf("%d", &type); FLUSH; //system("clear"); if (type < 0 || type > 5){ printf("Please in put the right type\n"); //system("clear"); continue; } switch (type){ //system("clear"); case 1://insert doInsertStudent(list); break; case 2://del num doDelStudentByNum(list); break; case 3://del score doDelStudentByScore(list); break; case 4://change doChangeStudentScore(list); break; case 5://look up doFindStudentScore(list); break; case 0: default: printList(list); break; } } return 0; }