测试框架:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"CircleList.h"
using namespace std;
struct Teacher
{
CircleListNode node;
int age;
char name[64];
};
void main()
{
int i = 0, len = 0;
Circlelist* list = NULL;
list = List_Create();
if (list == NULL)
{
return;
}
Teacher t1, t2, t3, t4, t5;
t1.age = 20;
strcpy(t1.name, "wang1");
t2.age = 30;
strcpy(t2.name, "wang2");
t3.age = 40;
strcpy(t3.name, "wang3");
t4.age = 50;
strcpy(t4.name, "wang4");
t5.age = 60;
strcpy(t5.name, "wang5");
len = List_Length(list);
List_Inster(list, (CircleListNode*)&t1, 0);
List_Inster(list, (CircleListNode*)&t2, 0);
List_Inster(list, (CircleListNode*)&t3, 0);
List_Inster(list, (CircleListNode*)&t4, 0);
List_Inster(list, (CircleListNode*)&t5, 0);
//遍历
for (i = 0; i < 2*List_Length(list); i++)//证明是否是循环链表只要把长度乘以2,打印两次
{
Teacher* tmp = (Teacher*)List_Get(list, i);
if (tmp == NULL)
{
return;
}
printf("teacher->age :%d\n", tmp->age);
printf("teacher->name :%s\n", tmp->name);
}
//删除链表
while (List_Length(list)>0)
{
Teacher* tmp = (Teacher*)List_Delete(list, 0);
if (tmp == NULL)
{
return;
}
printf("teacher->age :%d\n", tmp->age);
}
List_Destory(list);
system("pause");
}
头文件
#pragma once
#ifdef _MYLIST_H_
#define _MYLIST_H_
#endif // _MYSEQLIST_H_
typedef void Circlelist;
typedef struct CircleListNode
{
struct CircleListNode* next;
};
/*创建列表*/
Circlelist * List_Create();
/*释放列表*/
void List_Destory(Circlelist* list);
/*清空列表 回到初始状态*/
void List_Clear(Circlelist* list);
/*获取链表的长度*/
int List_Length(Circlelist* list);
/*获取指定位置的链表的元素数据*/
CircleListNode* List_Get(Circlelist* list, int pos);
/*插入元素数据*/
int List_Inster(Circlelist* list, CircleListNode* node, int pos);
/*元素数据删除*/
CircleListNode* List_Delete(Circlelist* list, int pos);
//add
CircleListNode* Circle_DeleteNode(Circlelist* list,CircleListNode* node);
//重置游标,重新指向链表的头部
CircleListNode* Circle_Reset(Circlelist* list);
//获取当前游标
CircleListNode* Circle_Current(Circlelist* list);
//把当前的位置返回,并使游标指向下一个位置
CircleListNode* Circle_Next(Circlelist* list);
头文件实现;
#include"CircleList.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct TLinkList
{
CircleListNode header;
CircleListNode* slider;
int lenght;
};
/*创建列表*/
Circlelist * List_Create()
{
TLinkList* ret = NULL;
ret = (TLinkList*)malloc(sizeof(TLinkList));
memset(ret, 0, sizeof(TLinkList));
ret->lenght = 0;
ret->header.next = NULL;
ret->slider = NULL;
return ret;
}
/*释放列表*/
void List_Destory(Circlelist* list)
{
if (list != NULL)
{
free(list);
list = NULL;
}
return;
}
/*清空列表 回到初始状态*/
void List_Clear(Circlelist* list)
{
TLinkList *tlist = NULL;
if (list == NULL)
{
return;
}
tlist = (TLinkList*)list;
tlist->lenght = 0;
tlist->header.next = NULL;
tlist->slider = NULL;
return;
}
/*获取链表的长度*/
int List_Length(Circlelist* list)
{
TLinkList* tlist = NULL;
if (list == NULL)
{
return 0;
}
tlist = (TLinkList*)list;
return tlist->lenght;
}
/*获取指定位置的链表的元素数据*/
CircleListNode* List_Get(Circlelist* list, int pos)
{
int i = 0;
if (list == NULL || pos<0)
{
return NULL;
}
CircleListNode* current = NULL;
TLinkList* tlist = NULL;
tlist = (TLinkList*)list;
current = &(tlist->header);
//让辅助指针指向链表的头部
for (i = 0; i < pos && (current->next != NULL); i++)
{
current = current->next;
}
return current->next;
}
/*插入元素数据*/
int List_Inster(Circlelist* list, CircleListNode* node, int pos)
{
if (list == NULL || pos<0 || node == NULL)
{
return 0;
}
int i = 0, ret = 0;
TLinkList* tlist = NULL;
tlist = (TLinkList*)list;
CircleListNode* current = (CircleListNode*)tlist;
//current = &(tlist->header);
for (i = 0; i < pos && (current->next != NULL); i++)
{
current = current->next;
}
node->next = current->next;
current->next = node;
//若是第一次插入节点
if (tlist->lenght==0)
{
tlist->slider = node;
}
tlist->lenght++;
//若是头插法 current仍然指向头部
//原因是:跳0步,没有跳走 或者pos是0 代表是头插法
if (current==(CircleListNode*)tlist)
{
//获取最后一个元素
CircleListNode* last = List_Get(tlist,tlist->lenght-1);
last->next = current->next;
}
return 0;
}
/*元素数据删除*/
CircleListNode* List_Delete(Circlelist* list, int pos)
{
int i = 0;
if (list == NULL || pos<0)
{
return 0;
}
TLinkList* tlist = NULL;
CircleListNode* current = NULL;
tlist = (TLinkList*)list;
current = &(tlist->header);
CircleListNode* ret = NULL;
CircleListNode* last = NULL;
for (i = 0; i <pos && (current->next != NULL); i++)
{
current = current->next;
}
//若删除的是第一个元素
if (current==(CircleListNode*)tlist)
{
last = (CircleListNode*)List_Get(tlist, tlist->lenght - 1);
}
//求要删除的元素
ret = current->next;
current->next = ret->next;
tlist->lenght--;
//判断链表是否为空
if (last != NULL)
{
tlist->header.next = ret->next;
last->next = ret->next;
}
//若删除的元素为游标所指的元素
if (tlist ->slider ==ret)
{
tlist->slider = ret->next;
}
//若删除元素后,链表长度为0
if (tlist->lenght==0)
{
tlist->header.next = NULL;
tlist->slider = NULL;
}
return ret;
}
CircleListNode* Circle_DeleteNode(Circlelist* list, CircleListNode* node)
{
TLinkList* tlist = NULL;
tlist = (TLinkList*)list;
CircleListNode* ret = NULL;
int i = 0;
if (tlist!= NULL)
{
CircleListNode* current = (CircleListNode*)tlist;
for ( i = 0; i < tlist->lenght; i++)
{
if (current ->next==node)
{
ret = current->next;
break;
}
current = current->next;
}
if (ret!=NULL)
{
List_Delete(tlist,i);
}
}
return ret;
}
CircleListNode* Circle_Reset(Circlelist* list)
{
TLinkList* tlist = (TLinkList*)list;
CircleListNode* ret = NULL;
if (tlist!=NULL)
{
tlist->slider = tlist->header.next;
ret = tlist->slider;
}
return NULL;
}
CircleListNode* Circle_Current(Circlelist* list)
{
TLinkList* tlist = (TLinkList*)list;
CircleListNode* ret = NULL;
if (tlist != NULL)
{
ret = tlist->slider;
}
return ret;
}
CircleListNode* Circle_Next(Circlelist* list)
{
TLinkList* tlist = (TLinkList*)list;
CircleListNode* ret = NULL;
if ((tlist != NULL)&&tlist->slider!=NULL)
{
ret = tlist->slider;
tlist->slider = ret->next;
}
return ret;
}