# include <stdio.h>
# include <malloc.h>
struct Node
{
int data;
struct Node * pNext;
};
struct Node * create_list()
{
int len;
struct Node * pHead = (struct Node *)malloc(sizeof(struct Node));
struct Node * p = pHead;
pHead->pNext = NULL;
printf("请输入节点个数:");
scanf("%d", &len);
for (int i = 0; i < len; i++)
{
struct Node * pNew = (struct Node *)malloc(sizeof(struct Node));
pNew->pNext = NULL;
if (pNew)
{
pHead->pNext = pNew;
pNew->data = i;
pNew->pNext = NULL;
pHead = pNew;
}
else
{
printf("空间分配失败!\n");
return p;
}
}
return p;
}
void traverse_list(struct Node * pHead)
{
if (!pHead->pNext)
{
printf("该链表为空!\n");
return;
}
while (pHead)
{
printf("%d\n", pHead->pNext->data);
pHead = pHead->pNext;
}
return;
}
int main(void)
{
traverse_list(create_list());
return 0;
}