【课外作业】二维双向链表练习代码

来源:互联网 发布:linux分页显示命令 编辑:程序博客网 时间:2024/06/05 17:21

经典的学生管理项目准备来临,为了能方便存储学生信息,利用课余时间尝试写出的一个二维链表,但由于实现函数功能的时候,并没有考虑到如何把链表存储的信息写到文件里,更别提把信息读回到链表中,而且查找功能也并不满意,很多都是想到的时候在改,本来打算改进一下函数的实现的,但是时间实在不够用,只能这样了。

(本来打算后来再补回注释的,但是实在懒,还是算了)


/*练习代码:建立一个二维双向链表,用来存储班级和学生的数据实现对班级和学生数据的增、删、改、查并打印出出班级和学生的信息*/#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct _student{struct _student *p_next;struct _student *p_before;struct _student *p_search;int age;char *name;}Student;typedef struct _class{Student *p_stu_next;struct _class *p_next;struct _class *p_before;struct _class *p_search;int size;char *name;}Class;typedef struct _stack{int size;Class *p_next;}Stack;//search the classClass* search_class(Stack *p_stack){char name[50];printf("input class's name :");scanf("%s", name);Class *p_destination = p_stack->p_next;p_destination->p_search = p_stack->p_next;while (NULL != p_destination->p_search){if (strcmp(name, p_destination->p_search->name) == 0){printf("search class success!\n");return p_destination->p_search;}p_destination->p_search = p_destination->p_search->p_next;}printf("search class failed.\n");return NULL;}//search the studentStudent* search_student(Stack *p_stack){Class *p_temp = search_class(p_stack);p_temp->p_search = p_temp;if (NULL == p_temp){return NULL;}printf("input student's name:");char name[50];scanf("%s", name);while (NULL != p_temp->p_stu_next){if (0 == strcmp(name, p_temp->p_search->p_stu_next->name)){printf("search student success!\n");return p_temp->p_stu_next;}p_temp->p_search->p_stu_next = p_temp->p_search->p_stu_next->p_next;}printf("search student failed!\n");return NULL;}//edit student infovoid edit_student(Stack *p_stack){Student *p_stu = search_student(p_stack);if (NULL == p_stu){printf("this student is not exist!\n");return;}printf("input new massage\nstudent name is:");char name[50];scanf("%s", name);strcpy(p_stu->name, name);printf("age is: ");scanf("%d", &(p_stu->age));printf("edit completed!\n");}//delete a studentvoid delete_student(Stack *p_stack){Class *p_temp = search_class(p_stack);p_temp->p_search = p_temp;if (NULL == p_temp){printf("delete failed,this class is not exist!\n");return;}printf("104:input student's name:");char name[50];scanf("%s", name);while (NULL != p_temp->p_search->p_stu_next){if (0 == strcmp(name, p_temp->p_search->p_stu_next->name)){printf("111:search success!\n");break;}p_temp->p_search->p_stu_next = p_temp->p_search->p_stu_next->p_next;}if (NULL == p_temp->p_search->p_stu_next){printf("delete failed, this student is not exist!\n");return;}Student *p_stu = p_temp->p_search->p_stu_next;if (p_stu->p_before != NULL){p_stu->p_before->p_next = p_stu->p_next;}if (p_stu->p_next != NULL){p_stu->p_next->p_before = p_stu->p_before;}p_temp->size--;free(p_stu->name);free(p_stu);printf("delete student success!\n");}//create a studentStudent* create_student(Stack *p_stack){Student *p_stu = malloc(sizeof(Student));p_stu->name = malloc(50);p_stu->p_next = NULL;p_stu->p_before = NULL;p_stu->p_search = NULL;printf("input student's name: ");char name[50];scanf("%s", name);strcpy(p_stu->name, name);printf("input student's age: ");scanf("%d", &(p_stu->age));return p_stu;}//add a studentvoid add_student(Stack *p_stack, Student *p_stu){Class *p_class = search_class(p_stack);while (NULL == p_class){printf("this class is not exist!\nAre your want to continue?(y/n)\n");char flag1;scanf("%c", &flag1);if (flag1 != 'y' || flag1 != 'Y'){free(p_stu->name);free(p_stu);p_stu = NULL;printf("add student failed!\n");return;}p_class = search_class(p_stack);}(p_class->size)++;if (NULL != p_class->p_stu_next){p_class->p_stu_next->p_before = p_stu;}p_stu->p_next = p_class->p_stu_next;p_class->p_stu_next = p_stu;printf("end\n");printf("add student success!\n");}//create a classClass* create_class(){printf("input class's name: ");Class *p_class = malloc(sizeof(Class));if (NULL == p_class){perror("create class failed!\n");return NULL;}p_class->p_stu_next = NULL;p_class->p_before = NULL;p_class->p_next = NULL;p_class->p_search = NULL;p_class->size = 0;p_class->name = malloc(50);if (p_class->name == NULL){perror("create class failed!\n");free(p_class);return NULL;}char name[50];scanf("%s", name);strcpy(p_class->name, name);return p_class;}//add a classvoid add_class(Stack *p_stack, Class *p_class){if (NULL != p_stack->p_next){p_stack->p_next->p_before = p_class;}p_class->p_next = p_stack->p_next;p_stack->p_next = p_class;}//delete a classvoid delete_class(Stack *p_stack){Class *p_class = search_class(p_stack);if (NULL == p_class){printf("this class is not exist!\n");return;}if (NULL != p_class->p_before){p_class->p_before->p_next = p_class->p_next;}if (NULL != p_class->p_next){p_class->p_next->p_before = p_class->p_before;}free(p_class->name);free(p_class);p_stack->size--;p_class = NULL;printf("delete class success!\n");}//edit a class's infovoid edit_class(Stack *p_stack){Class *p_class = search_class(p_stack);printf("input the new info:\nnew name is:");char name[50];scanf("%s",  name);strcpy(p_class->name, name);}//create a stackvoid create_stack(Stack **pp_stack){*pp_stack = malloc(sizeof(Stack));(*pp_stack)->size = 0;(*pp_stack)->p_next = NULL;}//print student infovoid print_student(Student *p_stu, int show){if (NULL == p_stu){return;}if (1 == show){print_student(p_stu->p_next, 1);}printf("student's info\nname: %s\nage: %d\n", p_stu->name, p_stu->age);}//print class infovoid print_class(Class *p_class, int show){if (NULL == p_class){return;}printf("Class's name is: %s\nsize is %d\n", p_class->name, p_class->size);if (p_class->size > 0 && 1 == show){Student *p_stu = p_class->p_stu_next;print_student(p_stu, show);}}//print all class infovoid print_all_class(Class *p_class, int show){if (NULL == p_class){return;}print_class(p_class, show);print_all_class(p_class->p_next, show);}//print stack infovoid print_stack(Stack *p_stack){print_all_class(p_stack->p_next, 1);}




0 0
原创粉丝点击