一、循环双链表
基于单链表实现循环双链表;相比于单链表,循环双链表的数据结点有两个指针,分别指向该节点的上一个数据节点(前驱)和下一个数据结点(后继),且头结点或首元结点的前驱指向链表的最后一个数据结点,而最后一个数据结点的后继指向头结点或首元结点。
二、基本操作
1.结点类型描述
typedef struct DNode{
ElemType data;
struct DNode* next, * prior;
}DNode,*LinkList;
指针next指向后继结点,prior指向前驱结点
2.初始化
void Initial(LinkList& L) { //初始化
L = (DNode*)malloc(sizeof(DNode));
if (L)
{
L->next = L;
L->prior = L;
L->data = 0;
printf("Initialization succeeded!\n");
}
else
printf("Error:Insufficient memory!\n");
}
初始化时头结点的前驱和后继都指向自身。
注:这里用头结点的数据域存储链表长度信息,因此初始化设为0,若无需要可忽略
3.按序查找
DNode* GetElem(LinkList L) //按序号查找
{
int i = 0;
LinkList p = L;
if (L->next == L->prior)
{
printf("Error:Tabular void\n");
return NULL;
}
printf("Please enter the number:");
scanf_s("%d", &i);
if ((i <= 0) || (i > L->data))
{
printf("Error:lllegal serial number\n");
return NULL;
}
while (i--)
p = p->next;
if (p)
return p;
printf("Error:error in linked list\n");
return NULL;
}
若头结点的前驱与后继相等,则说明表空。
4.按值查找
DNode* LocateElem(LinkList L) //按值查找
{
int value = 0;
if (L->next == L->prior)
{
printf("Error:Tabular void\n");
return NULL;
}
LinkList p = L->next;
printf("Please enter the value:");
scanf_s("%d", &value);
while (p != L)
{
if (value == p->data)
{
printf("Success!\n");
return p;
}
p = p->next;
}
printf("Defeat:target value not found\n");
return NULL;
}
因为该结构是循环结构,当指针p再次指向头结点时表示链表循环一周。
4.插入
bool Input(LinkList& L) //插入
{
int i = 0;
LinkList n = L;
LinkList p = (DNode*)malloc(sizeof(DNode));
if (!p) {
printf("Error:Insufficient memory!\n");
return false;
}
printf("Please enter the number:");
scanf_s("%d", &i);
if ((i <= 0) || (i > L->data+1))
{
printf("Error:lllegal serial number\n");
return false;
}
while (i--)
n = n->next;
printf("Please enter the value:");
scanf_s("%d", &(p->data));
p->next = n;
p->prior = n->prior;
n->prior->next = p;
n->prior = p;
L->data++;
return true;
}
示意图:
2、3必须在4的前面
5.删除
bool Delete(LinkList& L) //删除
{
int i = 0;
LinkList n = L;
printf("Please enter the number:");
scanf_s("%d", &i);
if (L->next == L->prior) {
printf("Error:Tabular void\n");
return false;
}
if ((i <= 0 ) || (i > L->data)) {
printf("Error:lllegal serial number\n");
return false;
}
while (i--)
n = n->next;
n->prior->next = n->next;
n->next->prior = n->prior;
free(n);
L->data--;
return true;
}
示意图:
三、代码实现
#include "stdio.h"
#include"stdlib.h" //循环双链表
typedef int ElemType;
typedef struct DNode{
ElemType data;
struct DNode* next, * prior;
}DNode,*LinkList;
void Initial(LinkList& L) { //初始化
L = (DNode*)malloc(sizeof(DNode));
if (L)
{
L->next = L;
L->prior = L;
L->data = 0;
printf("Initialization succeeded!\n");
}
else
printf("Error:Insufficient memory!\n");
}
DNode* GetElem(LinkList L) //按序查找
{
int i = 0;
LinkList p = L;
if (L->next == L->prior)
{
printf("Error:Tabular void\n");
return NULL;
}
printf("Please enter the number:");
scanf_s("%d", &i);
if ((i <= 0) || (i > L->data))
{
printf("Error:lllegal serial number\n");
return NULL;
}
while (i--)
p = p->next;
if (p)
return p;
printf("Error:error in linked list\n");
return NULL;
}
DNode* LocateElem(LinkList L) //按值查找
{
int value = 0;
if (L->next == L->prior)
{
printf("Error:Tabular void\n");
return NULL;
}
LinkList p = L->next;
printf("Please enter the value:");
scanf_s("%d", &value);
while (p != L)
{
if (value == p->data)
{
printf("Success!\n");
return p;
}
p = p->next;
}
printf("Defeat:target value not found\n");
return NULL;
}
bool Input(LinkList& L) //插入
{
int i = 0;
LinkList n = L;
LinkList p = (DNode*)malloc(sizeof(DNode));
if (!p) {
printf("Error:Insufficient memory!\n");
return false;
}
printf("Please enter the number:");
scanf_s("%d", &i);
if ((i <= 0) || (i > L->data+1))
{
printf("Error:lllegal serial number\n");
return false;
}
while (i--)
n = n->next;
printf("Please enter the value:");
scanf_s("%d", &(p->data));
p->next = n;
p->prior = n->prior;
n->prior->next = p;
n->prior = p;
L->data++;
return true;
}
bool Delete(LinkList& L) //删除
{
int i = 0;
LinkList n = L;
printf("Please enter the number:");
scanf_s("%d", &i);
if (L->next == L->prior) {
printf("Error:Tabular void\n");
return false;
}
if ((i <= 0 ) || (i > L->data)) {
printf("Error:lllegal serial number\n");
return false;
}
while (i--)
n = n->next;
n->prior->next = n->next;
n->next->prior = n->prior;
free(n);
L->data--;
return true;
}
bool Output(LinkList& L) //输出
{
if (L->next == L->prior)
{
printf("Error:Tabular void\n");
return false;
}
int i = 1;
LinkList p = L->next;
printf("[Table length]%d\n", L->data);
while (p != L)
{
printf("[%d]%d\n", i++, p->data);
p = p->next;
}
return true;
}
bool Air(LinkList& L) //清空
{
if (L->next == L->prior)
{
printf("Error:Tabular void\n");
return false;
}
LinkList p = L->next, i = NULL;
while (p != L)
{
i = p;
p = p->next;
free(i);
}
L->data = 0;
L->next = L;
L->prior = L;
return true;
}
bool Input_Continuously(LinkList& L) { //连续写入
int i = 0;
LinkList n = L,p = NULL;
printf("Please enter the number of inserts:");
scanf_s("%d", &i);
if (i <= 0) {
printf("Error:lllegal serial number\n");
return false;
}
while (i--)
{
p = (DNode*)malloc(sizeof(DNode));
if (!p) {
printf("Error:Insufficient memory!\n");
return false;
}
printf("Please enter the value:");
scanf_s("%d", &(p->data));
p->next = n;
p->prior = n->prior;
n->prior->next = p;
n->prior = p;
L->data++;
}
return true;
}
int main() {
LinkList L = NULL;
bool SWITCH = true;
DNode* GetElem_value = NULL, * LocateElem_value = NULL;
bool (*f)(LinkList & L) = NULL;
int CHOOSE = 0;
Initial(L);
do
{
printf("Operate:\n1.连续插入\t2.插入\t3.删除\t4.输出\t5.清空\t6.按序查找\t7.按值查找\t8.退出\n");
scanf_s("%d", &CHOOSE);
switch (CHOOSE)
{
case 1:
f = Input_Continuously;
break;
case 2:
f = Input;
break;
case 3:
f = Delete;
break;
case 4:
f = Output;
break;
case 5:
f = Air;
break;
case 6:
f = NULL;
if(GetElem_value = GetElem(L))
printf("The value is %d\n", GetElem_value->data);
break;
case 7:
f = NULL;
if(LocateElem_value = LocateElem(L))
printf("The value is %d\n", LocateElem_value->data);
break;
case 8:
SWITCH = false;
break;
default:
printf("Operate not found\n");
break;
}
if (f)
{
if (f(L))
printf("Successful execution!\n");
else
printf("Execution failed!\n");
}
} while (SWITCH);
return 0;
}