免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 1569 | 回复: 10
打印 上一主题 下一主题

请大家指教这个程序有什么问题?(特别重要!)急! 请求解答! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-05-25 13:52 |只看该作者 |倒序浏览
首先想问下大家:你们在编译程序的时候,用过Dev-C++个吗?小弟在用这个的同时,还用VC60编译器。如下是我写的一个小程序,望大家指教:
#include <stdio.h>;
#include <stdlib.h>;
#include <malloc.h>;
#include <string.h>;

typedef struct list_t {
    int id;
    char name[41];
    int age;
    struct list_t *next;
}LIST;

LIST *add_node(int id, char name[], int age, LIST *p);
LIST *remove_node(int id, LIST *p);
void display_node(LIST *head);
int save_list(LIST *head);
LIST *load_list(void);
void free_node(LIST *head);

int main(void)
{
    int a, id, age;
    char ch, s[50], con = 'Y', name[41];
    LIST *head = NULL;

    if((head = (LIST *)malloc(sizeof(LIST))) == NULL) {
        printf("Insertion is not success!";
        exit(1);
    }
      
    head->;next = NULL;

    do {
        printf("A(Add) R(Remove) D(Display) S(Save) L(Load) Q(QUIT)\n";
        printf("lease  change:";
        scanf("%s", &ch);
        gets(s);

        switch(ch) {
            case 'A':
                do {
                    while(1) {
                        printf("lease input the \"id\":";
                        scanf("%d", &id);
                                        gets(s);

                        if(id < 1 || id >; 9999 ) {
                            printf("error!\n";
                            continue;
                        }
                        break;
                    }

                    while(1) {
                        printf("lease input the \"name\"(less than 40 letters):";
                        scanf("%s", &name);
                                    gets(s);

                        a = strlen(name);
                        if(a >; 40) {
                            printf("error!\n";
                            continue;
                        }
                        break;
                    }

                    while(1) {
                        printf("lease input the \"age\"(1 - 200):";
                        scanf("%d", &age);
                        gets(s);

                        if(age < 1 || age >; 200) {
                            printf("error!\n";
                            continue;
                        }
                        break;
                    }

                    head = add_node(id, name, age, head);

                    printf("Continue?(Y/N):";
                    scanf("%s", &con);
                    gets(s);

                                    while((con != 'Y') && (con != 'N')) {
                                            printf("Input error!\n");
                                            printf("Continue?(Y/N):");
                        scanf("%s", &con);
                                        gets(s);
                                            if((con == 'N') || (con == 'Y')) {
                                                    break;
                                            }
                                    }

                }while(con == 'Y');

                break;
            case 'R':
                                if(head->;next == NULL) {
                                        printf("The list is empty!\n");
                    break;
                }
                                printf("lease input the \"id\":");
                scanf("%d", &id);
                                gets(s);
                head = remove_node(id, head);
                break;
            case 'D':
                display_node(head);
                break;
            case 'S':
                save_list(head);
                printf("File Save OK!\n");
                break;
            case 'L':
                free_node(head->;next);
                head = load_list();
                break;
            case 'Q':
                printf("The end!\n");
                free_node(head->;next);
                exit(0);
            default:
                printf("Input error! Again!\n");
        }

    } while(ch != '\0');

    return 0;
}

LIST *add_node(int id, char name[], int age, LIST *p)
{
    LIST *head = p, *pr = NULL, *new = NULL;

    while(p != NULL && p ->; id < id) {
        pr = p;
        p = p ->; next;
    }
    if((new = (LIST *)malloc(sizeof(LIST))) == NULL) {
        printf("Insertion is not success!");
        exit(1);
    } else {
        new->;id = id;
        strcpy(new->;name, name);
        new->;age = age;
        new->;next = pr->;next;
        pr->;next = new;
    }

    return (head);
}

LIST *remove_node(int id, LIST *p)
{
    LIST *head = p;
    LIST *p1 = NULL;
    LIST *p2 = NULL;

    p1 = head;
    while(id != p1->;id && p1->;next != NULL) {
        p2 = p1;
        p1 = p1->;next;
    }
    if(id == p1->;id) {
        if(p1 == head) {
            head = p1->;next;
                        printf("delete OK!\n");
        } else {
            p2->;next = p1->;next;
                        free(p1);
                        printf("delete OK!\n");
        }
    } else {
        printf("%d not in list !\n", id);
    }

    return (head);
}

void display_node(LIST *head)
{
    LIST *p;

    if(head->;next == NULL) {
        printf("The list is empty!\n");
    } else {
        printf("Output the id :\n");
        p = head->;next;
        while(p != NULL) {
            printf("id: %d\n", p->;id);
            printf("name: %s\n", p->;name);
            printf("age: %d\n\n", p->;age);
            p = p->;next;
        }
    }
}

int save_list(LIST *head)
{
    LIST *p;
    FILE *fp;

    if((fp = fopen("a.txt", "w")) == NULL) {
        return(1);
    } else {
        if(head->;next == NULL) {
                    fprintf(fp, "The list is empty! Please add data first!\n");
        } else {
                p = head->;next;
            fprintf(fp, "ID\tName\tAge\n");

            while(p != NULL) {
                fprintf(fp, "%d\t%s\t%d\n", p->;id, p->;name, p->;age);
                p = p->;next;
            }
        }
    }

    fclose(fp);
    return 0;
}

LIST *load_list(void)
{
    int a = 12, id, age, i;
    char strid[6], name[41] = "", strage[4];
    FILE *fp;
    LIST *head = NULL;

    if((fp = fopen("a.txt", "r")) == NULL) {
        printf("Cannot open file!\n");
        exit(0);
    }
    fseek(fp, a, 0);

    for(i = 1; ;i ++) {
        if(fscanf(fp, "%s", strid) == EOF) {
            break;
        } else {
                        id = atoi(strid);
                        if(id == 0) {
                                printf("The list is empty! Please add data first!\n");
                break;
                        }
            fseek(fp, a = (a + strlen(strid) + 1), 0);
            fscanf(fp, "%s", name);
            fseek(fp, a = (a + strlen(name) + 1), 0);
            fscanf(fp, "%s", strage);
                        age = atoi(strage);
            fseek(fp, a = (a + strlen(strage) + 3), 0);

                    head = add_node(id, name, age, head);
                }
    }

    fclose(fp);
        return(head);
}

void free_node(LIST *head)
{
    LIST *nextdata = NULL;

    while(head != NULL) {
        nextdata = head;
        free(head);
        head = nextdata;
    }
}

有这样的问题:(1)我用Dev-C++编译的时候,在做add_node函数调用的时候说“指定的内存不能‘read’”是什么问题?而在VC60下编译,就没这样的问题。(2)我的load_node函数为什么不能执行?请大家给予指教。 谢谢 。

论坛徽章:
0
2 [报告]
发表于 2005-05-25 15:30 |只看该作者

请大家指教这个程序有什么问题?(特别重要!)急! 请求解答!

问题出在指针操作和堆释放上方面:
head最好是一个指向头节点的不变的指针,释放应由链尾向链头进行。
  1. void free_node(LIST *head)
  2. {
  3.    LIST *nextdata = NULL;

  4.    while(head != NULL) { //好像释放有问题
  5.        nextdata = head;
  6.        free(head);
  7.        head = nextdata;
  8.    }
  9. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2005-05-25 15:39 |只看该作者

请大家指教这个程序有什么问题?(特别重要!)急! 请求解答!

[quote]原帖由 "kernelxu"][/quote 发表:


能否给修改下?

论坛徽章:
0
4 [报告]
发表于 2005-05-26 00:37 |只看该作者

请大家指教这个程序有什么问题?(特别重要!)急! 请求解答!

楼主不会吧,能指出你的问题所在已经很8错了,范的错误也是分等级的,太低级的错误不太好说!

论坛徽章:
0
5 [报告]
发表于 2005-05-26 08:30 |只看该作者

请大家指教这个程序有什么问题?(特别重要!)急! 请求解答!

原帖由 "kernelxu" 发表:

问题出在指针操作和堆释放上方面:
head最好是一个指向头节点的不变的指针,释放应由链尾向链头进行。

代码:       
void free_node(LIST *head)
{
  LIST *nextdata = NULL;

  while(head != NULL) { //好像释放有问题
      nextdata = head;
      free(head);
      head = nextdata;
  }
}


这个...有问题吗?没看出来啊

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
6 [报告]
发表于 2005-05-26 08:47 |只看该作者

请大家指教这个程序有什么问题?(特别重要!)急! 请求解答!

>;>;在做add_node函数调用的时候说“指定的内存不能‘read’”是什么问题?
在插入到第一条记录之前的时候,pr = NULL,造成非法地址访问
>;>;我的load_node函数为什么不能执行?
没看到有个叫load_node的函数

论坛徽章:
0
7 [报告]
发表于 2005-05-26 08:50 |只看该作者

请大家指教这个程序有什么问题?(特别重要!)急! 请求解答!


  1. LIST *add_node(int id, char name[], int age, LIST *p)
  2. {
  3.    LIST *head = p, *pr = NULL, *new = NULL;

  4.    while(p != NULL && p ->; id < id) {
  5.        pr = p;
  6.        p = p ->; next; //p!=NULL不代表p->;next!=NULL;
  7.    }
  8.    if((new = (LIST *)malloc(sizeof(LIST))) == NULL) {
  9.        printf("Insertion is not success!");
  10.        exit(1);
  11.    } else {
  12.        new->;id = id;
  13.        strcpy(new->;name, name);
  14.        new->;age = age;
  15.        new->;next = pr->;next;
  16.        pr->;next = new;
  17.    }

  18.    return (head);
  19. }
复制代码

论坛徽章:
0
8 [报告]
发表于 2005-05-26 08:55 |只看该作者

请大家指教这个程序有什么问题?(特别重要!)急! 请求解答!

哦 对不起 是 load_list 函数

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
9 [报告]
发表于 2005-05-26 08:57 |只看该作者

请大家指教这个程序有什么问题?(特别重要!)急! 请求解答!

楼上的:应该是while循环体里可能根本进不去,这时pr=NULL
能进入循环,应该就没有啥问题了(后面用的是pr,不是p)

论坛徽章:
0
10 [报告]
发表于 2005-05-26 08:57 |只看该作者

请大家指教这个程序有什么问题?(特别重要!)急! 请求解答!

天啊,我不行了,您的LIST *load_list(void)根本就没法读啊
你用fprintf写的,就可以用fscanf读回来,用那么多seek干啥啊?
但是我觉得应该是用fwrite写整个的list和用fread读整个的list结构比较好吧?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP