这是个简易链表,我依次输入1 2 3但是最后只会输出1,并且输出1后,Node* p就会指向NULL,我不理解为什么会这样,请各位帮我看看
#include<stdio.h>
#include<stdlib.h>
typedef struct _node {
int value;
struct _node* next;
}Node;
typedef struct {
Node* head;
}List;
void add(List* plist, int number);
int main(int argc, char* argv[])
{
List list;
list.head=NULL;
int number;
do {
scanf("%d", &number);
add(&list, number);
} while (number != -1);
Node*p;
for(p=list.head;p;p=p->next){
printf("p=%d\n",p->value);
}
return 0;
}
void add(List* plist, int number)
{
if (number != -1)
{
Node* p = (Node*)malloc(sizeof(Node));
p->value = number;
p->next=NULL;
Node* last = plist->head;
if (last)
{
while (last->next) {
last = last->next;
last->next = p;
}
}else {
plist->head = p;
}
}
}