现在的位置: 首页 > 综合 > 正文

单链表的建立 C语言实现

2018年05月01日 ⁄ 综合 ⁄ 共 726字 ⁄ 字号 评论关闭
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>

struct LNode
{
	int data;
	struct LNode *next;
};

//创建链表,count为创建的链表的节点数目
struct LNode *create(int count)
{
	int i;
	struct LNode *pNode = NULL;
	struct LNode *pNewNode = NULL;
	struct LNode *head = NULL;

	printf("Input the integers :\n");
	for(i = count;i > 0;i--)
	{
		pNewNode = (struct LNode*)malloc(sizeof(struct LNode));//分配节点空间
		scanf_s("%d",&pNewNode->data);

		if(head == NULL)	//指定头结点
		{
			head = pNewNode;
			pNode = pNewNode;
		}
		else
		{
			pNode->next = pNewNode;
			pNode = pNewNode;
		}
	}
	
	pNode->next = NULL;
	return head;
}

//入口函数
int main()
{
	int count;//链表节点数
	struct LNode *node;
	printf("Input the count of nodes you want to create:");
	scanf_s("%d",&count);

	node = create(count);
	printf("The result is :\n");
	while(node)
	{
		printf("%d",node->data);
		node = node->next;
	}
	system("pause");
}





建立单链表的代码如下:

抱歉!评论已关闭.