标题:为什么用尾插法建立链表会无法输入数据?
只看楼主
温酒斩化腾
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2017-3-28
结帖率:72.73%
已结贴  问题点数:14 回复次数:4 
为什么用尾插法建立链表会无法输入数据?
#include<stdio.h>
#include<stdlib.h>
typedef struct player
{
    int value;
    struct player *next;
}PLAYER;
int main()
{
    int number=0,i=0;
    PLAYER *head=NULL;
    PLAYER *p=head;
    do
    {
        scanf("%d",&number);
        if(number!=-1)
        {
            i++;
            PLAYER *q=(PLAYER *)malloc(sizeof(PLAYER));
            q->value=number;
            q->next=NULL;
            p->next=q;
            p=q;
        }
    }
    while(number!=-1);
}
搜索更多相关主题的帖子: 尾插法 player int next number 
2017-07-12 16:24
静水且流深
Rank: 5Rank: 5
等 级:贵宾
威 望:11
帖 子:60
专家分:319
注 册:2017-7-7
得分:7 
加个实例化,然后遍历时跳过第一个节点
程序代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct player
{
    int value;
    struct player *next;
}PLAYER;
int main()
{
    int number=0,i=0;
    PLAYER *head=(PLAYER *)malloc(sizeof(PLAYER));//应该是头结点未初始化,空值调用引发的吧
    PLAYER *p=head;
    do
    {
        scanf("%d",&number);
        if(number!=-1)
        {
            i++;
            PLAYER *q=(PLAYER *)malloc(sizeof(PLAYER));
            q->value=number;
            q->next=NULL;
            p->next=q;
            p=q;
        }
    }
    while(number!=-1);
}

不过是爱情,又能走多久
2017-07-12 16:40
ab2642560312
Rank: 2
等 级:论坛游民
帖 子:4
专家分:10
注 册:2017-7-10
得分:7 
#include<stdio.h>
#include<stdlib.h>
typedef struct player
{
    int value;
    struct player *next;
}PLAYER;
void main()
{
    int number=0,i=0;
    PLAYER *head=(PLAYER *)malloc(sizeof(PLAYER));
    PLAYER *p=head;

    do
    {
        scanf("%d",&number);
        if(number!=-1)
        {
            i++;
            PLAYER *q=(PLAYER *)malloc(sizeof(PLAYER));
            q->value=number;
            q->next=NULL;
            p->next=q;
            p=q;
        }
    }
    while(number!=-1);
}
2017-07-12 16:42
温酒斩化腾
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2017-3-28
得分:0 
回复 2楼 静水且流深
谢谢
2017-07-12 16:50
温酒斩化腾
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2017-3-28
得分:0 
回复 3楼 ab2642560312
谢谢啊
2017-07-12 16:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-479353-1-1.html




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

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