循环链表的C风格实现(单向)
程序员文章站
2024-03-06 21:52:08
...
头文件:
#ifndef _CIRCLELIST_H_
#define _CIRCLELIST_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);
//在POS位置插入一个节点
int CircleList_Insert(CircleList* list, CircleListNode* node, int pos);
//获取POS位置节点的信息
CircleListNode* CircleList_Get(CircleList* list, int pos);
//删除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);
//游标的NEXT域
CircleListNode* CircleList_Next(CircleList* list);
#endif
//Cpp
#include "circleList.h"
#include <iostream>
using namespace std;
//这个为头链表头
typedef struct _tag_CircleList
{
CircleListNode header;
CircleListNode* slider;
int length;
}tagCircleList;
//创建一个循环链表
CircleList* CircleList_Create()
{
tagCircleList* ret = (tagCircleList*)malloc(sizeof(tagCircleList)); //分配内存
if (ret == NULL)
{
return NULL;
}
//初始化
ret->header.next = NULL;
ret->length = 0;
ret->slider = NULL;
return ret;
}
//删除一个循环链表
void CircleList_Destroy(CircleList* list)
{
if (list = NULL)
{
return;
}
//释放内存
free(list);
return;
}
//清空一个循环链表
void CircleList_Clear(CircleList* list)
{
tagCircleList* sList = NULL;
sList = (tagCircleList*)list;
if (sList == NULL)
{
return ;
}
//重置为初始化状态
sList->header.next = NULL;
sList->length = 0;
sList->slider = NULL;
return;
}
//返回链表的长度
int CircleList_Length(CircleList* list)
{
tagCircleList* sList = NULL;
sList = (tagCircleList*)list;
int ret = -1;
//异常处理
if (list == NULL)
{
return ret;
}
return sList->length;
}
//在POS位置插入一个节点
int CircleList_Insert(CircleList* list, CircleListNode* node, int pos)
{
tagCircleList* sList = NULL;
sList = (tagCircleList*)list;
int ret = -1;
//异常处理
if(list == NULL || node == NULL || pos<0)
{
return ret;
}
//临时变量Current
CircleListNode* Current = (CircleListNode*)sList;
for(int i = 0; (i < pos) && (Current->next != NULL); i++)
{
Current = Current->next;
}
node->next = Current->next;
Current->next = node;
//当长度为0时 游标指向node
if (sList->length == 0)
{
sList->slider = node;
}
sList->length++;
//如果current 依旧指向链表头 证明没跳走 是从0开始插入的 需要头插法
if (Current == (CircleListNode*)sList)
{
//定义一个辅助last 变量来获取尾部节点的信息
CircleListNode* last = (CircleListNode*)CircleList_Get(sList, sList->length - 1);
//将尾部节点的NEXT域存为当前节点(头节点)
last->next = Current->next;
}
return 0;
}
//获取POS位置节点的信息
CircleListNode* CircleList_Get(CircleList* list, int pos)
{
tagCircleList* sList = (tagCircleList*)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;
}
//删除POS位置的节点
CircleListNode* CircleList_Delete(CircleList* list, int pos)
{
tagCircleList* sList = (tagCircleList*)list;
CircleListNode* ret = NULL;
if ((sList != NULL) && (pos >=0) && (sList->length > 0))
{
//将Current指向表头
CircleListNode* Current = (CircleListNode*)(&(sList->header));
//辅助节点last 进行头节点的删除使用 存取最后一个元素
CircleListNode* last = NULL;
for(int 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;
Current->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)
{
tagCircleList* sList = (tagCircleList*)list;
CircleListNode* ret = NULL;
int i = 0;
if (sList != NULL)
{
CircleListNode* Current = (CircleListNode*)sList;
//循环查找node 在链表中的位置
for (i = 0; i < sList->length; i++)
{
if (Current->next == node)
{
ret = Current->next;
break;
}
Current = Current->next;
}
//找到了 使用CircleList_Delete 删除
if(ret != NULL)
{
CircleList_Delete(list, i);
}
}
return ret;
}
//重置游标位置
CircleListNode* CircleList_Reset(CircleList* list)
{
tagCircleList* sList = (tagCircleList*)list;
CircleListNode* ret = NULL;
//如果不为空
if (sList != NULL)
{
sList->slider = sList->header.next;
ret = sList->slider;
}
return ret;
}
//当前游标位置
CircleListNode* CircleList_Current(CircleList* list)
{
tagCircleList* sList = (tagCircleList*)list;
CircleListNode* ret = NULL;
//如果不为空
if (sList != NULL)
{
ret = sList->slider;
}
return ret;
}
//把游标位置返回,游标下移
CircleListNode* CircleList_Next(CircleList* list)
{
tagCircleList* sList = (tagCircleList*)list;
CircleListNode* ret = NULL;
//如果不为空
if((sList != NULL) && (sList->slider != NULL))
{
ret = sList->slider;
sList->slider = ret->next;
}
return ret;
}
测试框架如下:
#include "circleList.h"
#include <iostream>
using namespace std;
typedef struct _Temp_Test
{
CircleListNode node;
int temp;
char temp2;
}TempTast;
int main()
{
CircleList* circlelist = NULL;
circlelist = CircleList_Create();
//异常处理
if (circlelist == NULL)
{
cout << "Create Err " << endl;
return -1;
}
TempTast t1, t2, t3, t4, t5;
t1.temp = 1;
t2.temp = 2;
t3.temp = 3;
t4.temp = 4;
t5.temp = 5;
//插入元素
CircleList_Insert(circlelist, (CircleListNode*)(&t1), 0);
CircleList_Insert(circlelist, (CircleListNode*)(&t2), 0);
CircleList_Insert(circlelist, (CircleListNode*)(&t3), 0);
CircleList_Insert(circlelist, (CircleListNode*)(&t4), 0);
CircleList_Insert(circlelist, (CircleListNode*)(&t5), 0);
//测试功能
cout << "Length: " << CircleList_Length(circlelist) << endl;
//遍历两次
cout << "遍历两次:" << endl;
for(int i = 0; i < 2*CircleList_Length(circlelist); i++)
{
cout <<"Node:" << ((TempTast*)CircleList_Get(circlelist, i))->temp << endl;
}
cout << endl;
//删除第一个节点
cout <<"Node:" << ((TempTast*)CircleList_Delete(circlelist, 0))->temp << endl;
//清空
CircleList_Clear(circlelist);
cout << "After Clear Length: " << CircleList_Length(circlelist) << endl;
//插入元素
CircleList_Insert(circlelist, (CircleListNode*)(&t1), 0);
CircleList_Insert(circlelist, (CircleListNode*)(&t2), 0);
CircleList_Insert(circlelist, (CircleListNode*)(&t3), 0);
CircleList_Insert(circlelist, (CircleListNode*)(&t4), 0);
CircleList_Insert(circlelist, (CircleListNode*)(&t5), 0);
//删除指定元素
cout << "Delete Node :" << ((TempTast*)CircleList_DeleteNode(circlelist, (CircleListNode*)(&t1)))->temp << endl;
//显示游标当前位置
cout << "Silder Now :" << ((TempTast*)CircleList_Current(circlelist))->temp << endl;
//移动后
CircleList_Next(circlelist);
cout << "Silder After Next :" << ((TempTast*)CircleList_Current(circlelist))->temp << endl;
//重置后
CircleList_Reset(circlelist);
cout << "Silder After Reset :" << ((TempTast*)CircleList_Current(circlelist))->temp << endl;
cout << endl;
//销毁
CircleList_Destroy(circlelist);
cout << "circle has been Destroied" << endl;
system("pause");
return 0;
}