关于链表的问题
下面的程序中,如果使用注释"//"后面的语句,编译ok,但是无法运行。希望高手解答下面两种写法的区别。
/*--------------------------------------------------------------------------
输入数字,分配内存存储输入的数字,然后将存储的数字输到屏幕
--------------------------------------------------------------------------*/
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int item;
struct node *next;
}Node;
typedef Node* List;
void InitList(List *pt);
bool AddValue(List *plist,int item);
void OutPut(const List *list);
void FreeMem(List list);
int main(void)
{
int num;
List list; //List *list;
InitList(&list);//InitList(list);
puts("Please input a number:");
scanf("%d",&num);
while(getchar()!='\n')
continue;
if(AddValue(&list,num)) //if(AddValue(list,num))
//printf("%d\n",list->item);//printf("%d\n",(*list)->item);
OutPut(&list);//OutPut(list);
else
puts("alloc failed!");
FreeMem(list);
puts("Bye!");
return 0;
}
void InitList(List *pt)
{
*pt=NULL;
}
bool AddValue(List *pt,int n)
{
Node *pnew;
Node *current;
current=*pt;
pnew=(Node *)malloc(sizeof(Node));
if(pnew==NULL)
{
puts("failed!");
return false;
}
if(current==NULL)
*pt=pnew;
else
{
while(current->next!=NULL)
current=current->next;
current->next=pnew;
}
pnew->next=NULL;
pnew->item=n;
return true;
}
void OutPut(const List *list)
{
Node *temp=*list;
while(temp!=NULL)
{
printf(">%d<\n",temp->item);
temp=temp->next;
}
}
void FreeMem(List list)
{
Node *p=list;
Node *pt=list;
while(p!=NULL)
{
pt=p->next;
free(p);
p=pt;
}
}
[ 本帖最后由 行书万里 于 2013-5-21 22:01 编辑 ]