数据结构 -- 双向循环链表

本文介绍了一种常用的数据结构——双向循环链表,并提供了具体的C语言实现代码。该链表在Linux内核中用于进程描述符,具备方便的前后节点访问特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      这篇文章写的是双向循环链表,这也是个非常经典的数据结构,我们在看 Linux 内核代码时就经常会遇到它。比如,在Linux的内核中实现进程描述符,使用的就是双向循环链表,因为它使用起来很方便,每个节点不只是有 data 字段,还有 prev , next 字段,对于向前和向后查找相应的节点很方便,下面贴出实现代码,仍然是很简单的操作,且没有过多的错误控制:

头文件:

/*
 * dlut_dclist.h
 *
 *  Created on: 2014年1月13日
 *      Author: DLUTBruceZhang
 */

#ifndef DLUT_DCLIST_H_
#define DLUT_DCLIST_H_

typedef 		int 		need;

typedef struct _dclist
{
	need data;

	struct _dclist *next;
	struct _dclist *prev;

}dclist;

dclist *		dlut_dclist_create();

void			dlut_dclist_insert_to_head(dclist *, need);
void			dlut_dclist_insert_to_tail(dclist *, need);

void			dlut_dclist_delete_the_head(dclist *);
void			dlut_dclist_delete_the_tail(dclist *);
void			dlut_dclist_delete_the_data(dclist *, need);

need 			dlut_dclist_get_the_head(dclist *);
need			dlut_dclist_get_the_tail(dclist *);

void			dlut_dclist_print_the_dclist(dclist *);

void			dlut_dclist_delete_the_dclist(dclist *);

#endif /* DLUT_DCLIST_H_ */


C文件:

/*
 * dlut_dclist.c
 *
 *  Created on: 2014年1月13日
 *      Author: DLUTBruceZhang
 */


#include "dlut_dclist.h"
#include <stdlib.h>
#include <stdio.h>

dclist * dlut_dclist_create()
{
	dclist *head;

	head = (dclist *)malloc(sizeof(dclist));

	if (!head)
		return NULL;

	head -> data = 0;

	head -> next = head;
	head -> prev = head;

	return head;
}

void dlut_dclist_insert_to_head(dclist *head, need data)
{
	dclist *new_node;

	new_node = (dclist *)malloc(sizeof(dclist));

	if (!new_node)
		return;

	new_node -> data = data;

	new_node -> next = head -> next;
	head -> next -> prev = new_node;
	head -> next = new_node;
	new_node -> prev = head;

	head -> data += 1;

	return;
}

void dlut_dclist_insert_to_tail(dclist *head, need data)
{
	dclist *new_node;

	new_node = (dclist *)malloc(sizeof(dclist));

	if (!new_node)
		return;

	new_node -> data = data;

	dclist *_head = head;

	while (_head -> next != head)
		_head = _head -> next;

	new_node -> next = head;
	head -> prev = new_node;
	new_node -> prev = _head;
	_head -> next = new_node;

	head -> data += 1;

	return;
}

void dlut_dclist_delete_the_head(dclist *head)
{
	dclist *old_node;

	if (head -> next == head)
		return;

	old_node = head -> next;

	head -> next -> next -> prev = head;
	head -> next = head -> next -> next;

	head -> data -= 1;

	free(old_node);

	return;
}

void dlut_dclist_delete_the_tail(dclist *head)
{
	dclist *old_node;

	if (head -> next == head)
		return;

	dclist *_head = head;

	while (_head -> next != head)
		_head = _head -> next;

	old_node = _head;

	_head -> prev -> next = head;
	head -> prev = _head -> prev;

	head -> data -= 1;

	free(old_node);

	return;
}

void dlut_dclist_delete_the_data(dclist *head, need data)
{
	if (head -> next == head)
		return;

	dclist *_head = head;

	while (_head -> data != data && _head -> next != head)
		_head = _head -> next;

	if (_head -> data == data)
	{
		_head -> prev -> next = _head -> next;
		_head -> next -> prev = _head -> prev;

		head -> data -= 1;
	}

	return;
}

need dlut_dclist_get_the_head(dclist *head)
{
	return head -> data == 0 ? -1 : head -> next -> data;
}

need dlut_dclist_get_the_tail(dclist *head)
{
	return head -> data == 0 ? -1 : head -> prev -> data;
}

void dlut_dclist_print_the_dclist(dclist *head)
{
	dclist *_head = head -> next;

	while (_head != head)
	{
		printf("%d  ", _head -> data);

		_head = _head -> next;
	}

	printf("\n");

	return;
}

void dlut_dclist_delete_the_dclist(dclist *head)
{
	while (head -> next != head)
		dlut_dclist_delete_the_head(head);

	free(head);

	return;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值