马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 乔宽 于 2020-4-18 17:30 编辑 #ifndef CIRCLELINKLIST
#define CIRCLELINKLIST
#define CIRCLELINKLIST_TRUE 1
#define CIRCLELINKLIST_FALSE 0
#include <stdio.h>
#include <stdlib.h>
//链表小结点
typedef struct CIRCLELINKNODE{
struct CIRCLELINKNODE * next;
}CircleLinkNode;
//链表结构体
typedef struct CIRCLELINKLIST{
CircleLinkNode head;
int size;//判断循环是否停止的标志
}CircleLinkList;
//编写针对链表结构体操作的API函数
//比较回调
typedef int(*COMPARENODE)(CircleLinkNode *, CircleLinkNode *);
typedef void(*PRINTLINKLIST)(CircleLinkNode *);
//初始化函数
CircleLinkList * Init_CircleLinkList();
//插入函数
void Insert_CircleLinkList(CircleLinkList * clist, int pos, CircleLinkNode* data);
//根据位置删除
void RemoveByPos_CircleLinkList(CircleLinkList * clist, int pos);
//根据值删除函数
void RemoveByValue_CircleLinkList(CircleLinkList * clist, CircleLinkNode * data,COMPARENODE compare);
//获取链表的长度
int Size_CircleLinkList(CircleLinkList * clist);
//判断是否为空
int IsEmpty_CircleLinkList(CircleLinkList * clist);
//返回第一个元素
CircleLinkNode * Front_CircleLinkList(CircleLinkList * clist);
int Find_CircleLinkList(CircleLinkList * clist, CircleLinkNode * data, COMPARENODE compare);
//打印
void Print_CircleLinkList(CircleLinkList * clist, PRINTLINKLIST print);
//释放
void FreeSpace_CircleLinkList(CircleLinkList * clist);
#endif
#include "CircleLinkList.h"
//初始化函数
CircleLinkList * Init_CircleLinkList(){
CircleLinkList *clist = (CircleLinkList *)malloc(sizeof(CircleLinkList));
clist->head.next = &(clist->head);//指向自己
clist->size = 0;
return clist;
}
//插入函数
void Insert_CircleLinkList(CircleLinkList * clist, int pos, CircleLinkNode* data){
if (clist == NULL)
{
return;
}
if (data == NULL)
{
return;
}
if (pos < 0 || pos >=clist->size)
{
pos = clist->size;
}
//根据位置查找结点
//辅助指针变量
CircleLinkNode * pCurrent = &(clist->head);
for (int i = 0; i < pos; i++)
{
pCurrent = pCurrent->next;
}
data->next = pCurrent->next;
pCurrent->next = data;
clist->size++;
}
//根据位置删除
void RemoveByPos_CircleLinkList(CircleLinkList * clist, int pos){
if (clist == NULL)
{
return;
}
if (pos < 0 || pos >= clist->size){
return;
}
CircleLinkNode * pCurrent = &(clist->head);
for (int i = 0; i < pos; i++){
pCurrent = pCurrent->next;
}
//缓存当前结点的下一个结点
CircleLinkNode * pNext = pCurrent->next;
pCurrent->next = pNext->next;
clist->size--;
}
//根据值删除函数
void RemoveByValue_CircleLinkList(CircleLinkList * clist, CircleLinkNode * data, COMPARENODE compare){
if (clist == NULL){
return;
}
if (data == NULL){
return;
}
CircleLinkNode * pPrev = &(clist->head);
CircleLinkNode * pCurrent = pPrev->next;
int i = 0;
for ( i = 0; i < clist->size; i++){
if (compare(pCurrent,data) == CIRCLELINKLIST_TRUE)
{
pPrev->next = pCurrent->next;
break;
}
pPrev = pCurrent;
pCurrent = pPrev->next;
}
clist->size--;
}
//获取链表的长度
int Size_CircleLinkList(CircleLinkList * clist){
return clist->size;
}
//判断是否为空
int IsEmpty_CircleLinkList(CircleLinkList * clist){
if (clist->size == 0)
{
return CIRCLELINKLIST_TRUE;
}
return CIRCLELINKLIST_FALSE;
}
//返回第一个元素
CircleLinkNode * Front_CircleLinkList(CircleLinkList * clist){
return clist->head.next;
}
//查找
int Find_CircleLinkList(CircleLinkList * clist, CircleLinkNode * data, COMPARENODE compare){
if (clist == NULL)
{
return - 1;
}
if (data == NULL)
{
return -1;
}
CircleLinkNode * pCurrent = clist->head.next;
int flag = -1;
for (int i = 0; i < clist->size; i++)
{
if (compare(pCurrent , data) == CIRCLELINKLIST_TRUE){
flag = i;
break;
}
pCurrent = pCurrent->next;
}
return flag;
}
//打印
void Print_CircleLinkList(CircleLinkList * clist, PRINTLINKLIST print){
if (clist == NULL){
return;
}
CircleLinkNode * pCurrent = clist->head.next;
for (int i = 0; i < clist->size * 3; i++){
if (pCurrent == &(clist->head))
{
pCurrent = pCurrent->next;
printf("-------------------------- \n");
}
print(pCurrent);
pCurrent = pCurrent->next;
}
}
//释放
void Free_CircleLinkList(CircleLinkList * clist)
{
if (clist == NULL)
{
return;
}
free(clist);
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CircleLinkList.h"
typedef struct PERSON{
CircleLinkNode node;
char name[64];
int age;
int score;
}Person;
void MyPrint(CircleLinkNode *data){
Person *p = (Person *)data;
printf("Name:%s Age:%d Score:%d\n", p->name, p->age, p->score);
}
int main(void){
//创建循环链表
CircleLinkList * clist = Init_CircleLinkList();
//创建数据
Person p1, p2, p3, p4, p5;
strcpy(p1.name, "aaa");
strcpy(p2.name, "bbb");
strcpy(p3.name, "ccc");
strcpy(p4.name, "ddd");
strcpy(p5.name, "eee");
p1.age = 10;
p2.age = 20;
p3.age = 30;
p4.age = 40;
p5.age = 50;
p1.score = 10;
p2.score = 20;
p3.score = 30;
p4.score = 40;
p5.score = 50;
Insert_CircleLinkList(clist, 100, (CircleLinkNode*)&p1);
Insert_CircleLinkList(clist, 100, (CircleLinkNode*)&p2);
Insert_CircleLinkList(clist, 100, (CircleLinkNode*)&p3);
Insert_CircleLinkList(clist, 100, (CircleLinkNode*)&p4);
Insert_CircleLinkList(clist, 100, (CircleLinkNode*)&p5);
//打印
Print_CircleLinkList(clist ,MyPrint);
//释放内存
FreeSpace_CircleLinkList(clist);
system("pause");
return 0;
}
在运行Freespace_CircleLinK(clist);时出错。
|