免费注册 查看新帖 |

Chinaunix

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

请大家帮我看看这个程序的问题出在哪里,在下谢过了!关于链表的 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-28 08:06 |只看该作者 |倒序浏览
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
        int data;
        struct node *next;
}linklist;

void creatlist(linklist *S)            /* 就是这个函数的使用,是不是有问题*/
{
        S = (linklist *)malloc(sizeof(linklist));
        if (S == NULL){
                printf ("No enough memory!\n");
        }
        else {
                (S -> next = NULL);
        }
}
void outputlist(linklist *S)
{
        linklist *P;
       
        P = S -> next;
        while (P != NULL){
                printf ("%d\t",P -> data);
                P = P -> next;
        }
        printf ("\n");
}

void main()
{
        int n,i;
        linklist *L, *S;
                           /* 这里有使用是不是有点问题呢?*/
        creatlist(*&L);                                                    
        printf ("Input the items you want:");
        scanf ("%d\n",&n);
        for (i = 0; i < n; i ++ ){
                creatlist(*&S);
                L -> next = S;
                printf ("Input the %d data:", i + 1);
                scanf ("%d\n",*&S -> data);
        }
        outputlist(L);
}

[ 本帖最后由 liec 于 2006-9-28 08:23 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-09-28 08:39 |只看该作者
也不说你觉得问题在哪,想得到什么结果?

论坛徽章:
0
3 [报告]
发表于 2006-09-28 08:43 |只看该作者
就是先输入一个链表,再把链表输出

论坛徽章:
0
4 [报告]
发表于 2006-09-28 08:49 |只看该作者
不只是你说的地方有问题
还有很多问题

论坛徽章:
0
5 [报告]
发表于 2006-09-28 08:52 |只看该作者
帮忙说说吧,好吗!

论坛徽章:
0
6 [报告]
发表于 2006-09-28 09:02 |只看该作者
linklist *L, *S;//要初始NULL
void creatlist(linklist *S)//可改为linklist * creatlist(void),而且创建一个节点要连接到表尾
void outputlist(linklist *S)//不要打印漏了

建议重写

论坛徽章:
0
7 [报告]
发表于 2006-09-28 13:03 |只看该作者
void main()
{
        int n,i;
        linklist *L, *S;
        creatlist(*&L);                                      //为什么这样调用?creatlist(L);                                       
        printf ("Input the items you want:");
        scanf ("%d\n",&n);
//在下面的代码中,L为指乡链表头部的指针,S为指向新创建的结构体的指针,你的指向链表表尾的指针在那?
        for (i = 0; i < n; i ++ ){
                creatlist(*&S);
                L -> next = S;
                printf ("Input the %d data:", i + 1);
                scanf ("%d\n",*&S -> data);
        }
        outputlist(L);
}

应该这样:
void main()
{
        int n,i;
        linklist *L, *S,*tmp;
        creatlist(L);   
       tmp=L;                     //指向链表尾部                                                            
        printf ("Input the items you want:");
        scanf ("%d\n",&n);
        for (i = 0; i < n; i ++ ){
                creatlist(*&S);
                tmp -> next = S;            //把S加到表尾
                tmp = S;                          //tmp指向表尾
                printf ("Input the %d data:", i + 1);
                scanf ("%d\n",&S -> data);
        }
        outputlist(L);
}

[ 本帖最后由 cceczjxy 于 2006-9-28 13:07 编辑 ]

论坛徽章:
0
8 [报告]
发表于 2006-09-28 14:20 |只看该作者
void creatlist(linklist *S)

这个函数明显的内存泄漏啊

因为L到S属于值传递

L的值一直没变。。(用GDB调试,打印L的值,自己看看)

你的链表怎么产生?





你的程序应该修改成这个样子:

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
        int data;
        struct node *next;
}linklist;

void creatlist(linklist **S)
{
        *S = (linklist *)malloc(sizeof(linklist));
        if (*S == NULL){
                printf ("No enough memory!\n");
        }
        else {
                ((*S) -> next = NULL);
        }
}
void outputlist(linklist *S)
{
        linklist *P;

        P = S -> next;
        while (P != NULL){
                printf ("%d\t",P -> data);
                P = P -> next;
        }


}

int main()
{      
        int n,i;
        linklist *head, *tmp, *cur;
        creatlist(&head);
        cur = head;                          
        printf ("Input the items you want:");
        scanf ("%d",&n);
        for (i = 0; i < n; i ++ ){
                creatlist(&tmp);
                cur -> next = tmp;
                printf ("Input the %d data:", i + 1);
                scanf ("%d",&(tmp->data));
                printf("\n");
                cur = tmp;
        }
        outputlist(head);
        
        return 0;
}

[ 本帖最后由 dreamflyg 于 2006-9-28 14:25 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2006-09-28 15:14 |只看该作者
终于看到有人用2级指针了

论坛徽章:
0
10 [报告]
发表于 2006-09-28 16:00 |只看该作者
LZ没弄清楚值传递和地址传递。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP