| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 352 人关注过本帖
标题:链表找错 编译链接无错 输入后有错
只看楼主 加入收藏
烟雾中的迷茫
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:621
专家分:1069
注 册:2011-2-9
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
链表找错 编译链接无错 输入后有错
程序代码:
#include<stdio.h>
#include<stdlib.h>

struct test
{
    int num;
    struct test *next;
};

int main(int argc,char *argv[])
{
    struct test* init();
    void relp(struct test *head);
    struct test *head;

    head=init();
    relp(head);
    return 0;
}

struct test* init()
{
    struct test *head,*p,*t;

    printf("依次输入各值\n");
    head=(int *)malloc(sizeof(int));
    scanf("%d",&head->num);
    p=t=head;
    p->next=NULL;

    while(p->num!=0)
    {
        t=(int *)malloc(sizeof(int));
        scanf("%d",&t->num);
        p->next=t;
        p=t;
    }
    p->next=NULL;
    return head;
}

void relp(struct test *head)
{
    struct test *p;
    p=head;
    while(p->next!=NULL)
    {
        free(p);
        p=head->next;
        head=head->next;
    }
    printf("释放完毕\n");
    return 0;
}
        

求指教
2011-08-02 09:34
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:20 
程序代码:
#include<stdio.h>
#include<stdlib.h>

 
struct test

 {
     int num;
     struct test *next;

 };

 
int main(int argc,char *argv[])

 {
     struct test* init();
     void relp(struct test *head);
     struct test *head;

 
    head=init();
     relp(head);
     return 0;

 }

 
struct test* init()

 {
     struct test *head,*p,*t;

 
    printf("依次输入各值\n");
     head = (struct test *)malloc(sizeof(struct test)); //head=(int *)malloc(sizeof(int));
     scanf("%d",&head->num);
     p=t=head;
     p->next=NULL;

 
    while(p->num!=0)
     {
         t=(struct test *)malloc(sizeof(struct test));//t=(int *)malloc(sizeof(int));
         scanf("%d",&t->num);
         p->next=t;
         p=t;
     }
     p->next=NULL;
     return head;

 }

 
void relp(struct test *head)

 {
     struct test *p;
     p=head;
     /*while(p->next!=NULL)
     {
         free(p);
         p=head->next;
         head=head->next;
     }*/
     while(p != NULL)
     {
         head = p;
         p = p->next;
         free(head);
     }
     printf("释放完毕\n");
     //return 0;
 }

重剑无锋,大巧不工
2011-08-02 09:49
烟雾中的迷茫
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:621
专家分:1069
注 册:2011-2-9
收藏
得分:0 
汗,都一个月么写了  就快忘了  谢了 呵
2011-08-02 10:06
快速回复:链表找错 编译链接无错 输入后有错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012098 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved