刚学到链表始终有个问题,请各位能指教一下吗?
程序代码:#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
void crea(int);
int main(void) {
int x;
scanf("%d",&x);
crea(x);
system("pause");
return 0;
}
void crea( int n) {
struct node *p,*head;
head=(struct node *)malloc(sizeof(struct node));//创建头结点。
p=(struct node *)malloc(sizeof(struct node));//创建第一个结点。
head->next=p;//将头结点指向第一个结点。
for(int i=0; i<n; i++) {
scanf("%d",&p->data);//输入结点中的数据.
p->next=(struct node *)malloc(sizeof(struct node));//创建下一个结点.
p=p->next;//将这个结点指向下一结点.
p->next=0;//这句怎么改,始终多指向了一个地,我以为它是指向尾结点,实际上不是.
}
p=head->next;//指向首结点.
while(p) {
printf("%d ",p->data);
p=p->next;
}
}在输出链表时始终多指向了一个地址,如何修改呢.
[此贴子已经被作者于2018-5-3 08:41编辑过]






