数据结构双向循环链表

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int DataType;

typedef struct DoubleLinkList
{
	struct DoubleLinkList* prev;
	struct DoubleLinkList* next;
	DataType data;
}DLL;

void init(DLL** ppl);//初始化
void clear(DLL** ppl);//清空
void destroy(DLL* pl);//销毁
void print(DLL* pl);//打印
DLL* node(DataType x, DLL* prev, DLL* next);//获取新节点
void addLast(DLL* pl, DataType x);//添加
void add(DLL* pl, int idx, DataType x);//索引添加
DLL* removeLast(DLL* pl);//删除
DLL* remove(DLL* pl, int idx);//索引删除
DLL* getLast(DLL* pl);//查询
DLL* get(DLL* pl, int idx);//索引查询
int find(DLL* pl, DataType x);//查询索引
void set(DLL* pl, int idx, DataType x);//修改
int isEmpty(DLL* pl);//判空
int size(DLL* pl);//获取大小

#include "DLlist.h"

void init(DLL** ppl)
{
	assert(ppl);
	DLL* new = (DLL*)malloc(sizeof(DLL));//获取头节点
	if (!new)
	{
		printf("失败");
		exit(-1);
	}
	new->next = new;
	new->prev = new;
	*ppl = new;
}

void clear(DLL** ppl)
{
	assert(ppl);
	init(ppl);
}

void destroy(DLL* pl)
{
	assert(pl);
	DLL* cur = pl->next;
	DLL* next = cur->next;
	//释放数据节点
	while (cur != pl) {
		next = cur->next;//获取下一个节点
		free(cur);
		cur = next;
	}
	free(pl);//释放头节点
}

void print(DLL* pl)
{
	assert(pl);
	DLL* cur = pl->next;
	while (cur != pl)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

DLL* node(DataType x,DLL* prev, DLL* next)
{
	DLL* new = (DLL*)malloc(sizeof(DLL));
	new->data = x;
	new->prev = prev;
	new->next = next;
	return new;
}

void addLast(DLL* pl, DataType x)
{
	assert(pl);
	DLL* tail = pl->prev;
	DLL* new = node(x, tail, pl);
	tail->next = new;
	pl->prev = new;
}

void add(DLL* pl, int idx, DataType x)
{
	assert(pl);
	assert(idx >= 0);
	DLL* cur = idx==0?pl:get(pl, idx-1);//找到idx-1位置的节点
	//idx-1节点不存在
	if (cur == NULL)
		return;
	DLL* new = node(x, cur, cur->next);
	cur->next->prev = new;
	cur ->next = new;
}

DLL* removeLast(DLL* pl)
{
	assert(pl);
	if (isEmpty(pl))
		return NULL;
	DLL* cur = pl->prev->prev;//找到倒数第二个节点
	DLL* ans = cur->next;
	cur->next = pl;
	pl->prev = cur;
	return ans;
}


DLL* remove(DLL* pl, int idx)
{
	assert(pl);
	assert(idx >= 0);
	if (isEmpty(pl))
		return NULL;
	DLL* cur = idx == 0 ? pl : get(pl, idx - 1);//找到idx-1位置的节点
	//idx-1或idx节点不存在
	if (cur == NULL || cur->next == pl)
		return NULL;
	DLL* ans = cur->next;
	cur->next = ans->next;
	ans->next->prev = cur;
	return ans;
}

DLL* getLast(DLL* pl)
{
	assert(pl);
	if (isEmpty(pl))//空表
		return NULL;
	DLL* cur = pl;
	while (cur->next != pl)
	{
		cur = cur->next;
	}
	return cur;
}

DLL* get(DLL* pl, int idx)
{
	assert(pl);
	assert(idx >= 0);
	DLL* cur = pl->next;
	while (idx-- && cur != pl)
	{
		cur = cur->next;
	}
	if (idx > 0||cur==pl)//节点不存在
		return NULL;
	return cur;
}


int find(DLL* pl, DataType x)
{
	assert(pl);
	int idx = 0;
	DLL* cur = pl->next;
	while (cur != pl)
	{
		if (cur->data == x)
			return idx;
		cur = cur->next;
		idx++;
	}
	return -1;
}


void set(DLL* pl, int idx, DataType x)
{
	assert(pl);
	assert(idx >= 0);
	DLL * cur = get(pl, idx);
	if (cur == NULL)
		return;
	cur->data = x;
}


int isEmpty(DLL* pl)
{
	assert(pl);
	return pl->next == pl ? 1 : 0;
}

int size(DLL* pl)
{
	assert(pl);
	if (pl->next == pl)
		return 0;
	DLL* cur = pl;
	int size = 0;
	while (cur->next != pl)
	{
		cur = cur->next;
		size++;
	}
	return size;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值