#ifndef _CIRCLELIST_H_ #define _CIRCLELIST_H_ typedef void CircleList; typedef struct _tag_CircleListNode CircleListNode; struct _tag_CircleListNode { CircleListNode* next; }; CircleList* CircleList_Create(); void CircleList_Destroy(CircleList* list); void CircleList_Clear(CircleList* list); int CircleList_Empty(CircleList* list); int CircleList_Length(CircleList* list); int CircleList_Insert(CircleList* list, CircleListNode* node, int pos); CircleListNode* CircleList_Get(CircleList* list, int pos); CircleListNode* CircleList_Delete(CircleList* list, int pos); CircleListNode* CircleList_DeleteNode(CircleList* list,CircleListNode* node); CircleListNode* CircleList_Reset(CircleList* list); CircleListNode* CircleList_Current(CircleList* list); CircleListNode* CircleList_Next(CircleList* list); #endif #include <stdio h=""> #include <malloc h=""> #include "CircleList.h" typedef struct _tag_CircleList { CircleListNode header; int length; CircleListNode* slider; //游标 } TCircleList; CircleList* CircleList_Create() { TCircleList* ret = (TCircleList*)malloc(sizeof(TCircleList)); if(ret != NULL) { ret->header.next = NULL; ret->length = 0; ret->slider = NULL; } return ret; } void CircleList_Destroy(CircleList* list) { free(list); } void CircleList_Clear(CircleList* list) { TCircleList* slist = (TCircleList*)list; if(slist != NULL) { slist->header.next = NULL; slist->length = 0; slist->slider = NULL; } } int CircleList_Empty(CircleList* list) { TCircleList* slist = (TCircleList*)list; int ret = 0; if((slist != NULL) && (slist->length == 0) && (slist->slider == NULL)) { ret = 1; } return ret; } int CircleList_Length(CircleList* list) { TCircleList* slist = (TCircleList*)list; int ret = -1; if(slist != NULL) { ret = slist->length; } return ret; } int CircleList_Insert(CircleList* list, CircleListNode* node, int pos) { TCircleList* slist = (TCircleList*)list; int i = 0; int ret = (slist != NULL) && (0 <= pos); if(ret) { CircleListNode* current = (CircleListNode*)slist; for(i=0;(i < pos) && (current->next != NULL);i++) { current = current->next; } node->next = current->next; current->next = node; //判断是否是在空的时候插入节点 if(slist->length == 0) { slist->slider = node; } slist->length++; //修正第一个元素 和最后一个元素 比slist->length ==0 强 if(current == (CircleListNode*)slist) { CircleListNode* last = CircleList_Get(list,slist->length-1); last->next = current->next; } } return ret; } CircleListNode* CircleList_Get(CircleList* list, int pos) { TCircleList* slist = (TCircleList*)list; CircleListNode* ret = NULL; int i = 0; if(0 <= pos) { CircleListNode* current = (CircleListNode*)slist; for(i=0;i<pos i="" current="current-">next; } ret = current->next; } return ret; } CircleListNode* CircleList_Delete(CircleList* list, int pos) { TCircleList* slist = (TCircleList*)list; CircleListNode* ret = NULL; int i = 0; if((slist != NULL) && (0 <= pos)&& (slist->length > 0)) //提高健壮性 { CircleListNode* current = (CircleListNode*)slist; CircleListNode* first = (CircleListNode*)CircleList_Get(list,0); CircleListNode* last = NULL; for(i=0;i<pos i="" current="current-">next; } ret = current->next; current->next = ret->next; //判断删除的是否是第一个元素 if(ret == first) { last = (CircleListNode*)CircleList_Get(list,slist->length-1); //提高效率 slist->header.next = ret->next; last->next = ret->next; } slist->length--; //判断删除的与游标使用同一个节点 if(slist->slider == ret) { slist->slider = ret->next; } if(slist->length == 0) { slist->header.next = NULL; slist->slider == NULL; } } return ret; } CircleListNode* CircleList_DeleteNode(CircleList* list,CircleListNode* node) { TCircleList* slist = (TCircleList*)list; CircleListNode* ret = NULL; int i=0; if(slist != NULL) { CircleListNode* current = (CircleListNode*)slist; for(i=0;i<slist->length;i++) { if(current->next == node) { ret = current->next; break; } current = current->next; } if( ret != NULL) { CircleList_Delete(list,i); } } return ret; } CircleListNode* CircleList_Reset(CircleList* list) { TCircleList* slist = (TCircleList*)list; CircleListNode* ret = NULL; if(slist != NULL) { slist->slider = slist->header.next; ret = slist->slider; } return ret; } CircleListNode* CircleList_Current(CircleList* list) { TCircleList* slist = (TCircleList*)list; CircleListNode* ret = NULL; if(slist != NULL) { ret = slist->slider; } return ret; } CircleListNode* CircleList_Next(CircleList* list) { TCircleList* slist = (TCircleList*)list; CircleListNode* ret = NULL; if((slist != NULL) && (slist->slider != NULL)) { ret = slist->slider; slist->slider = ret->next; } return ret; } </slist-></pos></pos></malloc></stdio> /****************************************** *Function :实现约瑟夫问题 * ******************************************/ /***************************************** #include <stdio h=""> #include <stdlib h=""> #include "CircleList.h" struct Value { CircleListNode header; int v; }; int main(int argc, char *argv[]) { int i = 0; CircleList* list = CircleList_Create(); struct Value v1; struct Value v2; struct Value v3; struct Value v4; struct Value v5; struct Value v6; struct Value v7; struct Value v8; v1.v = 1; v2.v = 2; v3.v = 3; v4.v = 4; v5.v = 5; v6.v = 6; v7.v = 7; v8.v = 8; CircleList_Insert(list, (CircleListNode*)&v1, CircleList_Length(list)); CircleList_Insert(list, (CircleListNode*)&v2, CircleList_Length(list)); CircleList_Insert(list, (CircleListNode*)&v3, CircleList_Length(list)); CircleList_Insert(list, (CircleListNode*)&v4, CircleList_Length(list)); CircleList_Insert(list, (CircleListNode*)&v5, 5); CircleList_Delete(list, 0); for(i=0; i<2 circlelist_length="" list="" i="" struct="" value="" pv="(struct" value="" circlelist_get="" list="" i="" printf="" d="" n="" pv-="">v); } printf(" "); while( CircleList_Length(list) > 0 ) { struct Value* pv = (struct Value*)CircleList_Delete(list, 0); printf("%d ", pv->v); } printf(" "); CircleList_Insert(list, (CircleListNode*)&v1, CircleList_Length(list)); CircleList_Insert(list, (CircleListNode*)&v2, CircleList_Length(list)); CircleList_Insert(list, (CircleListNode*)&v3, CircleList_Length(list)); CircleList_Insert(list, (CircleListNode*)&v4, CircleList_Length(list)); CircleList_Insert(list, (CircleListNode*)&v5, CircleList_Length(list)); CircleList_Insert(list, (CircleListNode*)&v6, CircleList_Length(list)); CircleList_Insert(list, (CircleListNode*)&v7, CircleList_Length(list)); CircleList_Insert(list, (CircleListNode*)&v8, CircleList_Length(list)); for(i=0; i<circlelist_length list="" i="" struct="" value="" pv="(struct" value="" circlelist_next="" list="" printf="" d="" n="" pv-="">v); } printf(" "); struct Value* pv = (struct Value*)CircleList_Reset(list); printf("%d ", pv->v); printf("length is %d ",CircleList_Length(list)); printf(" "); while( CircleList_Length(list) > 0 ) { struct Value* pv = NULL; for(i=1; i<3 i="" printf="" enter="" n="" pv="(struct" value="" circlelist_next="" list="" printf="" d="" n="" pv-="">v); printf("end "); } pv = (struct Value*)CircleList_Current(list); printf("即将删除的元素%d ", pv->v); pv = (struct Value*)CircleList_DeleteNode(list, (CircleListNode*)pv); printf("删除的元素%d ", pv->v); } printf("the list is Empty(%d) ",CircleList_Empty(list)); printf(" "); CircleList_Destroy(list); return 0; } ********************************************************************/ /****************************************** *Function :实现约瑟夫问题 * ******************************************/ #include <stdio h=""> #include <stdlib h=""> #include "CircleList.h" /* run this program using the console pauser or add your own getch, system("pause") or input loop */ typedef struct value { CircleListNode header; char c; } DataElem; int main(int argc, char *argv[]) { int i = 0; CircleList* list = CircleList_Create(); /* DataElem v1,v2,v3,v4,v5,v6; v1.c = 'a'; v2.c = 'b'; v3.c = 'c'; v4.c = 'd'; v5.c = 'e'; v6.c = 'f'; CircleList_Insert(list,(CircleListNode*)&v1,0); CircleList_Insert(list,(CircleListNode*)&v2,0); CircleList_Insert(list,(CircleListNode*)&v3,0); CircleList_Insert(list,(CircleListNode*)&v4,0); CircleList_Insert(list,(CircleListNode*)&v5,0); CircleList_Insert(list,(CircleListNode*)&v6,0); for(i=0;i<CircleList_Length(list);i++) { DataElem* p = (DataElem*)CircleList_Delete(list,CircleList_Length(list)-1); printf("the element is %c ",p->c); } CircleList_Clear(list); printf("CircleList is Empty(%d) ",CircleList_Empty(list)); */ printf("约瑟夫问题 "); DataElem d1,d2,d3,d4,d5,d6,d7,d8; d1.c = '1'; d2.c = '2'; d3.c = '3'; d4.c = '4'; d5.c = '5'; d6.c = '6'; d7.c = '7'; d8.c = '8'; CircleList_Insert(list,(CircleListNode*)&d1,CircleList_Length(list)); CircleList_Insert(list,(CircleListNode*)&d2,CircleList_Length(list)); CircleList_Insert(list,(CircleListNode*)&d3,CircleList_Length(list)); CircleList_Insert(list,(CircleListNode*)&d4,CircleList_Length(list)); CircleList_Insert(list,(CircleListNode*)&d5,CircleList_Length(list)); CircleList_Insert(list,(CircleListNode*)&d6,CircleList_Length(list)); CircleList_Insert(list,(CircleListNode*)&d7,CircleList_Length(list)); CircleList_Insert(list,(CircleListNode*)&d8,CircleList_Length(list)); for(i=0;i<circlelist_length list="" i="" dataelem="" p="(DataElem*)CircleList_Current(list);" circlelist_next="" list="" printf="" the="" element="" is="" c="" n="" p-="">c); } printf("the length is %d ",CircleList_Length(list)); while( CircleList_Length(list) > 0 ) { DataElem* p = NULL; for(i=1; i<3 i="" circlelist_next="" list="" p="(DataElem*)CircleList_Current(list);" printf="" c="" n="" p-="">c); CircleList_DeleteNode(list, (CircleListNode*)p); } printf(" "); CircleList_Destroy(list); return 0; }