|

楼主 |
发表于 2019-4-6 14:00:11
|
显示全部楼层
本帖最后由 cookies945 于 2019-4-6 14:06 编辑
自己偶然发现了解决的方法,欢迎大佬们也来发表一下自己的看法呀,但是还是有很多问题,比如删除链表最后一个学生,链表就没有了,选择删除操作的时候,如果输入了一个比链表长度还大的数,也会发生错误
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct students{
- char name[10];
- int num;
- float score;
- struct students *next;
- };
- int count=0;
- int *p = &count;
- void getInfo(struct students *);
- void addStu(struct students **);
- int delStu(struct students **,int );
- int searchStu(struct students *,int );
- void showList(struct students *);
- int main(){
- struct students *S = NULL;//生成一个头节点
- int op,i;
- do{
- printf("welcome to students' information manage system!\n");
- printf("please chose your operation:\n");
- printf("1 add a student to specified location\n");
- printf("2 delete a student from the specified location\n");
- printf("3 find a student through his number\n");
- printf("4 show the list:\n");
- printf("0 exit\n");
- scanf("%d",&op);
- switch(op){
- case 1:printf("input the student information:");addStu(&S);break;
- case 2:printf("input the num you want to delete:");scanf("%d",&i);delStu(&S,i);break;
- case 3:printf("input his num:");scanf("%d",&i);searchStu(S,i);break;
- case 4:showList(S);break;
- }
- }while(op != 0);
-
- return 0;
- }
- void getInfo(struct students *s){
- printf("input students's name :\n");
- scanf("%s",s->name);
- printf("input students's num and score:\n");
- scanf("%d%f",&s->num,&s->score);
- }
- void addStu(struct students **S1){
- struct students *ns,*temp,*move = *S1;
- int flag = 0;
-
- ns = (struct students *)malloc(sizeof(struct students));
- if(ns == NULL){
- printf("menory allocation error!");
- exit(1);
- }
- getInfo(ns);
- if(move == NULL){ //空链表的操作;
- move=ns;
- ns->next=NULL;
- *S1 = move;
- }
- else{
- while(move->num<ns->num && move->next != NULL){//通过新学生的学号来移动move与temp指针,
- flag = 1;
- temp = move;
- move = move->next;
- }
- if(move->next == NULL && move->num<=ns->num){ //新学生的学号是最大的
- move->next = ns;
- ns->next = NULL;
- }
- else if(!flag){ //目前没有发生问题
- temp = *S1;
- move = ns;
- ns->next = temp;
- *S1 = move;
- }
- else{ //学生学号刚好在中间
- temp->next = ns;
- ns->next = move;
- }
- }
- }
- int delStu(struct students **S1,int n){
- struct students *temp;
- struct students **ptr = S1; //用来接收旧的结构体节点,并准备释放内存
- if((*ptr)->next == NULL){
- printf("can't delete student from an empty list\n");
- }
- while((*ptr)->num < n){
- (*ptr)=(*ptr)->next;
- }
- if((*ptr) == NULL){
- printf("can't find this student!\n");
- }
- if((*ptr)->num == n){
- temp = (*ptr);
- (*ptr) = temp->next;
- // free(temp);
- printf("delete complete\n");
- }
- return 0;
- }
- int searchStu(struct students *S1,int n){
- int i = 0;
- while(S1 != NULL){
- if(S1->num == n){
- printf("he/she is the %d student\n",i+1);
- return i+1;
- }
- S1 = S1->next;
- i++;
- }
- if(S1->next == NULL && S1->num != n){
- printf("can't find this student");
- return 0;
- }
- return 0;
- }
- void showList(struct students *S1){
- struct students *s;
- s = S1;
- if(S1 == NULL){
- printf("no student in this list!\n");
- }
- else{
- printf("name\tnum\tscore\t\n");
- }
- while(s != NULL){
- printf("%s\t%d\t%f\t\n",s->name,s->num,s->score);
- s = s->next;
- }
-
- }
复制代码 |
|