五、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、测试结果