简单的单链表(c语言实现)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

typedef struct lk
{
	int id;
	struct lk *pnext;
	int data;
}Node;
Node *CreateNode()
{
	Node *node = NULL;
	node = (Node *)malloc(sizeof(Node));
	node->pnext = NULL;
	node->data = (rand()%100);
	return node;
}
void InitList(Node **head ,int count)
{
	Node *temp = *head;
	int i = 0;
	
	if(temp == NULL)
	{
		printf("list init error !\n");
	}
	else
	{
		for(i = 0; i < count; i++)
		{
			temp->pnext = CreateNode();
			temp->pnext->id = i;
			//printf("i = %d    ",i);
			//printf("%d",temp->pnext->id);
			temp = temp->pnext;
		}
	}
	printf("\n");
}
void FreeList(Node *head)
{
	if(head->pnext == NULL)
	{
		if(head != NULL)
		{
			printf("head free \n");
			free(head);
		}
		printf("end free\n");
		return;
	}
	else
	{
		printf("one free\n");
		FreeList(head->pnext);
	}
}
void DisplayList(Node * head)
{
	Node *temp = head;
	if(temp == NULL)
	{
		printf("display error \n");
	}
	else
	{
		temp = temp->pnext;
		while(temp != NULL)
		{			
			printf(" id : %d ,data : %d \n",temp->id,temp->data);
			temp = temp->pnext;
		}
	}
}
int main()
{
	Node *list= NULL;
	list = (Node *)malloc(sizeof(Node));
	int count = 0;
	printf("input create nodes:");
	scanf("%d",&count);
	
	InitList(&list,count);
	DisplayList(list);
	FreeList(list);
	return 0;
}

只是简单的实现链表的创建和遍历。

Linux GCC 编译器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值