《C语言》环形双链表增、删、查、改、销毁
- Main.c
- A_DoubleList.h
- A_DoubleList.c
Main.c
#include "A_DoubleList.h"
#include
#define N 10
void main()
{
#if 0
A_DoubleList Head;
InitList(&Head);
for (int i = 0; i < 1; i++)
{
PushBack(&Head,i);
}
puts("正向打印:");
Show(&Head);
puts("\n反向打印:");
ReverseShow(&Head);
#endif
#if 0
A_DoubleList Head;
InitList(&Head);
for (int i = 0; i < N; i++)
{
PushHead(&Head, i);
}
puts("正向打印:");
Show(&Head);
puts("\n反向打印:");
ReverseShow(&Head);
#endif
#if 0
A_DoubleList Head;
InitList(&Head);
for (int i = 0; i < N; i++)
{
PushBack(&Head, i);
}
puts("正向打印:");
Show(&Head);
puts("\n查找结果:");
Node* P_Res = FindNode(&Head, 0);
NULL != P_Res ? printf("%d\n",P_Res->Data): puts("Can't find.");
P_Res = FindNode(&Head, 5);
NULL != P_Res ? printf("%d\n", P_Res->Data) : puts("Can't find.");
P_Res = FindNode(&Head, 9);
NULL != P_Res ? printf("%d\n", P_Res->Data) : puts("Can't find.");
P_Res = FindNode(&Head, 2018);
NULL != P_Res ? printf("%d\n", P_Res->Data) : puts("Can't find.");
#endif
#if 0
A_DoubleList Head;
InitList(&Head);
for (int i = 0; i < N; i++)
{
PushBack(&Head, i);
}
puts("正向打印:\n");
Show(&Head);
InsertNode(&Head, 0, 2018);
InsertNode(&Head, 5, 2018);
InsertNode(&Head, 9, 2018);
puts("插入数据之后:");
Show(&Head);
#endif
#if 0
A_DoubleList Head;
InitList(&Head);
for (int i = 0; i < 10; i++)
{
PushBack(&Head, i);
}
puts("正向打印:\n");
Show(&Head);
Modification(&Head, 0, 2018);
Modification(&Head, 5, 2018);
Modification(&Head, 9, 2018);
Show(&Head);
#endif
#if 0
A_DoubleList Head;
InitList(&Head);
for (int i = 0; i < 10; i++)
{
PushBack(&Head, i);
}
puts("正向打印:\n");
Show(&Head);
DeleteNode(&Head, 0);
DeleteNode(&Head, 5);
DeleteNode(&Head, 9);
puts("节点删除之后:\n");
Show(&Head);
#endif
#if 0
A_DoubleList Head;
InitList(&Head);
for (int i = 0; i < 10; i++)
{
PushBack(&Head, i);
}
puts("正向打印:\n");
Show(&Head);
Destory(&Head);
puts("链表销毁\n");
Show(&Head);
#endif
system("pause");
}
A_DoubleList.h
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include
#include
typedef int DataType;
typedef struct Node
{
DataType Data;
struct Node* P_Pre;
struct Node* P_Next;
}Node;
typedef struct A_DoubleList
{
Node* P_Head;
Node* P_Tail;
}A_DoubleList;
void InitList(A_DoubleList* P_List);
void InitNode(Node* P_Node,DataType Data);
void Show(A_DoubleList* P_List);
void ReverseShow(A_DoubleList* P_List);
void PushBack(A_DoubleList* P_List, DataType Data);
void PushHead(A_DoubleList* P_List, DataType Data);
Node* FindNode(A_DoubleList* P_List, DataType Data);
void InsertNode(A_DoubleList* P_List, DataType Rear,DataType InsData);
void Modification(A_DoubleList* P_List, DataType OldData, DataType NewData);
void DeleteNode(A_DoubleList* P_List, DataType DelData);
void Destory(A_DoubleList* P_List);
#ifdef __cplusplus
}
#endif
A_DoubleList.c
#include "A_DoubleList.h"
void InitList(A_DoubleList* P_List)
{
P_List->P_Head = P_List->P_Tail = NULL;
}
void InitNode(Node* P_Node, DataType Data)
{
P_Node->P_Pre = P_Node->P_Next = NULL;
P_Node->Data = Data;
}
void Show(A_DoubleList* P_List)
{
if (NULL ==P_List->P_Head&&NULL==P_List->P_Tail)
{
return;
}
else if (P_List->P_Head == P_List->P_Tail)
{
printf("%p\t%p\t%p\t%d\n", P_List->P_Head,P_List->P_Head->P_Pre,P_List->P_Head->P_Next,P_List->P_Head->Data);
}
else
{
Node* P_Bak =P_List->P_Head;
while (P_Bak!= P_List->P_Tail)
{
printf("%p\t%p\t%p\t%d\n", P_Bak, P_Bak->P_Pre, P_Bak->P_Next, P_Bak->Data);
P_Bak = P_Bak->P_Next;
}
printf("%p\t%p\t%p\t%d\n", P_Bak, P_Bak->P_Pre, P_Bak->P_Next, P_Bak->Data);
}
puts("");
}
void ReverseShow(A_DoubleList* P_List)
{
if (NULL ==P_List->P_Head&&NULL == P_List->P_Tail)
{
return;
}
else if (P_List->P_Head == P_List->P_Tail)
{
printf("%p\t%p\t%p\t%d\n", P_List->P_Head, P_List->P_Head->P_Pre, P_List->P_Head->P_Next, P_List->P_Head->Data);
}
else
{
Node* P_Bak = P_List->P_Tail;
while (P_Bak!=P_List->P_Head)
{
printf("%p\t%p\t%p\t%d\n", P_Bak,P_Bak->P_Pre, P_Bak->P_Next, P_Bak->Data);
P_Bak = P_Bak->P_Pre;
}
printf("%p\t%p\t%p\t%d\n", P_Bak, P_Bak->P_Pre, P_Bak->P_Next, P_Bak->Data);
}
}
void PushBack(A_DoubleList* P_List, DataType Data)
{
Node* P_New = (Node*)malloc(sizeof(Node));
InitNode(P_New, Data);
if (P_List->P_Head == NULL && P_List->P_Tail == NULL)
{
P_List->P_Head=P_List->P_Tail=P_New;
P_New->P_Next = P_New->P_Pre = P_New;
}
else
{
if (P_List->P_Head == P_List->P_Tail)
{
P_List->P_Head->P_Pre = P_List->P_Head->P_Next = P_New;
P_New->P_Pre = P_New->P_Next = P_List->P_Head;
P_List->P_Tail = P_New;
}
else
{
P_List->P_Tail->P_Next = P_New;
P_New->P_Pre = P_List->P_Tail;
P_New->P_Next = P_List->P_Head;
P_List->P_Tail = P_New;
P_List->P_Head->P_Pre = P_List->P_Tail;
}
}
}
void PushHead(A_DoubleList* P_List, DataType Data)
{
Node* P_New = (Node*)malloc(sizeof(Node));
InitNode(P_New, Data);
if (P_List->P_Head == NULL && P_List->P_Tail == NULL)
{
P_List->P_Head = P_List->P_Tail = P_New;
P_New->P_Next = P_New->P_Pre = P_New;
}
else
{
P_New->P_Next =P_List->P_Head;
P_New->P_Pre = P_List->P_Tail;
P_List->P_Head->P_Pre = P_New;
P_List->P_Head = P_New;
P_List->P_Tail->P_Next =P_List->P_Head;
}
}
Node* FindNode(A_DoubleList* P_List, DataType Data)
{
if (NULL == P_List->P_Head&&NULL == P_List->P_Tail)
{
return NULL;
}
Node* P_Bak =P_List->P_Head;
while (P_Bak->P_Next!=P_List->P_Head)
{
if (P_Bak->Data == Data)
{
return P_Bak;
}
P_Bak = P_Bak->P_Next;
}
return P_Bak->Data == Data ? P_Bak :NULL;
}
void InsertNode(A_DoubleList* P_List, DataType Rear, DataType InsData)
{
Node* P_Res = FindNode(P_List, Rear);
if (NULL == P_Res)
{
return;
}
else
{
Node* P_New = (Node*)malloc(sizeof(Node));
InitNode(P_New, InsData);
if (P_Res == P_List->P_Head)
{
if (P_List->P_Head == P_List->P_Tail)
{
P_List->P_Head->P_Pre = P_List->P_Head->P_Next = P_New;
P_New->P_Pre = P_New->P_Next = P_List->P_Head;
P_List->P_Tail = P_New;
}
else
{
P_New->P_Pre = P_List->P_Head;
P_New->P_Next = P_List->P_Head->P_Next;
P_List->P_Head->P_Next->P_Pre = P_New;
P_List->P_Head->P_Next = P_New;
}
}
else if (P_Res != P_List->P_Head&&P_Res != P_List->P_Tail)
{
P_New->P_Pre = P_Res;
P_New->P_Next = P_Res->P_Next;
P_Res->P_Next->P_Pre= P_Res->P_Next = P_New;
}
else if(P_Res==P_List->P_Tail)
{
P_New->P_Pre = P_List->P_Tail;
P_New->P_Next = P_List->P_Tail->P_Next;
P_List->P_Tail->P_Next = P_New;
P_List->P_Tail = P_New;
}
}
}
void Modification(A_DoubleList* P_List, DataType OldData, DataType NewData)
{
Node* P_Res = FindNode(P_List, OldData);
if (NULL == P_Res)
{
return;
}
else
{
P_Res->Data = NewData;
}
}
void DeleteNode(A_DoubleList* P_List, DataType DelData)
{
Node* P_Res = FindNode(P_List, DelData);
if (NULL == P_Res)
{
return;
}
else
{
if (P_Res == P_List->P_Head)
{
if (P_List->P_Head == P_List->P_Tail)
{
P_List->P_Head->P_Next = P_List->P_Head->P_Pre = NULL;
P_List->P_Head->Data = 0;
P_List->P_Head = P_List->P_Tail = NULL;
}
else
{
P_List->P_Head = P_List->P_Head->P_Next;
P_List->P_Head->P_Pre = P_List->P_Tail;
P_List->P_Tail->P_Next = P_List->P_Head;
}
}
else
{
if (P_Res != P_List->P_Head&&P_Res != P_List->P_Tail)
{
P_Res->P_Pre->P_Next = P_Res->P_Next;
P_Res->P_Next->P_Pre = P_Res->P_Pre;
}
else
{
if (P_List->P_Head->P_Next == P_List->P_Head->P_Pre&&P_List->P_Tail->P_Next == P_List->P_Tail->P_Pre)
{
P_List->P_Tail = P_List->P_Tail->P_Pre;
}
else
{
P_List->P_Tail = P_List->P_Tail->P_Pre;
P_List->P_Tail->P_Next = P_List->P_Head;
P_List->P_Head->P_Pre = P_List->P_Tail;
}
}
}
P_Res->Data = 0;
P_Res->P_Next = P_Res->P_Pre = NULL;
free(P_Res);
P_Res = NULL;
}
}
void Destory(A_DoubleList* P_List)
{
while (NULL != P_List->P_Tail)
{
DeleteNode(P_List, P_List->P_Head->Data);
}
}