标签:获取 查找 bsp des image size circle include 循环列表
循环列表图示
没多什么,就多了一个循环的处理方式。
#ifndef _CIRCLELIST_H_ #define _CIRCLELIST_H_ typedef void CircleList; typedef struct _tag_CircleListNode { struct _tag_CircleListNode * next; }CircleListNode; CircleList* CircleList_Create(); void List_Destroy(CircleList* list); void CircleList_Clear(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); //游标指向2号位置 //把2号位置返回出来,同时让游标下移到3号位置 CircleListNode* CircleList_Next(CircleList* list); #endif
#include <stdio.h> #include <malloc.h> #include "circlelist.h" typedef struct _tag_CircleList { CircleListNode header; CircleListNode* slider;// 游标指针 int length; } TCircleList; CircleList* CircleList_Create() // O(1) { TCircleList* ret = (TCircleList*)malloc(sizeof(TCircleList)); if (ret == NULL) { return NULL; } ret->length = 0; ret->header.next = NULL; ret->slider = NULL; return ret; } void CircleList_Destroy(CircleList* list) // O(1) { if (list == NULL) { return ; } free(list); } //没有内存的管理,仅仅是将应用程序分配的内存空间给串联了起来, //因此这里的清楚操作就变得很简单了 void CircleList_Clear(CircleList* list) // O(1) { TCircleList* sList = (TCircleList*)list; if (sList == NULL) { return ; } sList->length = 0; sList->header.next = NULL; sList->slider = NULL; } int CircleList_Length(CircleList* list) // O(1) { TCircleList* sList = (TCircleList*)list; int ret = -1; if (list == NULL) { return ret; } ret = sList->length; return ret; } int CircleList_Insert(CircleList* list, CircleListNode* node, int pos) // O(n) { //首先进行条件判断 if(list == NULL || node == NULL || pos <0) { return -1; } TCircleList* sList = (TCircleList*)list; if(sList->length == 0) { sList->header.next = node; sList->slider = node; sList->length++; node->next = node; return 0; } CircleListNode* current = (CircleListNode*)sList; for(int i=0; (i<pos) && (current->next != NULL); i++) { current = current->next; } //current->next 0号节点的地址 node->next = current->next; //1 current->next = node; //2 sList->length++; //若头插法 current仍然指向头部 //(原因是:跳0步,没有跳走) if( current == (CircleListNode*)sList ) { //获取最后一个元素 CircleListNode* last = CircleList_Get(sList, sList->length - 1); last->next = current->next; //3 } return 0; } CircleListNode* CircleList_Get(CircleList* list, int pos) // O(n) { if (list==NULL || pos<0) { return NULL; } TCircleList* sList = (TCircleList*)list; CircleListNode* current = (CircleListNode*)sList; for(int i=0; i<pos; i++) { current = current->next; } return current->next; } CircleListNode* CircleList_Delete(CircleList* list, int pos) // O(n) { TCircleList* sList = (TCircleList*)list; //条件判断 if(list == NULL || pos < 0 || sList->length == 0) { return NULL; } //只剩下一个元素的情况下 if(sList->length == 1) { CircleListNode *ret = sList->header.next; sList->header.next = NULL; sList->slider = NULL; sList->length = 0; return ret; } CircleListNode* ret = NULL; CircleListNode* current = (CircleListNode*)sList; for(int i=0; i<pos; i++) { current = current->next; } CircleListNode* last = NULL; //若删除第一个元素(头结点) if( current == (CircleListNode*)sList ) { last = (CircleListNode*)CircleList_Get(sList, sList->length - 1); } //求要删除的元素 ret = current->next; current->next = ret->next; sList->length--; //如果是删除第一个元素 if( last != NULL ) { sList->header.next = ret->next; last->next = ret->next; } //若删除的元素为游标所指的元素 if( sList->slider == ret ) { sList->slider = ret->next; } return ret; } CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node) // O(n) { TCircleList* sList = (TCircleList*)list; CircleListNode* ret = NULL; int i = 0; if( sList != NULL ) { CircleListNode* current = (CircleListNode*)sList; //查找node在循环链表中的位置i for(i=0; i<sList->length; i++) { if( current->next == node ) { ret = current->next; break; } current = current->next; } //如果ret找到,根据i去删除 if( ret != NULL ) { CircleList_Delete(sList, i); //根据结点的值 求出结点的位置 ,根据位置删除元素 } } return ret; } CircleListNode* CircleList_Reset(CircleList* list) // O(1) { 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) // O(1) { TCircleList* sList = (TCircleList*)list; CircleListNode* ret = NULL; if( sList != NULL ) { ret = sList->slider; } return ret; } //把当前位置返回,并且游标下移 //把当前游标所指的位置的元素返回出去 //游标下移 CircleListNode* CircleList_Next(CircleList* list) // O(1) { TCircleList* sList = (TCircleList*)list; CircleListNode* ret = NULL; if( (sList != NULL) && (sList->slider != NULL) ) { ret = sList->slider; sList->slider = ret->next; } return ret; }
通过循环链表,可以很好的解决约瑟夫问题。
#include <stdio.h> #include <stdlib.h> #include "circlelist.h" struct Value { CircleListNode header; int v; }; void main() { int i = 0; CircleList* list = CircleList_Create(); struct Value v1, v2, v3, v4, v5, v6, v7, 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, 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 ", pv->v); } printf("\n"); //重置游标 CircleList_Reset(list); while( CircleList_Length(list) > 0 ) { struct Value* pv = NULL; for(i=1; i<3; i++) { CircleList_Next(list); } pv = (struct Value*)CircleList_Current(list); printf("%d ", pv->v); CircleList_DeleteNode(list, (CircleListNode*)pv); //根据结点的值,进行结点元素的删除 } CircleList_Destroy(list); return ; }
标签:获取 查找 bsp des image size circle include 循环列表
原文地址:https://www.cnblogs.com/randyniu/p/9164106.html