建立链表并赋值后遍历输出,输出的结果带有一串乱码是为什么呢?
(刚学链表懵懵懂懂求解答,谢谢~)
代码如下——
#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;
}
结果: