数据结构 双向链表 实现 纯代码

双向链表 操作函数原型声明

node_t * init();
//显示双向链表内容
void display(node_t *head);
//在双向链表中查找第I个节点存放的地址
node_t *find(node_t *head,int i);
//在双向链表中第I个节点后插入值为x的节点
node_t *insert(node_t *head,datatype x,int i);
//在双向链表中删除值为x的节点
node_t *delet(node_t *head,datatype x);

双向链表数据结构体

//双向链表
typedef int datatype;

typedef struct node
{
	struct node *prev;
	datatype info;
	struct node *next;
}node_t;

完整代码

#include <stdio.h>
#include <malloc.h>
//双向链表
typedef int datatype;

typedef struct node
{
	struct node *prev;
	datatype info;
	struct node *next;
}node_t;

node_t * init();
//显示双向链表内容
void display(node_t *head);
//在双向链表中查找第I个节点存放的地址
node_t *find(node_t *head,int i);
//在双向链表中第I个节点后插入值为x的节点
node_t *insert(node_t *head,datatype x,int i);
//在双向链表中删除值为x的节点
node_t *delet(node_t *head,datatype x);



node_t * init()
{
	node_t *head = (node_t *)malloc(sizeof(node_t));
	if(!head)
		printf("create a node faill!\n");
	head->prev = NULL;
	head->next = NULL;
	return head;
}
//显示双向链表内容
void display(node_t *head)
{
	node_t *p = head;
	int count = 0;
	printf("----- show list -----\n");
	while(p)
	{
		printf("%d : [%d]\n",count,p->info);
		count++;
		p = p->next;
	}
}
//在双向链表中查找第I个节点存放的地址
node_t *find(node_t *head,int i)
{
	node_t *p = head;
	int index = 0;
	//if(i < 1)
	//	return NULL;
	while(p && (index++ != i))
	{
		p = p->next;
	}
	if(!p)
	{
		printf("the list have no %d node\n",i);
		return NULL;
	}
	return p;
}
//在双向链表中第I个节点后插入值为x的节点
node_t *insert(node_t *head,datatype x,int i)
{
	node_t *p = NULL;
	/*
	//第0个节点不存放数据
	//插在第一个节点
	if(i == 0)
	{
		node_t *pnew = (node_t *)malloc(sizeof(node_t));
		if(!pnew)
		{
			printf("malloc err !\n");
			return NULL;
		}
		pnew->info = x;
		pnew->next = p;
		pnew->prev = p->prev;
		p->prev->next = pnew;
		p->prev = pnew;
	}
	*/
	
	p = find(head,i);
	if(p)
	{
		node_t *pnew = (node_t *)malloc(sizeof(node_t));
		if(!pnew)
		{
			printf("malloc err !\n");
			return NULL;
		}
		
		pnew->info = x;
		
		//当前P这个节点是最后一个节点
		if(p->next == NULL)
		{
			pnew->next = NULL;
			pnew->prev = p;
			p->next = pnew;
			return head;
		}
		//中间节点插入
		pnew->next = p->next;
		pnew->prev = p;
		p->next->prev = pnew;
		p->next = pnew;
		
		return head;
	}
	else
	{
		printf("p is NULL\n");
		return NULL;
	}
	
}
//在双向链表中删除值为x的节点
node_t *delet(node_t *head,datatype x)
{
	node_t *p = head;
	
	while(p && (p->info != x))
	{
		p = p->next;
	}
	if(!p)
	{
		printf("have no x node\n");
	}
	else
	{		
		p->prev->next = p->next;
		p->next->prev = p->prev;
		free(p);
		p = NULL;
		return head;
	}
}

int main()
{
	//双向链表测试
	node_t *dlink_head = init();
	insert(dlink_head,9,0);
	insert(dlink_head,10,1);
	insert(dlink_head,11,2);
	insert(dlink_head,12,3);
	insert(dlink_head,13,4);
	insert(dlink_head,14,5);
	display(dlink_head);
	printf("delet 11\n");
	dlink_head = delet(dlink_head,11);
	display(dlink_head);
	printf("delet 13\n");
	dlink_head = delet(dlink_head,13);
	display(dlink_head);
	printf("insert 99 88\n");
	insert(dlink_head,99,2);
	insert(dlink_head,88,1);
	display(dlink_head);
}

运行结果

猜你喜欢

转载自blog.csdn.net/qq_29796781/article/details/81700507