创建链表的一个简单问题
代码如下:----------------------------------------------------------------------------
# include <stdio.h>
# include <stdlib.h>
struct node
{
char data;
node * next;
};
void add(node * head)
{
printf("Now add some data to linked list\n0 for null\n");
char oData=0;
node * p1=NULL, *p2=NULL;
while(scanf("%c%*c", &oData))
{
if (oData=='0')
break;
p1=(node *)malloc(sizeof(node));
p1->data=oData;
p1->next=NULL;
if (head==NULL)
{
head=p1;
p2=head; //¾í2»ÄüD′Ôúò»DD£¿
}
else
{
p2->next=p1;
p2=p1;
}
printf("0 for null\n");
}
}
void printAll(node * head)
{
node *p1=head;
while (p1!=NULL)
{
printf("%c ", p1->data);
p1=p1->next;
}
printf("\n");
}
void main()
{
node * head=NULL;
add(head); //add函数执行完后head仍未空指针这是为何?而如果add改为有返回值node*,然后写成这个形式 head=add(head),此时head却不为空这是为何?
printAll(head);
}