单向循环链表:尾指针指向头结点。
下面是单向循环链表的基本操作函数编写:
创建CirleLinkList.h 和 CirleLinkList.c 和 循环链表.c
CirleLinkList.h
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include"CircleLinkList.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 CIRCLELINKLIST_TURE;
}
return CIRCLELINKLIST_FALSE;
}
int main(void)
{
//创建单向循环链表
CircleLinkList* clist = Init_CircleLinkList();
//创建数据
Person p1, p2, p3, p4, p5;
strcpy(p1.name, "aa");
strcpy(p2.name, "bb");
strcpy(p3.name, "cc");
strcpy(p4.name, "dd");
strcpy(p5.name, "ee");
p1.age = 10;
p2.age = 20;
p3.age = 30;
p4.age = 40;
p5.age = 50;
p1.score = 50;
p2.score = 60;
p3.score = 70;
p4.score = 80;
p5.score = 90;
//数据入链表
Insert_CircleLinkList(clist, 100, (CircleLinkNode*)&p1);
Insert_CircleLinkList(clist, 100, (CircleLinkNode*)&p2);
Insert_CircleLinkList(clist, 100, (CircleLinkNode*)&p3);
Insert_CircleLinkList(clist, 100, (CircleLinkNode*)&p4);
Insert_CircleLinkList(clist, 100, (CircleLinkNode*)&p5);
//打印
Print_CircleLinkList(clist, MyPrint);
Person pDel;
strcpy(pDel.name, "cc");
pDel.age = 30;
pDel.score = 70;
//根据值删除
RemoveByValue_CircleLinkList(clist, (CircleLinkNode*) & pDel, MyCompare);
//打印
printf("---------------------------------------\n");
Print_CircleLinkList(clist, MyPrint);
//释放内存
FreeSpace_CircleLinkList(clist);
printf("\n");
system("pause");
return 0;
}
CirleLinkList.c
#include "CircleLinkList.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) {
return;
}
if (data == NULL) {
return;
}
//友好处理,pos越界
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++;
}
//获得第一个元素
CircleLinkList* 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;
}
//缓存当前结点的下一个结点(删除的是pNext这个结点)
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 = clist->head.next;
int i = 0;
for (i = 0; i < clist->size; i++)
{
if (compare(pCurrent,data) == CIRCLELINKLIST_TURE)
{
pPrev->next = pCurrent->next;
break;
}
pPrev = pCurrent;
pCurrent = pPrev->next;
}
}
//获取链表长度
int Size_CircleLinkList(CircleLinkList* clist)
{
return clist->size;
}
//判断是否为空
int IsEmpty_CircleLinkList(CircleLinkList* clist)
{
if (clist == NULL) {
return CIRCLELINKLIST_TURE;
}
return CIRCLELINKLIST_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 flag = -1;
int i = 0;
for (i = 0; i < clist->size; i++)
{
if (compare(pCurrent, data) == CIRCLELINKLIST_TURE)
{
flag = i;
break;
}
pCurrent = pCurrent->next;
}
return flag;
}
//打印结点
void Print_CircleLinkList(CircleLinkList* clist, PRINTNODE print)
{
if (clist == NULL) {
return;
}
//辅助指针遍历
CircleLinkNode* pCurrent = clist->head.next;
for (int 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);
}
循环链表.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include"CircleLinkList.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 CIRCLELINKLIST_TURE;
}
return CIRCLELINKLIST_FALSE;
}
int main(void)
{
//创建单向循环链表
CircleLinkList* clist = Init_CircleLinkList();
//创建数据
Person p1, p2, p3, p4, p5;
strcpy(p1.name, "aa");
strcpy(p2.name, "bb");
strcpy(p3.name, "cc");
strcpy(p4.name, "dd");
strcpy(p5.name, "ee");
p1.age = 10;
p2.age = 20;
p3.age = 30;
p4.age = 40;
p5.age = 50;
p1.score = 50;
p2.score = 60;
p3.score = 70;
p4.score = 80;
p5.score = 90;
//数据入链表
Insert_CircleLinkList(clist, 100, (CircleLinkNode*)&p1);
Insert_CircleLinkList(clist, 100, (CircleLinkNode*)&p2);
Insert_CircleLinkList(clist, 100, (CircleLinkNode*)&p3);
Insert_CircleLinkList(clist, 100, (CircleLinkNode*)&p4);
Insert_CircleLinkList(clist, 100, (CircleLinkNode*)&p5);
//打印
Print_CircleLinkList(clist, MyPrint);
Person pDel;
strcpy(pDel.name, "cc");
pDel.age = 30;
pDel.score = 70;
//根据值删除
RemoveByValue_CircleLinkList(clist, (CircleLinkNode*) & pDel, MyCompare);
//打印
printf("---------------------------------------\n");
Print_CircleLinkList(clist, MyPrint);
//释放内存
FreeSpace_CircleLinkList(clist);
printf("\n");
system("pause");
return 0;
}