| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 751 人关注过本帖
标题:linux链表操作问题
只看楼主 加入收藏
windycat
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-3-16
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:8 
linux链表操作问题
linux 平台下,操作链表报错,请教各位

insert_pool(struct pool *head,struct pool *new)
{   
    printf("Insert starting ....:%x\n",&head);
    if((head)==NULL)
    {
        (head)=new;
    }
    else
    {
        new->next=(head);
        (head)->pre=new;
        (head)=new;
    }
}

这样编译后,执行的是链表的备份。采用引用参数,编译报错,错误是 函数定义这句有问题:expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
 insert_pool(struct pool *&head,struct pool *&new)
高手指点一下
搜索更多相关主题的帖子: linux 链表 
2010-03-16 15:42
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
收藏
得分:0 
insert_pool(struct pool *&head,struct pool *&new)
看看有没有写错。。。
我用你上面的代码编译是可以通过的
2010-03-16 15:53
windycat
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-3-16
收藏
得分:0 
检查多次没错。linux RHEL5.0 gcc 是不是少了什么头文件 已经带了stdlib.h,stdio.h

高手来指点一下
2010-03-16 16:03
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
收藏
得分:0 
这样编译后,执行的是链表的备份。采用引用参数,编译报错,错误是 函数定义这句有问题:expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
                          |
                          V
insert_pool(struct pool * & head,struct pool *&new)

加了那个以后,报的错就跟你的一样了
                        

2010-03-16 16:07
windycat
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-3-16
收藏
得分:0 
是的,采用前面的方式,操作的是表头的备份,下次引用这个head,还是空链表,看了别人说引用参数,试了一下,编译就出错

有没有别的解决办法
2010-03-16 16:14
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
收藏
得分:0 
用二级指针,传参数的时候传头的地址,C里不用引用, 不同C++
insert_pool(struct pool **head,struct pool *new)
{   
    if((*head)==NULL)
    {
        (*head)=new;
    }
    else
    {
        new->next=(*head);
        (*head)->pre=new;
        (*head)=new;
    }
}
2010-03-16 16:19
windycat
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-3-16
收藏
得分:0 
试过了,编译没问题。运行的时候出问题了
insert_pool(struct pool **head,struct pool *new)
{   
    if((*head)==NULL)
    {
        (*head)=new;<<<----段错误
    }
    else
    {
        new->next=(*head);
        (*head)->pre=new;
        (*head)=new;
    }
}
2010-03-16 16:30
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
收藏
得分:10 
我要看看你怎样传的参数,你的参数有没有给它空间
这样帮你调好麻烦呀,信息太少了
2010-03-16 16:35
cnfarer
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:179
帖 子:3330
专家分:21157
注 册:2010-1-19
收藏
得分:10 
这样吧
insert_pool(struct pool **head,struct pool **new)
{   
    if((*head)==NULL)
    {
        (*head)=*new;
    }
    else
    {
        new->next=(*head);
        (*head)->pre=*new;
        (*head)=*new;
    }
}

★★★★★为人民服务★★★★★
2010-03-16 17:06
快速回复:linux链表操作问题
数据加载中...
 
   



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

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