【4】循环链表与约瑟夫问题

循环链表与约瑟夫问题

循环链表:

1、初始化时,头节点指向本身
2、遍历终止条件为达到链表的size

数据结构

//链表的小结点
typedef struct CIRCLELINKNODE{
	struct CIRCLELINKNODE* next;
}CircleLinkNode;

//链表结构体
typedef struct CIRCLELINKLIST{
	CircleLinkNode head;
	int size;
}CircleLinkList;

//编写针对链表结构体操作的API函数

#define CIRCLELINKLIST_TRUE 1
#define CIRCLELINKLIST_FALSE 0

//比较回调
typedef int(*COMPARENODE)(CircleLinkNode*, CircleLinkNode*);
//打印回调
typedef void(*PRINTNODE)(CircleLinkNode*);

1、初始化

//初始化函数
CircleLinkList* Init_CircleLinkList(){

	CircleLinkList* clist = (CircleLinkList*)malloc(sizeof(CircleLinkList));
	clist->head.next = &(clist->head);
	clist->size = 0;

	return clist;
}

2、插入

//插入函数
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 ++;
}

3、获取第一个元素

CircleLinkNode* Front_CircleLinkList(CircleLinkList* clist){
	return clist->head.next;
}

4、删除(根据位置)

//根据位置删除
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--;
}

5、删除(根据值)

//根据值去删除
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) == CIRCLELINKLIST_TRUE){
			pPrev->next = pCurrent->next;
			clist->size--;
			break;
		}
		pPrev = pCurrent;
		pCurrent = pPrev->next;
	}
}

6、获取当前大小

//获得链表的长度
int Size_CircleLinkList(CircleLinkList* clist){
	return clist->size;
}

7、判断是否为空

//判断是否为空
int IsEmpty_CircleLinkList(CircleLinkList* clist){
	if (clist->size == 0){
		return CIRCLELINKLIST_TRUE;
	}
	return CIRCLELINKLIST_FALSE;
}

8、查找

//查找
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;
}

9、打印所有节点

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;
			printf("------------------\n");
		}
		print(pCurrent);
		pCurrent = pCurrent->next;
	}

}

10、释放内存

//释放内存
void FreeSpace_CircleLinkList(CircleLinkList* clist){

	if (clist == NULL){
		return;
	}
	free(clist);
}

约瑟夫问题

约瑟夫问题是个有名的问题:N个人围成一圈,从第一个开始报数,第M个将被杀掉,最后剩下一个,其余人都将被杀掉。例如N=6,M=5,被杀掉的顺序是:5,4,6,2,3,1。

c代码

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include "CircleLinkList.h"

#define M 8//定义m=8
#define N 3//n=3
//8个人围成一圈,报数为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 CIRCLELINKLIST_FALSE;
}

int main(void){

	//创建循环链表
	CircleLinkList* clist = Init_CircleLinkList();
	//链表插入数据
	MyNum num[M];
	for (int i = 0; i < 8;i ++){
		num[i].val = i + 1;
		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 = pNext;
			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("出错!");
	}

	printf("\n");

	//释放链表内存
	FreeSpace_CircleLinkList(clist);
	



	system("pause");
	return 0;
}

你可能感兴趣的:(数据结构,链表)