五、C语言实现双向循环链表

五、C语言实现双向循环链表

1、逻辑图
在这里插入图片描述
2、初始化
在这里插入图片描述
3、关于插入位置的处理
在这里插入图片描述
4、插入操作

插入操作:无需特殊考虑是头插还是尾插,别忘了head->length++;

在这里插入图片描述
5、删除操作

同理,删除操作也无需考虑头、尾删除操作,也别忘了head->length- -;
在这里插入图片描述

6、声明

#pragma once
//结构声明
typedef int DataType;

typedef struct Node
{
	union 
	{
		DataType data;
		int length;
	};

	struct Node* prior;
	struct Node* next;
}DoubleCirList;
//方法声明

//初始化
void InitList(DoubleCirList *head);

//按位置插入
void InsertOfPos(DoubleCirList* head, DataType value, int pos);

//头插
void InsertOfHead(DoubleCirList* head, DataType value);

//尾插
void InsertOfRear(DoubleCirList* head, DataType value);

//判空操作
bool IsEmpty(DoubleCirList* head);

//按位置删除
void DeleteOfPos(DoubleCirList* head, int pos);

//头删
void DeleteOfHead(DoubleCirList* head);

//尾删
void DeleteOfRear(DoubleCirList* head);

//输出
void ShowList(DoubleCirList* head);

//销毁
void DestoryList(DoubleCirList* head);



7、方法实现

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

//申请新结点
static DoubleCirList* ApplyNode(DataType value, DoubleCirList* prior, DoubleCirList* next)
{
	DoubleCirList* newspace = (DoubleCirList*)malloc(sizeof(DoubleCirList));
	if (newspace == NULL) exit(0);

	newspace->data = value;
	newspace->prior = prior;
	newspace->next = next;

	return newspace;
}

//初始化
void InitList(DoubleCirList* head)
{
	if (head == NULL) exit(0);
	
	head->next = head;
	head->prior = head;
	head->length = 0;
}

//按位置插入
void InsertOfPos(DoubleCirList* head, DataType value, int pos)
{
	if (head == NULL) exit(0);

	if (pos < 0) exit(0);
	//处理pos
	pos = pos % (head->length + 1);
	//找到pos位置的前驱
	DoubleCirList* p = head;
	while (pos)
	{
		p = p->next;
		pos--;
	}

	DoubleCirList* newnode = ApplyNode(value, p, p->next);
	if (newnode == NULL) exit(0);

	p->next->prior = newnode;
	p->next = newnode;

	head->length++;

}
//头插
void InsertOfHead(DoubleCirList* head, DataType value)
{
	InsertOfPos(head, value, 0);
}
//尾插
void InsertOfRear(DoubleCirList* head, DataType value)
{
	InsertOfPos(head, value, head->length);
}

//判空操作
bool IsEmpty(DoubleCirList* head)
{
	if (head == NULL) exit(0);

	return head->length == 0;
}
//按位置删除
void DeleteOfPos(DoubleCirList* head, int pos)
{
	if (head == NULL) exit(0);

	if (IsEmpty(head)) exit(0);

	pos = pos % (head->length + 1);

	DoubleCirList* p = head;
	while (pos)
	{
		p = p->next;
		pos--;
	}

	DoubleCirList* q = p->next;
	p->next = q->next;
	q->next->prior = p;

	free(q);
	q = NULL;

	head->length--;
}

//头删
void DeleteOfHead(DoubleCirList* head)
{
	DeleteOfPos(head, 0);
}

//尾删
void DeleteOfRear(DoubleCirList* head)
{
	DeleteOfPos(head, head->length-1);
}

//输出
void ShowList(DoubleCirList* head)
{
	if(IsEmpty(head)) exit(0);

	DoubleCirList* p = head->next;
	DoubleCirList* q = head;

	printf("正序: ");
	while (p != q)
	{
		printf("%d ",p->data);
		p = p->next;
	}
	printf("\n");

	q = q->prior;
	printf("逆序: ");
	while (q != p)
	{
		printf("%d ", q->data);
		q = q->prior;
	}
	printf("\n\n");
}

//销毁
void DestoryList(DoubleCirList* head)
{
	if (head == NULL) exit(0);

	while (head->length)
	{
		DeleteOfHead(head);
	}

}

8、主函数测试

#include <stdio.h>
#include "Double_Circular_List.h"

int main()
{
	DoubleCirList head;

	InitList(&head);

	//头、头插入
	InsertOfHead(&head, 12);
	InsertOfHead(&head, 34);
	InsertOfRear(&head, 88);
	ShowList(&head);

	//2、1位置插入
	InsertOfPos(&head, 66, 2);
	InsertOfPos(&head, 42, 1);
	ShowList(&head);

	//头、尾删
	DeleteOfHead(&head);
	DeleteOfRear(&head);
	ShowList(&head);

	//按位置删除
	DeleteOfPos(&head, 1);
	ShowList(&head);

	DestoryList(&head);

	return 0;
}

9、测试结果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值