自己写的,留着用来温习。
#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_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
#include
#include "CircleList.h"
typedef struct _TCircleList
{
CircleListNode* next;
CircleListNode* slider;
int length;
}TCircleList;
CircleList* CircleList_Create()
{
TCircleList* ret = (TCircleList*)malloc(sizeof(TCircleList));
if(ret != NULL)
{
ret->length =0;
ret->next =NULL;
ret->slider = NULL;
return ret;
}
else
return NULL;
}
void CircleList_Destroy(CircleList* list)
{
free(list);
}
void CircleList_Clear(CircleList* list)
{
TCircleList* slist = (TCircleList*)list;
if(slist != NULL)
{
slist->next = NULL;
slist->slider = NULL;
slist->length = 0;
}
}
int CircleList_Length(CircleList* list)
{
int ret = -1;
TCircleList* slist = (TCircleList*)list;
if(slist != NULL)
{
ret = slist->length;
}
return ret;
}
CircleListNode* CircleList_Get(CircleList* list, int pos)
{
TCircleList* slist = (TCircleList*)list;
CircleListNode* node = NULL;
int ret = (slist != NULL) && (pos >= 0);
int i = 0;
if(ret)
{
CircleListNode* current = (CircleListNode*)slist;
for(i=0;i
{
current = current -> next;
}
node = current -> next;
}
return node; //node == null means no element
}
int CircleList_Insert(CircleList* list, CircleListNode* node, int pos)
{
TCircleList* slist = (TCircleList*)list;
int i = 0;
int ret = (slist != NULL) && (pos >= 0) && (node != NULL);
if(ret)
{
CircleListNode* current = (CircleListNode*)slist;
for(i=0;(inext != NULL);i++)/*current->next!=NULL保证当链表中一个节点都还没有的时候,往pos>0的位置插不会出错*/
{
current = current -> next;
}
node -> next = current -> next;
current -> next = node;
if(slist->length == 0)//if insert frist element,init slider
{
slist->slider = node;
}
slist->length ++;//insert 已经完成,长度先加1
if(current == (CircleListNode*)slist)/*如果插入的pos是0,还要修改尾节点的指向,使之指向新插入的节点 */
{
CircleListNode* last = CircleList_Get(slist,slist->length-1);
last->next = node;
}
}
return ret;
}
CircleListNode* CircleList_Delete(CircleList* list, int pos)
{
TCircleList* slist = (TCircleList*)list;
CircleListNode* node = NULL;
int i = 0;
int ret = (slist != NULL) && (pos >= 0)&&(slist->length > 0) ;
if(ret)
{
CircleListNode* current = (CircleListNode*)slist;
CircleListNode* last = NULL;
for(i=0;i
{
current = current -> next;
}
if( (CircleListNode*)slist == current)
{
last = CircleList_Get(slist,slist->length-1);
}
node = current->next;
current->next = node->next;
if(last != NULL)//delet frist
{
last->next = current->next;
}
slist->length--;
if(slist->slider == node)//如果游标指向被删结点
{
slist->slider = node->next;
}
if(slist->length==0)
{
slist->next = NULL;
slist->slider = NULL;
}
}
return node;
}
CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node)
{
TCircleList* slist = (TCircleList*)list;
int i = 0;
int ret = (slist != NULL) && (node != NULL)&& (slist->length > 0) ;
CircleListNode* delet = NULL;
if(ret)
{
CircleListNode* current = (CircleListNode*)slist;
for(i=0;ilength;i++)
{
delet = current -> next;
if(delet == node)
{
break;
}
current = current->next;
}
if(delet != NULL)
{
CircleList_Delete(list, i);
}
}
return delet;
}
CircleListNode* CircleList_Reset(CircleList* list)
{
TCircleList* slist = (TCircleList*)list;
CircleListNode* ret = NULL;
if(slist != NULL)
{
slist->slider = slist->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((list != NULL)&&(slist -> slider != NULL))
{
ret = slist -> slider;
slist->slider = ret->next;
}
return ret;
}
阅读(547) | 评论(0) | 转发(0) |