# 数据结构--循环链表

#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* next;
int v;
}value;
int main(int argc, char *argv[])
{
CircleList* list=CircleList_Create();
value v1;
value v2;
value v3;
value v4;
value v5;
value v6;
value v7;
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;
int i=0;
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++)
{
value *pv=(value*)CircleList_Get(list,i);
printf("%d\n",pv->v);
}
printf("\n");

while(CircleList_Length(list)>0)
{
value *pv=(value*)CircleList_Delete(list,0);
printf("%d\n",pv->v);
}
printf("\n");

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++)
{
value *pv=(value*)CircleList_Next(list);
printf("%d\n",pv->v);
}
printf("\n");

CircleList_Reset(list);
while(CircleList_Length(list)>0)
{
value* pv=NULL;
for(i=1;i<3;i++)
{
CircleList_Next(list);
}
pv=(value*)CircleList_Current(list);
printf("%d\n",pv->v);
CircleList_DeleteNode(list,(CircleListNode*)pv);
}

CircleList_Destroy(list);
return 0;
}

#include <stdio.h>
#include <malloc.h>
#include "CircleList.h"
typedef struct _tag_TCircleList
{
CircleListNode* slider;
int length;
}TCircleList;

CircleList* CircleList_Create()
{
TCircleList* ret=(TCircleList*)malloc(sizeof(TCircleList));
if(ret!=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->length=0;
sList->slider=NULL;
}
}
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)&&(node!=NULL)&&(pos>=0));
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;
//插入第一个元素的时候，slider默认指向第一个元素
if(sList->length==0)
{
sList->slider=node;
node->next=node;
}
sList->length++;
}
return ret;
}
CircleListNode* CircleList_Delete(CircleList* list,int pos)
{
TCircleList* sList=(TCircleList*)list;
int i=0;
CircleListNode* ret=NULL;
int flag=((sList!=NULL)&&(pos>=0));
if(flag)
{
CircleListNode* current=(CircleListNode*)sList;
//标记第一个元素和最后一个元素
CircleListNode* last=(CircleListNode*)CircleList_Get(sList,sList->length-1);
for(i=0;i<pos;i++)
{
current=current->next;
}
ret=current->next;
current->next=ret->next;
sList->length--;
//第一个袁术是特殊的位置，表头的next,最后一个元素的next都需要移动
//要删除的元素是表头
if(first==ret)
{
last->next=ret->next;
}
//删除的元素刚好是游标所在的节点
if(sList->slider==ret)
{
sList->slider=ret->next;
}
//链表为空的情况
if(sList->length==0)
{
sList->slider=NULL;
}
}
return ret;
}
CircleListNode* CircleList_Get(CircleList* list,int pos)
{
TCircleList* sList=(TCircleList*)list;
CircleListNode* ret=NULL;
int i=0;
if((sList!=NULL)&&(pos>=0))
{
CircleListNode* current=(CircleListNode*)sList;
for(i=0;i<pos;i++)
{
current=current->next;
}
ret=current->next;
}
}
CircleListNode* CircleList_DeleteNode(CircleList* list,CircleListNode* node)
{
TCircleList* sList=(TCircleList*)list;
int i=0;
CircleListNode* ret=NULL;
int flag=((sList!=NULL));
if(flag)
{

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(sList,i);
}
}
return ret;
}
CircleListNode* CircleList_Reset(CircleList* list)
{
TCircleList* sList=(TCircleList*)list;
CircleListNode* ret=NULL;
if(sList!=NULL)
{
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;
}

#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_Length(CircleList* list);
int CircleList_Insert(CircleList* list,CircleListNode* node,int pos);
CircleListNode* CircleList_Delete(CircleList* list,int pos);
CircleListNode* CircleList_Get(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