头文件:
#ifndef _SEQSTACK_H_
#define _SEQSTACK_H_ typedef void SeqStack;
//创建一个栈
SeqStack* SeqStack_Create(int capacity);
//删除一个栈
void SeqStack_Destroy(SeqStack* stack);
//清空一个栈
void SeqStack_Clear(SeqStack* stack);
//入栈
int SeqStack_Push(SeqStack* stack, void* item);
//出栈
void* SeqStack_Pop(SeqStack* stack);
//获取栈顶元素
void* SeqStack_GetTop(SeqStack* stack);
//获取栈的长度
int SeqStack_GetSize(SeqStack* stack);
//获取栈的容量
int SeqStack_Capacity(SeqStack* stack); #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 = ;
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 = ;
sList->slider = NULL;
return;
} //返回链表的长度
int CircleList_Length(CircleList* list)
{
tagCircleList* sList = NULL;
sList = (tagCircleList*)list;
int ret = -;
//异常处理
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 = -;
//异常处理
if(list == NULL || node == NULL || pos<)
{
return ret;
}
//临时变量Current
CircleListNode* Current = (CircleListNode*)sList; for(int i = ; (i < pos) && (Current->next != NULL); i++)
{
Current = Current->next;
} node->next = Current->next;
Current->next = node; //当长度为0时 游标指向node
if (sList->length == )
{
sList->slider = node;
} sList->length++;
//如果current 依旧指向链表头 证明没跳走 是从0开始插入的 需要头插法
if (Current == (CircleListNode*)sList)
{
//定义一个辅助last 变量来获取尾部节点的信息
CircleListNode* last = (CircleListNode*)CircleList_Get(sList, sList->length - );
//将尾部节点的NEXT域存为当前节点(头节点)
last->next = Current->next;
}
return ;
} //获取POS位置节点的信息
CircleListNode* CircleList_Get(CircleList* list, int pos)
{ tagCircleList* sList = (tagCircleList*)list;
CircleListNode* ret = NULL;
int i = ;
if (list == NULL || pos < )
{
return NULL;
}
CircleListNode* Current = (CircleListNode*)sList;
for(i = ; 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 >=) && (sList->length > ))
{
//将Current指向表头
CircleListNode* Current = (CircleListNode*)(&(sList->header));
//辅助节点last 进行头节点的删除使用 存取最后一个元素
CircleListNode* last = NULL; for(int i = ; i < pos; i++)
{
Current = Current->next;
}
//删除头结点
if ( Current == (CircleListNode*)sList)
{
last = (CircleListNode*)CircleList_Get(sList, sList->length - );
}
//要删除的元素
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 == )
{
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 = ; if (sList != NULL)
{
CircleListNode* Current = (CircleListNode*)sList;
//循环查找node 在链表中的位置
for (i = ; 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 -;
} TempTast t1, t2, t3, t4, t5;
t1.temp = ;
t2.temp = ;
t3.temp = ;
t4.temp = ;
t5.temp = ;
//插入元素
CircleList_Insert(circlelist, (CircleListNode*)(&t1), );
CircleList_Insert(circlelist, (CircleListNode*)(&t2), );
CircleList_Insert(circlelist, (CircleListNode*)(&t3), );
CircleList_Insert(circlelist, (CircleListNode*)(&t4), );
CircleList_Insert(circlelist, (CircleListNode*)(&t5), );
//测试功能
cout << "Length: " << CircleList_Length(circlelist) << endl;
//遍历两次
cout << "遍历两次:" << endl;
for(int i = ; i < *CircleList_Length(circlelist); i++)
{
cout <<"Node:" << ((TempTast*)CircleList_Get(circlelist, i))->temp << endl;
}
cout << endl;
//删除第一个节点
cout <<"Node:" << ((TempTast*)CircleList_Delete(circlelist, ))->temp << endl;
//清空
CircleList_Clear(circlelist);
cout << "After Clear Length: " << CircleList_Length(circlelist) << endl; //插入元素
CircleList_Insert(circlelist, (CircleListNode*)(&t1), );
CircleList_Insert(circlelist, (CircleListNode*)(&t2), );
CircleList_Insert(circlelist, (CircleListNode*)(&t3), );
CircleList_Insert(circlelist, (CircleListNode*)(&t4), );
CircleList_Insert(circlelist, (CircleListNode*)(&t5), );
//删除指定元素
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 ;
}