| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1284 人关注过本帖
标题:为什么fidelast函数出错?
只看楼主 加入收藏
温酒斩化腾
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2017-3-28
结帖率:72.73%
收藏
已结贴  问题点数:7 回复次数:5 
为什么fidelast函数出错?
#include <stdio.h>
#include <stdlib.h>
typedef struct player
{
    int accont;
    char a[20];
    int power;
    struct player *next;
}PLAYER;
PLAYER *build(int num)
{
    PLAYER *prept,*head;
    int i;
    printf("input information:\n");
    PLAYER *pt=(PLAYER *)malloc(sizeof(PLAYER));
    if(pt!=NULL)
    {
        scanf("%d%s%d",&pt->accont,pt->a,&pt->power);
    }
    else
    {
        printf("error!\n");
        exit(0);
    }
    for(i=1;i<num;i++)
    {
        PLAYER *pt=(PLAYER *)malloc(sizeof(PLAYER));
        if(pt!=NULL)
        {
           scanf("%d%s%d",&pt->accont,pt->a,&pt->power);
        }
        else
        {
            printf("error!\n");
            exit(0);
        }
    }
    return head;
}
PLAYER *fidelast(PLAYER *head)
{
    PLAYER *pt=head;
    while(pt->next!=NULL)
    {
        pt=pt->next;
    }
    return pt;
}
PLAYER *fidenode(PLAYER *head,int num)
{
    int i;
    PLAYER *pt=head;
    for(i=1;i<num&&pt->next!=NULL;i++)
    {
        pt=pt->next;
    }
    return pt;
}
PLAYER *insertlast(PLAYER *head)
{
    PLAYER *prept,*pt;
    pt=(PLAYER *)malloc(sizeof(PLAYER));
    if(pt!=NULL)
        scanf("%d%s%d",&pt->accont,pt->a,&pt->power);
    else
        exit(0);
    prept=fidelast(head);
    prept->next=pt;
    pt->next=NULL;
    return head;
}
PLAYER *insert(PLAYER *head,int num)
{
    PLAYER *prept,*pt;
    pt=(PLAYER *)malloc(sizeof(PLAYER));
    if(pt!=NULL)
        scanf("%d%s%d",&pt->accont,pt->a,&pt->power);
    else
        exit(0);
    prept=fidenode(head,num);
    pt->next=prept->next;
    prept->next=pt;
    return head;
}

int main()
{
    PLAYER *q,*c;
    int a;
    printf("input how many:\n");
    scanf("%d",&a);
    q=build(a);
  
    printf("input what you wan fide:\n");
    scanf("%d",&a);
    c=fidenode(q,a);
    printf("%s",c->next);

}
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: player int next head NULL 
2017-07-01 20:52
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:0 
如果head为NULL ,那么pt为NULL,但是pt->next的值是什么?

09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-07-01 21:08
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
收藏
得分:7 
你的build子程序head指针并未赋值,完了还是野指针。
2017-07-01 21:17
温酒斩化腾
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2017-3-28
收藏
得分:0 
回复 3楼 ehszt
#include <stdio.h>
#include <stdlib.h>
typedef struct player
{
    int accont;
    char a[20];
    int power;
    struct player *next;
}PLAYER;
PLAYER *build(int num)
{
    PLAYER *prept,*head,*pt;
    int i;
    printf("input information:\n");
    pt=(PLAYER *)malloc(sizeof(PLAYER));
    if(pt!=NULL)
    {
        scanf("%d%s%d",&pt->accont,pt->a,&pt->power);
        head=pt;
        prept=pt;
    }
    else
    {
        printf("error!\n");
        exit(0);
    }
    for(i=1;i<num;i++)
    {
        PLAYER *pt=(PLAYER *)malloc(sizeof(PLAYER));
        if(pt!=NULL)
        {
           scanf("%d%s%d",&pt->accont,pt->a,&pt->power);
           prept->next=pt;
           prept=pt;
        }
        else
        {
            printf("error!\n");
            exit(0);
        }
    }
    return head;
}

PLAYER *fidenode(PLAYER *head,int num)
{
    int i=1;
    PLAYER *pt=head;
    while(i<num&&pt!=NULL)
    {
        pt=pt->next;
        i++;
    }
    return pt;
}

int main()
{
    PLAYER *q,*c;
    int a;
    printf("input how many:\n");
    scanf("%d",&a);
    q=build(a);
    printf("input what you wan fide:\n");
    scanf("%d",&a);
    c=fidenode(q,a);
    printf("%s",c->next);

}
我把指针的问题改了后为什么还是不对呢,请解释一下,谢谢
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
2017-07-01 23:14
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
收藏
得分:0 
printf("%s",c->next->a); //这里改下,你是要输出字符串
第二张图是越界,只有三个节点,第三个节点的next是什么?
2017-07-02 07:46
温酒斩化腾
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2017-3-28
收藏
得分:0 
回复 5楼 ehszt
谢谢大佬
2017-07-02 08:44
快速回复:为什么fidelast函数出错?
数据加载中...
 
   



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

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