不理解下面代码是如何链起来的
主要每次循环都会做last=head,那这样不就是类似太阳花那样,head为中心,数据都在外面一圈吗?
但是,遍历是正确的,那不是就是说它链起来了吗?
求解答
代码如下
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct _node{
int data;
struct _node *pRear;
}node;
int main()
{
int number;
node *head=NULL;
do{
scanf("%d",&number);
if(number!=-1){
node *p=(node*)malloc(sizeof(node));
p->data=number;
p->pRear=NULL;
node *last=head;
// printf("last.p=%p ",&last);
// printf("last.p=%p ",last);
// printf("last->pRear_p=%p ",last->pRear);
// printf("head.p=%p ",&head);
// printf("head.p=%p ",head);
if(last){
while(last->pRear!=NULL){
last=last->pRear;
}
last->pRear=p;
}else{
head=p;
}
}
}while(number!=-1);
node *ps;
for(ps=head;ps;ps=ps->pRear){
printf("%d\n",ps->data);
}
return 0;
}