#include<stdio.h>//链表遍历示意图
#include<malloc.h>
#include<stdbool.h>
struct node
{
int data;
struct node * pnext;
};//链表
struct node *creatlist();
void traverselist();
bool emptylist(struct node* phead)
{
if (phead->pnext == NULL)
return true;
else
return false;
}
int main(void)
{
struct node * phead;//用来存放头指针
phead = creatlist();
traverselist(phead);
return 0;
}
struct node * creatlist(void)
{
int len,i,val;
struct node* phead = (struct node*)malloc(sizeof(struct node));
if (NULL == phead)
{
printf("分配失败,程序终止\n");
exit(-1);
}
struct node* ptail = phead;
ptail->pnext =NULL;
printf("请输入您需要生成的链表节点的个数:len=");
scanf("%d", &len);
for (i = 0; i < len; ++i)
{
printf("请输入%d个节点的值:", i + 1);
scanf("%d", &val);
struct node* pnew = (struct node*)malloc(sizeof(struct node));
if (NULL == pnew)
{
printf("分配失败,程序终止\n");
exit(-1);
}
pnew->data = val;
ptail->pnext = pnew;
pnew->pnext = NULL;
}
return phead;
}
void traverselist(struct node* phead)
{
struct node* p=phead->pnext;
while (p != NULL)
{
printf("%d\n", p->data);
p = p->pnext;
}
return;
}
03-17
4232
