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

双链表的创建 C语言实现

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

//定义链表结构体
typedef struct node
{
	char name[20];
	struct node *prior;
	struct node *next;
}stud;

//创建链表,其中链表的节点数是count
stud *create(int count)
{
	stud *pNewNode = NULL;
	stud *pNode = NULL;
	stud *head = NULL;
	int i;

	//初始化头结点
	head = (stud*)malloc(sizeof(stud));
	head->name[0] = '\0';
	head->next = NULL;
	head->prior = NULL;

	pNode = head;
	for(i = 0;i < count;i++)	//循环创建节点
	{
		pNewNode = (stud*)malloc(sizeof(stud));
		pNode->next = pNewNode;
		printf("Input the %d students name:",i+1);
		scanf("%s",pNewNode->name);
		pNewNode->prior = pNode;
		pNewNode->next = NULL;
		
		//移动pNode节点到最新的末尾节点
		pNode = pNewNode;
	}
	pNode = NULL;
	return head;
}

int main()
{
	int count;	//定义链表的大小,节点个数
	stud *head = NULL;
	stud *temp = NULL;

	puts("Input the size of list: ");
	scanf("%d",&count);

	head = create(count);	//创建链表
	temp = head;
	while(temp)		//输出链表数据
	{
		printf("%s",temp->name);
		temp = temp->next;
	}

	

	return 0;
}

抱歉!评论已关闭.