1. 头文件
#pragma once
#ifndef CIRCLELINKLIST
#define CIRCLELINKLIST
#include <stdio.h>
#include <stdlib.h>
typedef struct CIRCLELINKNODE {
struct CIRCLELINKNODE* next;
}CircleLinkNode;
typedef struct CIRCLELINKLIST {
CircleLinkNode head;
int size;
}CircleLinkList;
#define CIRCLELINKLIST_TRUE 1
#define CIRCLELINKI7IST_FALSE 0
typedef int(*COMPARENODE)(CircleLinkNode*, CircleLinkNode*);
typedef void(*PRINTNODE)(CircleLinkNode*);
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
2. 函数实现
#include "CircleLinkList.h"
#include <stdlib.h>
#include <stdio.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;
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;
}
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 = clist->head.next;
int i = 0;
int flag = -1;
for (i = 0; i < clist->size; i++)
{
if (compare(pCurrent, data) == CIRCLELINKLIST_TRUE)
{
pPrev->next = pCurrent->next;
break;
}
pPrev = pCurrent;
pCurrent = pCurrent->next;
}
clist->size--;
}
int Size_CircleLinkList(CircleLinkList* clist) {
return clist->size;
}
int IsEmpty_CircleLinkList(CircleLinkList* clist) {
if (clist->size == 0) {
return CIRCLELINKLIST_TRUE;
}
return CIRCLELINKI7IST_FALSE;
return 0;
}
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;
for (int i = 0; i < clist->size; i++) {
if (compare(pCurrent, data) == CIRCLELINKLIST_TRUE) {
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);
}
3. 主函数
#define _CRr_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include"CircleLinklist.h"
#define M 8
#define N 3
typedef struct MYNUM {
CircleLinkNode node;
int val;
}MyNum;
void MyPrint(CircleLinkNode* data) {
MyNum* num = (MyNum*)data;
printf("%d ", num->val);
}
int MyCompare(CircleLinkNode* data1, CircleLinkNode* data2)
{
MyNum* num1 = (MyNum*)data1;
MyNum* num2 = (MyNum*)data2;
if (num1->val == num2->val)
{
return CIRCLELINKLIST_TRUE;
}
return CIRCLELINKI7IST_FALSE;
}
int main()
{
CircleLinkList* clist = Init_CircleLinkList();
MyNum num[M];
for (int i = 0; i < 8; i++)
{
num[i].val = 1 + i;
Insert_CircleLinkList(clist, i, (CircleLinkNode*)&num[i]);
}
Print_CircleLinkList(clist, MyPrint);
printf("\n");
int index = 1;
CircleLinkNode* pCurrent = clist->head.next;
while (Size_CircleLinkList(clist) > 1) {
if (index == N) {
MyNum* temNum = (MyNum*)pCurrent;
printf("%d ", temNum->val);
CircleLinkNode* pNext = pCurrent->next;
RemoveByValue_CircleLinklist(clist,pCurrent,MyCompare);
pCurrent = pCurrent->next;
if (pCurrent == &(clist->head)) {
pCurrent = pCurrent->next;
}
index = 1;
}
pCurrent = pCurrent->next;
if (pCurrent == &(clist->head)) {
pCurrent = pCurrent->next;
}
index++;
}
if (Size_CircleLinkList(clist) == 1) {
MyNum* tempNum = (MyNum*)Front_CircleLinkList(clist);
printf("%d ", tempNum->val);
}
else
printf("error");
FreeSpace_CircleLinkList(clist);
system("pause");
return 0;
}