1.单向环形链表
2.代码的简单示例
CircleLinkList.h
CircleLinkList.c
main.c
3. CircleLinkList.h
/*
文件名:CircleLinkList.h
作者:Muten
编码时间:20201022
编码目的:循环链表及其相关操作的声明
代码功能:声明循环链表及其相关操作
测试环境:Mircosoft Visual Studio Professional 2015 版本14.0.25431.01 Update3
版本信息:VER1.0
*/
#pragma once
#ifndef CIRCLELINKLIST
#define CIRCLELINKLIST
// 链表的小节点
typedef struct CIRCLELINKNODE {
struct CIRCLELINKNODE *next;
}CircleLinkNode;
// 链表结构体
// 这里也可以维护尾结点,这样可以快速找到尾部数据
typedef struct CIRCLIELINKLIST {
CircleLinkNode head;
int size;
}CircleLinkList;
// 编写针对链表结构体操作的API函数
// 比较回调
typedef int(*COMPARENODE)(CircleLinkNode*, CircleLinkNode*);
// 打印回调
typedef int(*PRINTNODE)(CircleLinkNode*);
#define NODESAME_TRUE 1
#define NODESAME_FALSE 0
#define CLISTEMPTY_TRUE 1
#define CLISTEMPTY_FALSE 1
// 初始化函数
CircleLinkList* Init_CircleLinkList();
// 插入函数
void Insert_CircleLinkList(CircleLinkList* clist,int pos, CircleLinkNode* data);
// 获得第一个元素
CircleLinkNode* Front_CircleLinkList(CircleLinkList* clist);
// 根据位置删除
void RemoveByPos_CircleLinkList(CircleLinkList* clist,int pos);
// 根据值去删除
void RemoveByValue_CircleLinkList(CircleLinkList* clist, CircleLinkNode* data,COMPARENODE compare);
// 获得链表的长度
int Size_CircleLinkList(CircleLinkList* clist);
// 判断是否为空
int IsEmpty_CircleLinkList(CircleLinkList* clist);
// 查找
int Find_CircleLinkList(CircleLinkList* clist, CircleLinkNode* data, COMPARENODE compare);
// 打印
void Print_CircleLinkList(CircleLinkList* clist,PRINTNODE print);
// 释放内存
void FreeSpace_CircleLinkList(CircleLinkList* clist);
#endif // !CIRCLELINKLIST
4.CircleLinkList.c
/*
文件名:CircleLinkList.c
作者:Muten
编码时间:20201022
编码目的:循环链表及其相关操作的操作
代码功能:循环链表及其相关操作的操作
测试环境:Mircosoft Visual Studio Professional 2015 版本14.0.25431.01 Update3
版本信息:VER1.0
*/
#include "CircleLinkList.h"
#include <stdio.h>
#include <stdlib.h>
CircleLinkList * Init_CircleLinkList()
{
CircleLinkList* clist = (CircleLinkList*)malloc(sizeof(CircleLinkList));
clist->head.next = &(clist->head);
clist->size = 0;
return clist;
}
void Insert_CircleLinkList(CircleLinkList * clist, int pos, CircleLinkNode * data)
{
if (clist == NULL || data == NULL)
return;
if (pos < 0 || pos >clist->size)
pos = clist->size;
// 根据位置查找节点
// 辅助指针变量
CircleLinkNode* pCurrent = &(clist->head);
for (int i = 0; i < pos; i++)
pCurrent = pCurrent->next;
//新数据入链表
data->next = pCurrent->next;
pCurrent->next = data;
clist->size++;
}
CircleLinkNode * Front_CircleLinkList(CircleLinkList * clist)
{
return clist->head.next;
}
void RemoveByPos_CircleLinkList(CircleLinkList * clist, int pos)
{
if (clist == NULL)
return;
if (pos < 0 || pos >= clist->size)
return;
// 根据pos找节点
// 辅助指针变量
CircleLinkNode* pCurrent = &(clist->head);
for (int i = 0; i < pos; i++)
{
pCurrent = pCurrent->next;
}
// 缓存当前节点的下一个基点
CircleLinkNode* pNext = pCurrent->next;
pCurrent->next = pNext->next;
clist->size--;
}
void RemoveByValue_CircleLinkList(CircleLinkList * clist, CircleLinkNode * data, COMPARENODE compare)
{
if (clist == NULL)
return;
if (data == NULL)
return;
// 这个是循环链表
CircleLinkNode* pPrev = &(clist->head);
CircleLinkNode* pCurrent = pPrev->next;
int i = 0;
for (i = 0; i < clist->size; i++)
{
if (compare(pCurrent, data) == NODESAME_TRUE)
{
pPrev->next = pCurrent->next;
clist->size--;
break;
}
pPrev = pCurrent;
pCurrent = pPrev->next;
}
}
int Size_CircleLinkList(CircleLinkList * clist)
{
return clist->size;
}
int IsEmpty_CircleLinkList(CircleLinkList * clist)
{
if (clist->size == 0)
return CLISTEMPTY_TRUE;
return CLISTEMPTY_FALSE;
}
int Find_CircleLinkList(CircleLinkList * clist, CircleLinkNode * data, COMPARENODE compare)
{
if (clist == NULL)
return -1;
if (data == NULL)
return -1;
CircleLinkNode* pCurrent = clist->head.next;
int i = 0;
int index = -1;
for (i = 0; i < clist->size; i++)
{
if (compare(pCurrent, data) == NODESAME_TRUE)
{
index = i;
break;
}
pCurrent = pCurrent->next;
}
return index;
}
void Print_CircleLinkList(CircleLinkList * clist, PRINTNODE print)
{
if (clist == NULL)
return;
// 辅助指针变量
CircleLinkNode* pCurrent = clist->head.next;
int i = 0;
for (i= 0; i < clist->size; i++)
{
if (pCurrent == &(clist->head))
{
pCurrent = pCurrent->next;
}
print(pCurrent);
pCurrent = pCurrent->next;
}
}
void FreeSpace_CircleLinkList(CircleLinkList * clist)
{
if (clist == NULL)
return;
free(clist);
}
5.
/*
文件名:main.c
作者:Muten
编码时间:20201022
编码目的:演示简单测试循环链表相关代码
代码功能:简单测试循环链表相关代码
测试环境:Mircosoft Visual Studio Professional 2015 版本14.0.25431.01 Update3
版本信息:VER1.0
*/
#pragma warning(disable:4996)
#include "CircleLinkList.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct PERSON
{
CircleLinkNode node;
char name[64];
int age;
int score;
}Person;
void MyPrint(CircleLinkNode* data)
{
Person* p = (Person*)data;
printf("name = %s,Age = %d,Score = %d.\n",p->name,p->age,p->score);
}
int MyCompare(CircleLinkNode* data1, CircleLinkNode* data2)
{
Person* p1 = (Person*)data1;
Person* p2 = (Person*)data2;
if (strcmp(p1->name, p2->name) == 0 && p1->age == p2->age && p1->score == p2->score)
{
return NODESAME_TRUE;
}
return NODESAME_FALSE;
}
int main()
{
CircleLinkList* clist = Init_CircleLinkList();
Person p1,p2,p3;
strcpy_s(p1.name,6,"AAAAA");
strcpy_s(p2.name,6, "BBBBB");
strcpy_s(p3.name,6, "CCCCC");
p1.age = 10;
p2.age = 20;
p3.age = 30;
p1.score = 65;
p2.score = 63;
p3.score = 66;
Insert_CircleLinkList(clist,100,(CircleLinkNode*)&p1);
Insert_CircleLinkList(clist,100, (CircleLinkNode*)&p2);
Insert_CircleLinkList(clist,100, (CircleLinkNode*)&p3);
Print_CircleLinkList(clist, MyPrint);
Person pDel;
strcpy_s(pDel.name,6,"BBBBB");
pDel.age = 20;
pDel.score = 63;
RemoveByValue_CircleLinkList(clist,(CircleLinkList*)&pDel, MyCompare);
printf("删除第二个元素后:\n");
Print_CircleLinkList(clist,MyPrint);
printf("sizeof(clist) = %d, clist->size = %d.\n", sizeof(clist), clist->size);
}