双向链表

一、双向链表简述

双向链表,顾名思义就是有两个指向的链表。将链表中的每个结点都设有两个指针域,一个指针指向其直接后继,另一个指针只想其直接前驱。它可以从链表中任意一个结点开始向两个方向遍历整个链表。

二、双向链表

在这里插入图片描述

#pragma once

typedef int ElemType;

typedef struct Node
{
	ElemType data;
	struct Node *prior;
	struct Node *next;
}DNode, *DList;

void InitDList(DList list);

int InsertDListPos(DList list, ElemType val, int pos);
int InsertDListHead(DList list, ElemType val);
int InsertDListTail(DList list, ElemType val);

void ShowDList(DList list);
void RShowDList(DList list);

int DeleteDListPos(DList list, int pos);
int DeleteDListHead(DList list);
int DeleteDListTail(DList list);

void ClearDList(DList list)
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

#include "dlist.h"

static void DeterPointIsNULL(DList list)
{
	assert(list != NULL);
	if (list == NULL)
	{
		printf("List is NULL, please check\n");
		exit(0);
	}
}

static int GetLength(DList list)
{
	DeterPointIsNULL(list);

	int count = 0;
	DList p = list;
	while (p->next)
	{
		count++;
		p = p->next;
	}

	return count;
}

DList _ApplyNode(ElemType val, DList prior, DList next)
{
	DList s = (DList)malloc(sizeof(DNode));

	s->data = val;
	s->prior = prior;
	s->next = next;

	return s;
}

void InitDList(DList list)
{
	list->next = NULL;
	list->prior = NULL;
}

int InsertDListPos(DList list, ElemType val, int pos)
{
	DeterPointIsNULL(list);

	if (pos < 0 || pos > GetLength(list))
	{
		printf("Pos is out of range, Insert fail");
		return 0;
	}

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

	DList s = _ApplyNode(val, p, p->next);

	if (p->next != NULL)
	{
		p->next->prior = s;
	}

	p->next = s;

	return 1;
}

int InsertDListHead(DList list, ElemType val)
{
	return InsertDListPos(list, val, 0);
}

int InsertDListTail(DList list, ElemType val)
{
	return InsertDListPos(list, val, GetLength(list));
}

void ShowDList(DList list)
{
	DeterPointIsNULL(list);

	DList p = list->next;
	while (p)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}

void RShowDList(DList list)
{
	DeterPointIsNULL(list);

	DList p = list->next;

	while (p->next != NULL)
	{
		p = p->next;
	}

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

int DeleteDListPos(DList list, int pos)
{
	DeterPointIsNULL(list);

	if (pos < 0 || pos >= GetLength(list))
	{
		printf("Pos is out of range, Delete fail\n");
		return 0;
	}

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

	DList q = p->next;
	
	p->next = q->next;
	if (q->next != NULL)
	{
		q->next->prior = p;
	}

	free(q);

	return 1;
}
int DeleteDListHead(DList list)
{
	return DeleteDListPos(list, 0);
}
int DeleteDListTail(DList list)
{
	return DeleteDListPos(list, GetLength(list) - 1);
}

void ClearDList(DList list)
{
	DeterPointIsNULL(list);

	while (list->next != NULL)
	{
		DeleteDListHead(list);
	}
}

三、双向循环链表

#pragma once

typedef int ElemType;

typedef struct Node
{
	ElemType data;
	struct Node *prior;
	struct Node *next;
}CDNode, *CDList;

void InitCDList(CDList list);

int InsertCDListPos(CDList list, ElemType val, int pos);
int InsertCDListHead(CDList list, ElemType val);
int InsertCDListTail(CDList list, ElemType val);

void ShowCDList(CDList list);
void RShowCDList(CDList list);

int DeleteCDListPos(CDList list, int pos);
int DeleteCDListHead(CDList list);
int DeleteCDListTail(CDList list);

void ClearCDList(CDList list);
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

#include "cdlist.h"

static void DeterPointIsNULL(CDList list)
{
	assert(list != NULL);
	if (list == NULL)
	{
		printf("List is NULL, please check");
		exit(0);
	}
}

static CDList _ApplyNode(ElemType val, CDList prior, CDList next)
{
	CDList s = (CDList)malloc(sizeof(CDNode));
	assert(s != NULL);

	s->data = val;
	s->prior = prior;
	s->next = next;

	return s;
}

static int GetLength(CDList list)
{
	DeterPointIsNULL(list);

	CDList p = list;
	int count = 0;

	while (p->next != list)
	{
		count++;
		p = p->next;
	}

	return count;
}

void InitCDList(CDList list)
{
	DeterPointIsNULL(list);

	list->prior = list;
	list->next = list;
}

int InsertCDListPos(CDList list, ElemType val, int pos)
{
	DeterPointIsNULL(list);

	if (pos < 0 || pos > GetLength(list))
	{
		printf("Pos is out of range, Insert fail\n");
		return 0;
	}

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

	CDList q = _ApplyNode(val, p, p->next);

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

	return 1;
}
int InsertCDListHead(CDList list, ElemType val)
{
	return InsertCDListPos(list, val, 0);
}
int InsertCDListTail(CDList list, ElemType val)
{
	//return InsertCDListPos(list, val, GetLength(list));

	DeterPointIsNULL(list);

	CDList p = list->prior;

	CDList q = _ApplyNode(val, p, p->next);
	p->next = q;
	p->next->prior = q;

	return 1;
}

void ShowCDList(CDList list)
{
	DeterPointIsNULL(list);

	CDList p = list->next;

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

void RShowCDList(CDList list)
{
	DeterPointIsNULL(list);

	CDList p = list->prior;

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

int DeleteCDListPos(CDList list, int pos)
{
	DeterPointIsNULL(list);

	if (pos < 0 || pos >= GetLength(list))
	{
		printf("Pos is out of range, Delete fail\n");
		return 0;
	}

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

	CDList q = p->next;

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

	free(q);

	return 1;
}
int DeleteCDListHead(CDList list)
{
	return DeleteCDListPos(list, 0);
}
int DeleteCDListTail(CDList list)
{
	DeterPointIsNULL(list);

	CDList p = list->prior;

	p->prior->next = list;
	list->prior = p->prior;

	free(p);

	return 1;
}

void ClearCDList(CDList list)
{
	while (list->next != NULL)
	{
		DeleteCDListHead(list);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值