xxs_s 2019-04-22 23:35 采纳率: 0%
浏览 999

请问建立链表并赋值,再遍历输出,输出的结果带有一串乱码是为什么呢?

建立链表并赋值后遍历输出,输出的结果带有一串乱码是为什么呢?
(刚学链表懵懵懂懂求解答,谢谢~)

代码如下——

#include
#include

struct node{
int data;//数据域
struct node* next;//指针域
};
int main()
{
int n,i;//确定链表长度
struct node head=(struct node)malloc(sizeof(struct node));//创建头结点
struct node p=(struct node)malloc(sizeof(struct node));//申请第一个结点
head->next=p;//连接头结点和首结点
p->next=NULL;
printf("please input the length:\n");
scanf("%d",&n);
printf("input the number:\n");
for(i=0;i {
struct node* s=(struct node*)malloc(sizeof(struct node));//创建并给结点s分配内存
scanf("%d",&s->data);//尾插法
p->next=s;
s->next=NULL;
p=s;
}
p=head;
while(p!=NULL)//遍历输出
{
printf("%-10d",p->next->data);
p=p->next;
}
return 0;
}

结果:
图片说明

  • 写回答

1条回答

  • harveyXXX 2019-04-23 10:40
    关注

    struct node p=(struct node)malloc(sizeof(struct node));//申请第一个结点
    head->next=p;//连接头结点和首结点
    p->next=NULL;

    第一个结点的data未赋值

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?