# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
typedef struct Node
{
int data; //数据域
struct Node *pNext;//指针域
} NODE, *PNODE; //NODE 等价于 struct Node PNODE等价于 struct Node *
//函数声明
PNODE create_list(void);
void traverse_list(PNODE pHead);
int main(void)
{
PNODE pHead =NULL;// 等价于struct PNODE * pHead=NULL
pHead=create_list(); //创建一个非循环单链表,并将该链表的头结点地址赋给pHead;
traverse_list(pHead);
return 0;
}
PNODE create_list(void)
{
int len; //用来存放有效节点的个数
int i;
int val;
//分配了一个不存放有效数据的头结点
PNODE pHead =(PNODE)malloc(sizeof(NODE));
if (NULL == pHead)
{
printf("分配失败,终止程序
");
exit (-1);
}
PNODE pTail =pHead;
pTail->pNext =NULL;
printf("请输入您需要生成链表节点的个数: len =");
scanf("%d",len);
for(i=0; i<len; ++i)
{
printf("请输入第%d个节点的值:", i+1);
scanf("%d",&val);
PNODE pNew =(PNODE)malloc(sizeof(NODE));
if (NULL == pHead)
{
printf("分配失败,终止程序
");
exit (-1);
}
pNew->data =val;
pTail->pNext =pNew;
pNew->pNext =NULL;
pTail = pNew;
}
return pHead;
}
void traverse_list(PNODE pHead)
{
PNODE p = pHead->pNext;
while(NULL !=p)
{
printf("%d",p->data);
p =p->pNext;
}
printf("
");
return ;
}