双循环链表

这篇博客详细介绍了如何使用C语言实现双向循环链表的初始化、头插、尾插、按位置插入和删除等操作。还包含了查找节点前驱、后继以及按值删除节点的功能,并提供了清空和销毁链表的方法。示例代码展示了这些功能的实际应用。
摘要由CSDN通过智能技术生成
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>

typedef int elemtype;

typedef struct CDNode
{
	union
	{
		int data;
		int length;
	};
	struct CDNode* next;
	struct CDNode* prior;
}CDNode, * PCDNode;



//初始化
void Init_list(PCDNode plist);

//头插
bool Insert_head(PCDNode plist, int val);

//尾插
bool Insert_tail(PCDNode plist, int val);

bool Del_head(PCDNode plist);
bool Del_tail(PCDNode plist);
//按位置插入
bool Insert_pos(PCDNode plist, int pos, int val);

//按位置删除

bool Del_pos(PCDNode  plist, int pos);

//按值删除 遇到值为val的第一个有效节点,返回其地址
PCDNode Del_val(PCDNode plist, int val);

//寻找值为val的节点的前驱
PCDNode prior(PCDNode plist, int val);

//寻找值为val节点的后继
PCDNode Get_next(PCDNode plist, int val);

//判空
bool IsEmpty(PCDNode plist);

//获取其有效长度
int Get_length(PCDNode plist);

//清空
void Clear(PCDNode plist);

//销毁
void Destory(PCDNode plist);
void Destory2(PCDNode plist);

//打印
void Show(PCDNode plist);
*/

#include"ctwolist1.h"





//初始化
void Init_list(PCDNode plist)
{
	assert(plist != NULL);
	if (plist == NULL)return;
	plist->next = plist;
	plist->prior = plist;
}
//按位置插入
bool Insert_pos2(PCDNode  plist, int pos, int val)
{
	assert(plist != NULL);
	if (pos<0 || pos>Get_length(plist))return false;
	CDNode* s = (CDNode*)malloc(sizeof(CDNode));
	CDNode* p = plist;
	assert(s != NULL);
	int i = 0;
	s->data = val;
	while (i < pos)
	{
		p = p->next;
		i++;
	}
	s->next = p->next;//这两步位置不能调换,为了保证后边的节点不丢失
	s->prior = p;
	p->next->prior = s;
	p->next = s;
	return true;
}

//头插
bool Insert_head(PCDNode plist, int val)
{
	Insert_pos2(plist, 0, val);
	return true;
}



//尾插
bool Insert_tail(PCDNode plist, int val)
{
	Insert_pos2(plist, Get_length(plist), val);
	return true;
}

//按位置删除
bool Del_pos(PCDNode plist, int pos)
{
	assert(plist != NULL);
	CDNode* p = plist;
	int i = 0;
	while (i < pos)
	{
		p = p->next;
		i++;
	}
	CDNode* q = p->next;//q指针必须定义在此处,不可在初始位置定义
	p->next = q->next;
	q->next->prior = p;
	free(q);
	q = NULL;
	return true;
}

bool Del_head(PCDNode plist)
{
	Del_pos(plist, 0);
	return true;
}

bool Del_tail(PCDNode plist)
{
	assert(plist != NULL);
	Del_pos(plist, Get_length(plist)-1);
	return true;
}



//寻找值为val的节点的前驱
PCDNode Get_prior(PCDNode plist, int val)
{
	assert(plist != NULL);
	CDNode* p = plist->next;
	int i = 0;
	while (p != plist)
	{
		if (p->data == val)
		{
			return p->prior;
			break;
		}
		else
			p = p->next;
	}
	return NULL;
}

//寻找值为val节点的后继
PCDNode Get_next(PCDNode plist, int val)
{
	assert(plist != NULL);
	CDNode* p = plist->next;
	int i = 0;
	while (p != plist)
	{
		if (p->data == val)
		{
			return p->next;
			break;
		}
		else
			p = p->next;
	}
	return NULL;
}

//按值删除 遇到值为val的第一个有效节点,返回其地址
PCDNode Del_val(PCDNode plist, int val)
{
	assert(plist != NULL);
	CDNode* p = Get_prior(plist, val);
	assert(p != NULL);
	CDNode* q = p->next;
	p->next = q->next;
    q->next->prior = p;
	free(q);
	q = NULL;
	return NULL;
}


//判空
bool IsEmpty(PCDNode plist)
{
	return plist->next == NULL;
}

//获取其有效长度
int Get_length(PCDNode plist)
{
	int count = 0;
	CDNode* p = plist->next;
	while (p != plist)
	{
		p = p->next;
		count++;
	}
	return count;
}

//清空
void Clear(PCDNode plist)
{
	Destory(plist);
}
//销毁
void Destory(PCDNode plist)
{
	assert(plist != NULL);
	while (plist->next != plist)//每次判断第一有效节点是否存在
	{
		CDNode* p = plist->next;
		plist->next = p->next;
		free(p);
	}
	plist->next = plist;//此时没有有效节点,那么将plist->next置NULL;
}
void Destory2(PCDNode plist)
{
	CDNode* p = plist->next;
	CDNode* q = NULL;
	plist->next = plist;
	if (p != plist)
	{
		q = p->next;
		free(p);
		p = q;
	}
}

//打印
void Show(PCDNode plist)
{
	assert(plist != NULL);
	CDNode* p = plist->next;
	while (p != plist)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}

int main()
{
	CDNode ps;
	Init_list(&ps);
	for (int i = 0; i < 10; i++)
	{
		Insert_head(&ps, i + 1);
	}
	Show(&ps);
	printf("length=%d\n", Get_length(&ps));
	Del_head(&ps);
	Show(&ps);
	printf("length=%d\n", Get_length(&ps));
	Del_tail(&ps);
	Show(&ps);
	printf("length=%d\n", Get_length(&ps));
	Del_val(&ps,8);
	Show(&ps);
	printf("length=%d\n", Get_length(&ps));
	Destory2(&ps);
	printf("length=%d\n", Get_length(&ps));
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值