数据结构双向链表的创建与初始化

#include
#include
#include

//定义节点类型
typedef int data_t;
typedef struct node
{
	data_t  data;//以整型数据为例
	struct node* prev;//指向struct node 点的指针
	struct node* next;//指向struct node 点的指针
}node_t;

int dlist_create(node_t**, data_t);//函数声明
/***
创建双向列表
head:接收头指针地址的指针变量
data:待存储的数据
return -1:链表创建失败
return 0:链表创建成功
***/
int dlist_create(node_t** head, data_t data)
{
	node_t* pnew = (node_t*)malloc(sizeof(node_t));
	if (pnew == NULL)
		return -1;
	pnew->data = data;
	pnew->prev = NULL;
	pnew->next = NULL;
	*head = pnew;
	return 0;
}
int main()
{
	node_t* head = NULL;
	dlist_create(&head, 56);
	printf("%d\n", head->data);
}

运行结果:

数据结构双向链表的创建与初始化_第1张图片

你可能感兴趣的:(数据结构,链表)