数据结构之双向循环链表

双向循环链表的实现【C语言】

表示

在这里插入图片描述

双向循环链表的实现

  • 结构的声明:
#pragma once

typedef int elemType;

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

	struct Node *prior;
	struct Node *next;
}DoubleCircleList;


  • 方法的声明
//方法的声明
void InitDoubleCircleList(DoubleCircleList* head);

bool InsertDoubleCircleList(DoubleCircleList* head,elemType value,int pos);

bool InsertDoubleCircleListHead(DoubleCircleList* head,elemType value);

bool InsertDoubleCircleListRear(DoubleCircleList* head,elemType value);

bool DeleteDoubleCircleList(DoubleCircleList* head,int pos);

bool DeleteDoubleCircleListHead(DoubleCircleList* head);

bool DeleteDoubleCircleListRear(DoubleCircleList* head);

int GetLength(DoubleCircleList* head);

bool IsEmpty(DoubleCircleList* head);

void DestoryDoubleCircleList(DoubleCircleList* head);

void ShowList(DoubleCircleList* head);

  • 方法的实现
#include<stdio.h>
#include<stdlib.h>
#include"double_circle_list.h"

//申请新结点(只在本文件内使用)
static DoubleCircleList* ApplyNewNode(DoubleCircleList* pri,DoubleCircleList* nex,int value)
{
	DoubleCircleList *new_node = (DoubleCircleList*)malloc(sizeof(DoubleCircleList));
	if (new_node == NULL) return NULL;

	new_node->data = value;
	new_node->next = nex;
	new_node->prior =pri;

	return new_node;
}

//在point结点后面插入一个结点
static bool InsertNodeAfter(DoubleCircleList* point,elemType value)
{
	DoubleCircleList *new_node = ApplyNewNode(point,point->next ,value);
	if (new_node == NULL) return false;

	point->next->prior = new_node;
	point->next = new_node;

	return true;
}

//方法的实现
void InitDoubleCircleList(DoubleCircleList* head)
{
	if(head==NULL) exit(0);

	head->next=head->prior=head;
	head->length =0;
}

bool InsertDoubleCircleList(DoubleCircleList* head,elemType value,int pos)
{
	if(head==NULL) exit(0);

	if(pos<0) return false;

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

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

	if(InsertNodeAfter(p,value))
	{
		head->length ++;
		return true;
	}

	return false;
}

bool InsertDoubleCircleListHead(DoubleCircleList* head,elemType value)
{
	return InsertDoubleCircleList(head,value,0);
}

bool InsertDoubleCircleListRear(DoubleCircleList* head,elemType value)
{
	if(head==NULL) exit(0);

	DoubleCircleList *p=head->prior ;
	if (InsertNodeAfter(p, value))
	{
		head->length++;
		return true;
	}

	return false;
}

bool DeleteDoubleCircleList(DoubleCircleList* head,int pos)
{
	if(head==NULL) exit(0);

	if (pos < 0 || head->length == 0) return false;

	pos %= head->length;

	DoubleCircleList *p=head;

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

	DoubleCircleList *q=p->next ;
	q->next->prior = p;
	p->next = q->next;
	free(q);
	head->length--;

	return true;
}

bool DeleteDoubleCircleListHead(DoubleCircleList* head)
{
	return DeleteDoubleCircleList(head,0);
}

bool DeleteDoubleCircleListRear(DoubleCircleList* head)
{
	if(head==NULL) exit(0);

	DoubleCircleList *p=head->prior ;
	p->next->prior =p->prior ;
	p->prior->next =p->next ;

	free(p);
	head->length--;
	return true;
}

int GetLength(DoubleCircleList* head)
{
	if(head==NULL) exit(0);

	return head->length ;
}

bool IsEmpty(DoubleCircleList* head)
{
	if(head==NULL) exit(0);

	return head->length ==0;
}

void DestoryDoubleCircleList(DoubleCircleList* head)
{
	if(head==NULL) exit(0);

	while(!IsEmpty(head))
	{
		DeleteDoubleCircleListHead(head);
	}
}

void ShowList(DoubleCircleList* head)
{
	if(head==NULL) exit(0);

	DoubleCircleList *p=head->next ;
	int len=head->length ;

	printf("正向打印:\n");
	while(len)
	{
		printf("%d ",p->data );
		p=p->next ;
		len--;
	}

	printf("逆向打印:\n");
	while(p!=head)
	{
		printf("%d ",p->data );
		p=p->prior ;
	}
	printf("\n");
}

int main()
{
	DoubleCircleList head;
	InitDoubleCircleList(&head);

	for(int i=1;i<6;i++)
	{
		InsertDoubleCircleListHead(&head,i);
	}

	ShowList(&head);

	DeleteDoubleCircleList(&head,3);
	ShowList(&head);
	printf("链表长度:%d\n",GetLength(&head));
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值