circlelist .h
#ifndef _MYCIRCLELIST_H_
#define _MYCIRCLELIST_H_
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef void CircleList;
typedef struct _tag_CircleListNode
{
struct _tag_CircleListNode* next;
}CircleListNode;
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 // !_MYCIRCLELIST_H_
头文件中定义链表结点
typedef struct _tag_CircleListNode
{
struct _tag_CircleListNode* next;
}CircleListNode;
circlelist.c
#include"circlelist.h"
typedef struct _tag_CircleList
{
CircleListNode header;
CircleListNode* slider; //游标是比链表新增的
int length;
}TCircleList;
CircleList* CircleList_Create()
{
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)
{
if (list != NULL)
{
free(list);
return;
}
return;
}
void CircleList_Clear(CircleList* list)
{
TCircleList* sList = (TCircleList*)list;
if (sList == NULL)
{
return;
}
sList->length = 0;
sList->header.next = NULL;
sList->slider = NULL;
return;
}
int CircleList_Length(CircleList* list)
{
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)
{
int ret = 0, i = 0;
TCircleList* sList = (TCircleList*)list;
if (list == NULL || node == NULL || pos < 0)
{
return -1;
}
CircleListNode* current = (CircleListNode*)sList;
for (i = 0; (i < pos) && (current->next != NULL); i++)
{
current = current->next;
}
node->next = current->next; //1
current->next = node; //2
//若第一次插入节点
if (sList->length == 0)
{
sList->slider = node;
}
sList->length++;
//如果是头插法
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)
{
TCircleList* sList = (TCircleList*)list;
CircleListNode* ret = NULL;
int i = 0;
if (list == NULL || pos < 0)
{
return NULL;
}
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) && (pos >= 0) && (sList->length > 0))
{
CircleListNode* current = (CircleListNode*)sList;
//CircleListNode* current = (CircleListNode*)(&(sList->header));
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)
{
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;
}
if(ret!=NULL)
{
CircleList_Delete(sList, 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;
}
循环链表一是要把尾部结点的next指向头结点
二是多了一个游标来寻找当前结点。