循环链表之约瑟夫问题

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


//API函数

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. 主函数

// 约瑟夫.cpp : 此文件包含 "main" 函数。程序执行将在此处开始

#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);
			//printf("\n");
			//printf("%d ", Size_CircleLinkList(clist));
			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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值