一个使我困惑的‘等号’,帮忙解释一下各位大虾。
程序代码:
# include<stdio.h> # include<stdlib.h> # include<malloc.h> struct node { int data; struct node *next; }; struct node *creat_list(void); void traver_list(struct node *phead); int main() { struct node *phead = NULL; phead = creat_list(); traver_list(phead); return 0; } struct node *creat_list(void) { int i,len,val; struct node *phead = (struct node *)malloc(sizeof(struct node));//生成头指针,指向头结点(*phead); if(NULL == phead) { printf("内存分分配失败,程序结束!\n"); exit(-1); } struct node *ptail =phead; ptail->next = NULL; printf("请输入链表的结点个数:\n"); scanf_s("%d",&len); for(i=0;i<len;++i) { struct node *pnew = (struct node *)malloc(sizeof(struct node));//生成新结点(*pnew); if(NULL == pnew) { printf("内存分配失败,程序结束!\n"); exit(-1); } printf("请输入第%d个结点的值: ",i+1); scanf_s("%d",&val); pnew->data = val; ptail->next = pnew; pnew->next =NULL; ptail = pnew; } return phead; } void traver_list(struct node *phead) { struct node *p =phead->next; while (NULL != p )// { printf("%d ",p->data); p = p->next; } printf("\n"); return ; }这是一个链表的生成
有一个地方很困惑:
pnew->data = val;
ptail->next = pnew;
pnew->next =NULL;
ptail = pnew;
中:
tpail = pnew;
是什么意思,把pnew的复制一份给ptail?这里使我相当混乱,请高人解答。
指点。
[ 本帖最后由 cosam 于 2011-9-15 00:44 编辑 ]