鱼C论坛

 找回密码
 立即注册
查看: 1218|回复: 6

[已解决]求大佬看看这个程序的哪里想法有问题吧

[复制链接]
发表于 2019-3-24 16:18:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#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 *);
int addStu(struct students **,int );
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 num you want to insert:");scanf("%d",&i);addStu(&S,i);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);
}
int addStu(struct students **S1,int n){
        struct students *ns,*temp;
        int i;
        if(n < 0 || n > count+1){
                printf("error:invalid num");
                return 1;
        }
        ns = (struct students *)malloc(sizeof(struct students));
        if(ns == NULL){
                printf("menory allocation error!");
        }
        getInfo(ns);
        for(i = 0;i < n-1;i++){
                *S1 = (*S1)->next;
        }
        if(*S1 !=  NULL){
                temp = *S1;
                *S1 = ns;
                ns->next = temp;
        }
        else{
                *S1 = ns;
                ns->next = NULL;
        }
        (*p)++;
        return 0;
}
int delStu(struct students **S1,int n){
        struct students *temp; //用来接收旧的结构体节点,并准备释放内存 
        int i;
        if(n > count || n < 0){
                printf("error!");
                return 1;
        }
        for(i = 0;i < n-1;i++){
                *S1 = (*S1)->next;
        }
        temp = *S1;
        *S1 = temp->next;
        free(temp);
        printf("delete complete");
        (*p)--;
        return 0;
}
int searchStu(struct students *S1,int n){
        int i;
        for(i = 0;i < count;i++){
                if(S1->num == n){
                        printf("he/she is the %d student");
                        return i+1;
                }
                S1 = S1->next;
        }
        if(i > count){
                        printf("can't find this student");
                        return 0;
                }
}
void showList(struct students *S1){
        int i;
        struct students *s;
        s = S1;
        if((*p) == 0){
                printf("no student in LinkList");
        }
        while(s != NULL){
        printf("name\tnum\tscore\n");
        printf("%s\t%d\t%f\n",s->name,s->num,s->score);
        s = s->next;
        }
        
}
插到第几个,第几个前面的就没有了,插到最后一个整个链表都没了
救救孩子吧
最佳答案
2019-3-25 17:56:18
本帖最后由 jackz007 于 2019-3-25 18:09 编辑

        addStu() 的最大问题是 S1 作为指向链表首节点的指针,其重要性不言而喻,可是在函数中却被当做普通变量变来变去,而这个变量 S1 又是通过地址传入,在函数中所有的改变都会映射到主函数中去,而在主函数中,却一直认为这个 S1 是链表首节点指针,从而造成严重混乱。
        我贴出来的代码是这次才精心编写的,没有照抄任何人的东西,你前面看见相似的代码估计也是我给别人的回帖。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-3-24 18:54:31 | 显示全部楼层
想法很好
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-24 20:17:07 | 显示全部楼层
      找问题实在是苦差事,我按楼主的程序框架重写了全部代码,楼主可以自己比对差别。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct students {
        char name[10]          ;
        int num                ;
        float score            ;
        struct students * next ;        
}                              ;

int count = 0                  ;

struct students * findStu(struct students * hd , const int num)
{
        struct students * p1 , * p2 , * ret     ; 
        bool f                                  ;
        ret = NULL                              ;
        if(count > 0 && hd != NULL) {
                for(f = false , p1 = p2 = hd ; p2 != NULL && ! f ;) {
                        if(p2 -> num == num) {
                                f = true        ;
                        } else {
                                p1 = p2         ;
                                p2 = p1 -> next ;
                        }
                }
                if(f) ret = p1                  ;
        } 
        return ret                              ;
}

struct students * addStu(struct students * hd)
{
        struct students * p1 , * p2 , * head                                                  ;
        int num                                                                               ;

        head = hd                                                                             ;
        p1 = hd                                                                               ;
        if(p1 != NULL) while(p1 -> next != NULL) p1 = p1 -> next                              ;
        for(;;) {
                printf("\n")                                                                  ;
                printf("input student\'s num to add : ")                                      ;
                scanf("%d" , & num)                                                           ;
                if(! num) break                                                               ;
                if(findStu(head , num) == NULL) {
                        if((p2 = (struct students *) malloc(sizeof(struct students))) != NULL) {
                                printf("input student\'s name : ")                            ;
                                scanf("%s" , p2 -> name)                                      ;
                                printf("input student\'s score: ")                            ;
                                scanf("%f" , & p2 -> score)                                   ;
                                p2 -> num = num                                               ;
                                p2 -> next = NULL                                             ;
                                if(! count) head = p2                                         ;
                                else p1 -> next = p2                                          ;
                                p1 = p2                                                       ;
                                count ++                                                      ;
                        } else {
                                fprintf(stderr , "malloc() cann\'t  to alocate the memory !") ;
                                break                                                         ;
                        }
                } else {
                        printf("\n")                                                          ;
                        printf("the student with num = %d already exists !\n" , num)          ;
                }
        }
        return head                                                                           ;
}

struct students * delStu(struct students * hd)
{
        struct students * p1 , * p2 , * head                                                                ;
        int num                                                                                             ;
        head = hd                                                                                           ;
        if(count > 0 && hd != NULL) {
                while(count > 0) {
                        printf("\n")                                                                        ;
                        printf("input student\'s num to delete : ")                                         ;
                        scanf("%d" , & num)                                                                 ;
                        if(! num) break                                                                     ;
                        if((p1 = findStu(head , num)) != NULL) {
                                if(p1 == head) {
                                       head = head -> next                                                  ;
                                       p2 = p1                                                              ;
                                } else {
                                       p2 = p1 -> next                                                      ;
                                       p1 -> next = p2 -> next                                              ;
                                }
                                free(p2)                                                                    ;
                                count --                                                                    ;
                                printf("the student with num = %d has been successfully deteted !\n" , num) ;
                        } else {
                                printf("\n")                                                                ;
                                printf("the student with num = %d doesn\'t exists !\n" , num)               ;
                        }
                }
        } else {
                printf("\n")                                                                                ;
                printf("empty list !\n")                                                                    ;
        }
        return head                                                                                         ;
}

void searchStu(struct students * hd)
{
        struct students * p                                                                   ;
        int num                                                                               ;

        if(count > 0 && hd != NULL) {
                for(;;) {
                        printf("\n")                                                          ;
                        printf("input student\'s num to search : ")                           ;
                        scanf("%d" , & num)                                                   ;
                        if(! num) break                                                       ;
                        if((p = findStu(hd , num)) != NULL) {
                                printf("p -> num = %d\n" , p -> num)                          ;
                                if(p != hd) p = p -> next                                     ;
                                printf("student's name  : %s\n"    , p -> name)               ;
                                printf("student's num   : %d\n"    , p -> num)                ;
                                printf("student's score : %5.2f\n" , p -> score)              ;
                        } else {
                                printf("\n")                                                  ;
                                printf("the student with num = %d doesn\'t exists !\n" , num) ;
                        }
                }
        } else {
                printf("\n")                                                                  ;
                printf("empty list !\n")                                                      ;
        }
}

void showList(struct students * hd)
{
        struct students * p                                               ;
        if(count > 0 && hd != NULL) {
                p = hd                                                    ;
                while(p != NULL) {
                        printf("\n")                                      ;
                        printf("student\'s name  : %s\n"    , p -> name)  ;
                        printf("student\'s num   : %d\n"    , p -> num)   ;
                        printf("student\'s score : %5.2f\n" , p -> score) ;
                        p = p -> next                                     ;
                }
        } else {
                printf("\n")                                              ;
                printf("empty list !\n")                                  ;
        }
}

int main()
{
        struct students * head                                               ;
        int op                                                               ;
        head = NULL                                                          ;
        count = 0                                                            ;
        do {
                printf("\n")                                                 ;
                printf("welcome to students\' information manage system!\n") ;
                printf("    1 add a student.\n")                             ;
                printf("    2 delete a student.\n")                          ;
                printf("    3 find a student.\n")                            ;
                printf("    4 show the list:\n")                             ;
                printf("    0 exit\n")                                       ;
                printf("\n")                                                 ;
                printf("       your choice : ")                              ;
                scanf("%d" , & op)                                           ;
                switch(op) {
                        case 1 :
                                head = addStu(head)                          ;
                                break                                        ;
                        case 2:
                                head = delStu(head)                          ;
                                break                                        ;
                        case 3:
                                searchStu(head)                              ;
                                break                                        ;
                        case 4:
                                showList(head)                               ;
                                break                                        ;
                }
        } while(op != 0)                                                     ;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-25 17:24:34 | 显示全部楼层
jackz007 发表于 2019-3-24 20:17
找问题实在是苦差事,我按楼主的程序框架重写了全部代码,楼主可以自己比对差别。

我在网上又看到和你这个差不多的,就是看不懂才来论坛问的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-25 17:56:18 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2019-3-25 18:09 编辑

        addStu() 的最大问题是 S1 作为指向链表首节点的指针,其重要性不言而喻,可是在函数中却被当做普通变量变来变去,而这个变量 S1 又是通过地址传入,在函数中所有的改变都会映射到主函数中去,而在主函数中,却一直认为这个 S1 是链表首节点指针,从而造成严重混乱。
        我贴出来的代码是这次才精心编写的,没有照抄任何人的东西,你前面看见相似的代码估计也是我给别人的回帖。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-26 09:51:54 | 显示全部楼层
jackz007 发表于 2019-3-25 17:56
addStu() 的最大问题是 S1 作为指向链表首节点的指针,其重要性不言而喻,可是在函数中却被当做普 ...

哦 原来如此,难怪我看很多人写的都有另外的指针来替代头节点
谢谢大佬,受教了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-28 15:31:52 | 显示全部楼层
jackz007 发表于 2019-3-25 17:56
addStu() 的最大问题是 S1 作为指向链表首节点的指针,其重要性不言而喻,可是在函数中却被当做普 ...

大佬,这个程序还是不行啊,就算改用其他指针指向头节点,也不能写道这个头节点里啊
#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 *);
int 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 num you want to insert:");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);
}
int addStu(struct students **S1){
        struct students *ns,*temp,*ptr = *S1;
        ns = (struct students *)malloc(sizeof(struct students));
        if(ns == NULL){
                printf("menory allocation error!");
        }
        getInfo(ns);
        if(ptr == NULL){
                ptr = ns;
                ns->next = NULL;
                (*p)++;
        }
        while(ptr->next != NULL && ptr->num < ns->num){
                temp = ptr;
                ptr = ptr->next;
        }
        if(ptr == *S1){
                temp = *S1;
                *S1 = ns;
                ns->next = temp;
        }
        else if(ptr->next == NULL){
                ptr->next = ns;
                ns->next = NULL;
                
        }
        else{
                temp->next = ns;
                ns->next = ptr;
        }
        printf("name\tnum\tscore\t\n",(*S1)->name,(*S1)->num,(*S1)->score);
        return 0;
}
int delStu(struct students **S1,int n){
        struct students *temp,*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;
        while(S1 != NULL){
                if(S1->num == n){
                        printf("he/she is the %d student");
                        return i+1;
                }
                S1 = S1->next;
        }
        if(S1->next == NULL && S1->num != n){
                        printf("can't find this student");
                        return 0;
                }
}
void showList(struct students **S1){
        if(*S1 == NULL){
                printf("no student in this list!\n");
        }
        else{
                printf("name\tnum\tscore\t\n");
        }
        while(*S1 != NULL){
                printf("%s\t%d\t%d\t\n",(*S1)->name,(*S1)->num,(*S1)->score);
                *S1 = (*S1)->next;
        }
        
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 12:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表