说明:
该循环链表能够存储任意类型的数据,其数据要求格式如下:
struct Value
{
CircleListNode header;
int(←这里可以任意类型) v;
};该循环链表表头的header指向了第一个元素,slider(滑动器)指向最近一个被操作的元素,length记录了当前链表中已存储的数据;
能存取任意数量的元素;
当使用CircleList_Get获取元素时,下标超过了length,则会返回头部,循环查找;
下面将给出该数据结构的代码,每个函数的结构分析 ,以及个别主要函数的汇编分析
代码:
CircleList.h
#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
CircleList.c
#include<stdio.h>
#include<malloc.h>
#include "CircleList.h"
//循环链表表头,slider记录了链表中当前被操作的元素地址(如插入、删除)
typedef struct _tag_CircleList
{
CircleListNode header;
CircleListNode* slider;
int length;
} TCircleList;
//创造链表
CircleList* CircleList_Create()
{
TCircleList* ret = (TCircleList*)malloc(sizeof(TCircleList));
if (ret != NULL)
{
ret->length = 0;
ret->header.next = NULL;
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->header.next = NULL;
sList->slider = NULL;
}
}
//当前链表的长度
int CircleList_Length(CircleList* list)
{
TCircleList* sList = (TCircleList*)list;
int ret = -1;
if (sList != NULL)
{
ret = sList->length;
}
return ret;
}
//插入元素,slider指向当前插入的元素
int CircleList_Insert(CircleList* list, CircleListNode* node, int pos)
{
TCircleList* sList = (TCircleList*)list;
int ret = (sList != NULL) && (pos >= 0) && (node != NULL);
int i = 0;
if (ret)
{
CircleListNode* current = (CircleListNode*)sList;
for (i = 0; (i < pos) && (current != NULL) && (i<sList->length); i++)
{
current = current->next;
}
node->next = current->next;
current->next = node;
//保持最后一个元素的next指向第一个元素;
if (pos == 0 || sList->length == 0)
{
CircleListNode* last = CircleList_Get(list, CircleList_Length(list));
last->next = node;
}
sList->slider = node;
sList->length++;
}
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;
}
return ret;
}
//根据下标删除元素,如果删除的是slider指向的元素,则slider下移一位
CircleListNode* CircleList_Delete(CircleList* list, int pos)
{
TCircleList* sList = (TCircleList*)list;
CircleListNode* ret = NULL;
int i = 0;
if ((sList != NULL) && (pos >= 0))
{
CircleListNode* current = (CircleListNode*)sList;
CircleListNode* first = sList->header.next;
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--;
if (first == ret)
{
sList->header.next = ret->next;
last->next = ret->next;
}
if (sList->slider == ret)
{
sList->slider = ret->next;
}
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;
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;
}
//重置slider位置,并返回slider
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;
}
//获取slider指向的元素
CircleListNode* CircleList_Current(CircleList* list)
{
TCircleList* sList = (TCircleList*)list;
CircleListNode* ret = NULL;
if (sList != NULL)
{
ret = sList->slider;
}
return ret;
}
//下移slider,并返回移动前的slider
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;
}
cmain.c
#include<stdio.h>
#include<stdlib.h>
#include "CircleList.h"
struct Value
{
CircleListNode header;
int v;
};
int main(int argc, char* argv[])
{
int i = 0;
CircleList* list = CircleList_Create();
struct Value v1;
struct Value v2;
struct Value v3;
struct Value v4;
struct Value v5;
struct Value v6;
struct Value v7;
struct 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;
CircleList_Insert(list, (CircleListNode*)&v1, 0);
CircleList_Insert(list, (CircleListNode*)&v2, CircleList_Length(list));
CircleList_Insert(list, (CircleListNode*)&v3, 0);
CircleList_Insert(list, (CircleListNode*)&v4, 5);
CircleList_Insert(list, (CircleListNode*)&v5, 5);
CircleList_DeleteNode(list, &v5);
for (i = 0; i < 2 * CircleList_Length(list); i++)
{
struct Value* pv = (struct Value*)CircleList_Get(list, i);
printf("%d\n", pv->v);
}
printf("\n");
while (CircleList_Length(list)>0)
{
struct Value* pv = (struct Value*)CircleList_Delete(list, 0);
printf("%d\n", pv->v);
}
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);
}
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;
}
函数结构分析:
1.CircleList_Create
![](http://i.imgur.com/BOeJOS1.png=400x400)
2.CircleList_Destroy
![](http://i.imgur.com/nuchG6J.png=400x400)
3.CircleList_Clear
![](http://i.imgur.com/Fw3NR1v.png=400x400)
4.CircleList_Length
![](http://i.imgur.com/WKhmRR4.png=400x400)
5.CircleList_Insert
![](http://i.imgur.com/psNCy8W.png=400x400)
6.CircleList_Get
![](http://i.imgur.com/kKCUHi4.png=400x400)
7.CircleList_Delete
![](http://i.imgur.com/W9hfytd.png=400x400)
8.CircleList_DeleteNode
![](http://i.imgur.com/2Huxhwn.png=400x400)
9.CircleList_Reset
![](http://i.imgur.com/ZlxSORV.png=400x400)
10.CircleList_Current
![](http://i.imgur.com/xGZPSGx.png=400x400)
11.CircleList_Next
![](http://i.imgur.com/lERDaUd.png=400x400)
汇编分析:
1.CircleList_Create
![](http://i.imgur.com/tfgU1Yd.png=400x400)
2.CircleList_Insert
![](http://i.imgur.com/W1xW3uD.png=400x400)
3.CircleList_Delete
![](http://i.imgur.com/GVrKNWZ.png=400x400)