#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct line)
#define NULL 0
struct line{
int num;
struct line *next;
};
int main() {
struct line *p1,*p2,*head;
int j,k=0;
p1=p2=head=(struct line *)malloc(LEN);
scanf("%d",&p1->num);
while (p1->num!=0){
p1=(struct line *)malloc(LEN);
scanf("%d",&p1->num);
if(p1->num==0)
p2->next=NULL;
else{
p2->next=p1;
p2=p1;
}
k++;
}
p2->next=head;
p1=head->next;
p1=p1->next;
for (j = 1; j <= k; j++) {
printf("-->%d",p1->num);
p1=p1->next;
}
return 0;
}
首先p1 p2 head同指向同一片内存空间,
输入第一个p1->num=1时,p1内存大小加64位,
p2也值向p1,,跟p1内存空间大小一样
p1:3-4-1-2
p2:4-1-2-3
本文介绍了一个用C语言编写的程序,展示了如何创建动态链表并接收用户输入,存储整数,最后遍历链表打印元素。

被折叠的 条评论
为什么被折叠?



