链表遍历示意图

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值