线性表_循环链表
何谓之循环链表,就是在类linux内核链表的基础上多了一条指针,即尾节点指回了首节点(也有指回头节点的,这里以首节点为例),还是丑图一张说明情况:
与之前链表不同之出就在于:
1.多了一个指回来的指针。
2.头节点多了一个slider游标(后面会提到)。
3.多了三个关于slider的api函数,和一个根据节点坐标来删除节点的函数。
4.插入、删除操作是否在头节点操作与一般位置略有不同。
后面演示代码有些句柄,这里先展示一些重定义,方便后面理解代码:
typedef void CircleList;
//小的业务节点,做穿针引线之用途
typedef struct _tag_CircleListNode
{
struct _tag_CircleListNode * next;
}CircleListNode;
//大的业务节点
typedef struct _tag_CircleList
{
CircleListNode header;
CircleListNode* slider;
int length;
} TCircleList;
这是一个头节点,后续只要将大节点中包含小的业务节点CircleListNode就可以将他们串起来,最后再指回头节点即可形成一条循环链表。
如是,一个循环链表即形成。
它的基本api包括:
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);
//add
CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node);
CircleListNode* CircleList_Reset(CircleList* list);
CircleListNode* CircleList_Current(CircleList* list);
CircleListNode* CircleList_Next(CircleList* list);
与之前的不同主要体现在Insert和Delete操作上:
int CircleList_Insert(CircleList* list, CircleListNode* node, int pos)
插入操作差异可分为如下几种情况:
1.第一次插入节点
2.非第一次插入节点
(1)插入头部位置
(2)插入非头部位置
还是用图试图表现其差异:
所以有了如下插入操作代码:
int CircleList_Insert(CircleList* list, CircleListNode* node, int pos) // O(n)
{
int ret = 0, i=0;
TCircleList* sList = (TCircleList*)list;
if (list == NULL || node== NULL || pos<0)
{
return -1;
}
//if( ret )
{
CircleListNode* current = (CircleListNode*)sList;
for(i=0; (i<pos) && (current->next != NULL); i++)
{
current = current->next;
}
//current->next 0号节点的地址
node->next = current->next; //1
current->next = node; //2
//若第一次插入节点
if( sList->length == 0 )
{
sList->slider = node;
}
sList->length++;
//若头插法 current仍然指向头部
//(原因是:跳0步,没有跳走)
if( current == (CircleListNode*)sList )
{
//获取最后一个元素
CircleListNode* last = CircleList_Get(sList, sList->length - 1);
last->next = current->next; //3
}
}
return ret;
}
CircleListNode* CircleList_Delete(CircleList* list, int pos)
与插入类似删除也存在一定差异,这里不再赘述,代码中体会;
CircleListNode* CircleList_Delete(CircleList* list, int pos) // O(n)
{
TCircleList* sList = (TCircleList*)list;
CircleListNode* ret = NULL;
int i = 0;
if( (sList != NULL) && (pos >= 0) && (sList->length > 0) )
{
CircleListNode* current = (CircleListNode*)sList;
CircleListNode* last = NULL;
for(i=0; i<pos; i++)
{
current = current->next;
}
//若删除第一个元素(头结点)
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;
}
//若删除元素后,链表长度为0
if( sList->length == 0 )
{
sList->header.next = NULL;
sList->slider = NULL;
}
}
return ret;
}
还有如下新增加的一个函数,无非是1找到要删除节点2调用delete删除。
CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node)
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;
}
最后说一个循环链表的应用:约瑟夫问题
问题是这样的:8个人围成一圈,从第一个开始报数,报到3的出列,下一个人从1开始报,求最后出列后的顺序是什么?(36152847)
代码如下:
#include <stdio.h>
#include <malloc.h>
typedef struct _tag_CircleListNode
{
struct _tag_CircleListNode * next;
}CircleListNode;
typedef void CircleList;
typedef struct _tag_CircleList
{
CircleListNode header;
CircleListNode* slider;
int length;
} TCircleList;
struct Value
{
CircleListNode header;
int v;
};
CircleListNode* CircleList_Get(CircleList* list, int pos);
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)
{
int ret = 0, i=0;
TCircleList* sList = (TCircleList*)list;
if (list == NULL || node== NULL || pos<0)
{
return -1;
}
//if( ret )
{
CircleListNode* current = (CircleListNode*)sList;
for(i=0; (i<pos) && (current->next != NULL); i++)
{
current = current->next;
}
//current->next 0号节点的地址
node->next = current->next; //1
current->next = node; //2
//若第一次插入节点
if( sList->length == 0 )
{
sList->slider = node;
}
sList->length++;
//若头插法 current仍然指向头部
//(原因是:跳0步,没有跳走)
if( current == (CircleListNode*)sList )
{
//获取最后一个元素
CircleListNode* last = CircleList_Get(sList, sList->length - 1);
last->next = current->next; //3
}
}
return ret;
}
CircleListNode* CircleList_Get(CircleList* list, int pos) // O(n)
{
TCircleList* sList = (TCircleList*)list;
CircleListNode* ret = NULL;
int i = 0;
if (list==NULL || pos<0)
{
return NULL;
}
//if( (sList != NULL) && (pos >= 0) && (sList->length > 0) )
{
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) // O(n)
{
TCircleList* sList = (TCircleList*)list;
CircleListNode* ret = NULL;
int i = 0;
if( (sList != NULL) && (pos >= 0) && (sList->length > 0) )
{
CircleListNode* current = (CircleListNode*)sList;
CircleListNode* last = NULL;
for(i=0; i<pos; i++)
{
current = current->next;
}
//若删除第一个元素(头结点)
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;
}
//若删除元素后,链表长度为0
if( sList->length == 0 )
{
sList->header.next = NULL;
sList->slider = NULL;
}
}
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;
}
int 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\n", 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\n", pv->v);
CircleList_DeleteNode(list, (CircleListNode*)pv);
}
CircleList_Destroy(list);
return 0;
}
运行结果如下: