【C++】【五】循环链表
本站寻求有缘人接手,详细了解请联系站长QQ1493399855
数据结构:具体的高效有序的管理内存的方法。
- 链表:数据结构的一种 节点:每一块内存 每一个节点可以是裸指针 也可以是结构体 ,结合企业链表的思路可以将类型强转,完成高效的访问。
main.cc
// 单向循环链表.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include<stdlib.h>
#include <iostream>#define CIRCLELINKLIST_TRUE 1
#define CIRCLELINKLIST_FALSE 0typedef struct clinknode {struct clinknode* next;
}clinknode;typedef struct circlelinkist {clinknode head;int size;
}circlelinkist;typedef int(*COMPARE)(clinknode*, clinknode*);typedef void(*PRINTNODE)(clinknode*);circlelinkist* Init_circlelinkist() {circlelinkist* clist = (circlelinkist*)malloc(sizeof(circlelinkist));clist->head.next = &(clist->head);clist->size = 0;return clist;
}void Insert_circlelinkist(circlelinkist* clist, int pos, clinknode* data) {if (clist == NULL) {return;}if (data == NULL) {return;}if (pos<0 || pos>clist->size) {pos = clist->size;}//辅助指针变量clinknode* pcur = &(clist->head);for (int i = 0; i < pos; ++i) {pcur = pcur->next;}data->next = pcur->next;pcur->next = data;clist->size++;
}clinknode* Front_circlelinkist(circlelinkist* clist) {return clist->head.next;
}void RemoveByPos_circlelinkist(circlelinkist* clist, int pos) {if (clist == NULL) {return;}if (pos < 0 || pos >= clist->size) {return;}clinknode* pcur = &(clist->head);for (int i = 0; i < pos; ++i) {pcur = pcur->next;}clinknode* pnext = pcur->next;pcur->next = pnext->next;clist->size--;
}void RemoveByValue_circlelinkist(circlelinkist* clist, clinknode* data, COMPARE compare) {if (clist == NULL) {return;}if (data == NULL) {return;}clinknode* pper = &(clist->head);clinknode* pcur = &(clist->head);for (int i = 0; i < clist->size; ++i) {if (compare(pcur, data) == CIRCLELINKLIST_TRUE) {pper->next = pcur->next;break;}pper = pcur;pcur = pcur->next;}clist->size--;
}int Size_circlelinkist(circlelinkist* clist) {if (clist->size == 0) {return CIRCLELINKLIST_TRUE;}return CIRCLELINKLIST_FALSE;
}int IsEmpty_circlelinkist(circlelinkist* clist) {if (clist->size == 0) {return CIRCLELINKLIST_TRUE;}return CIRCLELINKLIST_FALSE;
}int Find_circlelinkist(circlelinkist* clist, clinknode* data, COMPARE compare) {if (clist == NULL) {return CIRCLELINKLIST_FALSE;}if (data == NULL) {return CIRCLELINKLIST_FALSE;}clinknode* pcur = clist->head.next;int flag = -1;for (int i = 0; i < clist->size; ++i) {if (compare(pcur, data) == CIRCLELINKLIST_TRUE) {flag = i;break;}pcur = pcur->next;}return flag;
}void Print_circlelinkist(circlelinkist* clist, PRINTNODE print) {if (clist == NULL) {return;}clinknode* pcur = clist->head.next;//for (int i = 0; i < clist->size * 3; ++i)for (int i = 0; i < clist->size; ++i) {/* if (pcur == &(clist->head)) {pcur = pcur->next;printf("========================
");}*/print(pcur);pcur = pcur->next;}
}void Free_circlelinkist(circlelinkist* clist) {if (clist == NULL) {return;}
}typedef struct PERSON {clinknode node;char name[64];int age;
}person;void myprint(clinknode* data) {person* p = (person*)data;printf("name:%s, age:%d
", p->name, p->age);
}int mycompare(clinknode* data1, clinknode* data2) {person* p1 = (person*)data1;person* p2 = (person*)data2;if (strcmp(p1->name,p2->name) == 0 && p1->age == p2->age) {return CIRCLELINKLIST_TRUE;}return CIRCLELINKLIST_FALSE;
}int main()
{circlelinkist* list = Init_circlelinkist();person p1, p2, p3, p4, p5;strcpy_s(p1.name, "aaa");strcpy_s(p2.name, "bbb");strcpy_s(p3.name, "ccc");strcpy_s(p4.name, "ddd");strcpy_s(p5.name, "eee");p1.age = 10;p2.age = 20;p3.age = 30;p4.age = 40;p5.age = 50;Insert_circlelinkist(list, 100, (clinknode*)&p1);Insert_circlelinkist(list, 100, (clinknode*)&p2);Insert_circlelinkist(list, 100, (clinknode*)&p3);Insert_circlelinkist(list, 100, (clinknode*)&p4);Insert_circlelinkist(list, 100, (clinknode*)&p5);Print_circlelinkist(list, myprint);printf("---------------------------
");person pdel;strcpy_s(pdel.name, "ccc");pdel.age = 30;RemoveByValue_circlelinkist(list,(clinknode*)&pdel,mycompare);Print_circlelinkist(list, myprint);printf("---------------------------
");Free_circlelinkist(list);system("pause");return 0;
}