#include <stdio.h>
#include <stdlib.h>
/**
*企业链表,与常规链表不同是,链表数据类型和链表进行分离(解耦)
*当业务发生变化,需要新的数据类型时,不需要写新的链表
*/
//与自定义数据类型node进行对应
typedef struct Node{
struct Node *next;
}STNode_def;
//链表头结点
typedef struct CicLinklist{
struct Node head;
int size; //包含链表大小因为获取链表大小时不需要每次遍历(链表结点数量过多会消耗时间)
}STCicLinklist_def;
#define SUCCESS 0
#define FAILURE -1
//typedef void (*PRINTLIST)(STNode_def* node);
//typedef int (*CICLINKLIST_COMPARE)(STNode_def*, STNode_def*);
//循环链表初始化
STCicLinklist_def* ciclinklist_init()
{
STCicLinklist_def* list_ptr = (STCicLinklist_def*)malloc(sizeof(STCicLinklist_def));
list_ptr->head.next = &(list_ptr->head);
list_ptr->size = 0;
return list_ptr;
}
//尾插入
int ciclinklist_push_back(STCicLinklist_def* list_ptr, STNode_def* data)
{
if (list_ptr == NULL || data == NULL)
{
return FAILURE;
}
//辅助结点指针,头结点指针赋值给赋值结点
STNode_def* p = &(list_ptr->head);
while (p->next != &(list_ptr->head))
{
p = p->next;
}
p->next = data;
data->next = &(list_ptr->head);
list_ptr->size++;
return SUCCESS;
}
//头插入
int ciclinklist_push_front(STCicLinklist_def* list_ptr, STNode_def* data)
{
if (list_ptr == NULL || data == NULL)
{
return FAILURE;
}
data->next = list_ptr->head.next;
list_ptr->head.next = data;
list_ptr->size++;
return SUCCESS;
}
//根据pos位置进行插入
int ciclinklist_push_bypos(STCicLinklist_def* list_ptr, int pos, STNode_def* data)
{
if (list_ptr == NULL || data == NULL)
{
return FAILURE;
}
if (pos < 0 || pos >= list_ptr->size)
{
pos = list_ptr->size; //pos位置不合法时,默认尾部插入
printf("pos=%d\n", pos);
}
int i = 0;
STNode_def* p = &(list_ptr->head);
//指定位置插入节点,需找出前驱结点
while (p->next != &(list_ptr->head) && i<pos)
{
p = p->next;
i++;
}
data->next = p->next;
p->next = data;
list_ptr->size++;
return SUCCESS;
}
//尾删
int ciclinklist_pop_back(STCicLinklist_def* list_ptr)
{
if (list_ptr == NULL)
{
return FAILURE;
}
if (list_ptr->size == 0)
{
return FAILURE;
}
//辅助结点指针,需找到删除结点的前驱结点
int i = 0;
STNode_def* p = &(list_ptr->head);
while (p->next != &(list_ptr->head) && i < list_ptr->size-1)
{
p = p->next;
i++;
}
p->next = &(list_ptr->head);
list_ptr->size--;
return SUCCESS;
}
//头删
int ciclinklist_pop_front(STCicLinklist_def* list_ptr)
{
if (list_ptr == NULL)
{
return FAILURE;
}
if (list_ptr->size == 0)
{
return FAILURE;
}
//辅助结点指针,需找到删除结点的前驱结点
list_ptr->head.next = list_ptr->head.next->next;
list_ptr->size--;
return SUCCESS;
}
//指定pos位置删除
int ciclinklist_pop_bypos(STCicLinklist_def* list_ptr, int pos)
{
if (list_ptr == NULL)
{
return FAILURE;
}
//空链表删除也报错
if (pos < 0 || pos > list_ptr->size -1 || list_ptr->size == 0)
{
return FAILURE;
}
//辅助结点指针,需找到删除结点的前驱结点
int i = 0;
STNode_def* p = &(list_ptr->head);
while (p->next != &(list_ptr->head) && i < pos)
{
p = p->next;
i++;
}
p->next = p->next->next;
list_ptr->size--;
return SUCCESS;
}
//根据值进行遍历查找比较删除
int ciclinklist_pop_byvalue(STCicLinklist_def* list_ptr, STNode_def* value, int (*CICLINKLIST_COMPARE)(STNode_def*, STNode_def*))
{
if (list_ptr == NULL)
{
return FAILURE;
}
//空链表删除也报错
if (list_ptr->size == 0)
{
return FAILURE;
}
//辅助结点指针,需找到删除结点的前驱结点
STNode_def* p = &(list_ptr->head);
while (p->next != &(list_ptr->head))
{
if (CICLINKLIST_COMPARE(p->next, value))
{
p->next = p->next->next;
list_ptr->size--;
break;
}
p = p->next;
}
return SUCCESS;
}
//遍历链表
void ciclinklist_print(STCicLinklist_def* list_ptr, void (*PRINTLIST)(STNode_def*))
{
if (list_ptr == NULL || PRINTLIST == NULL)
{
return;
}
STNode_def* p = &(list_ptr->head);
while (p->next != &(list_ptr->head))
{
p = p->next;
PRINTLIST(p);
}
}
/**约瑟夫环问题,是一个经典的循环链表问题,
*题意是:已知 n 个人(以编号1,2,3,…,n分别表示)围坐在一张圆桌周围,
*从编号为 k 的人开始顺时针报数,数到 m 的那个人出列;
*他的下一个人又从 1 还是顺时针开始报数,数到 m 的那个人又出列;
*依次重复下去,要求找到最后出列的那个人,假设m为3
*/
void ciclinklist_ysfprint(STCicLinklist_def* list_ptr, void (*PRINTLIST)(STNode_def*), int (*CICLINKLIST_COMPARE)(STNode_def*, STNode_def*))
{
if (list_ptr == NULL || PRINTLIST == NULL)
{
return;
}
int cnt = 1;
STNode_def* p = list_ptr->head.next;
while (list_ptr->size > 1)
{
if (list_ptr->size == 1)
{
break;
}
if (p == &(list_ptr->head))
{
p = list_ptr->head.next;
}
//删除结点需要找出前一个结点
if (cnt == 2)
{
STNode_def* pnext = p->next;
if (pnext == &(list_ptr->head))
{
pnext = list_ptr->head.next;
}
PRINTLIST(pnext);
ciclinklist_pop_byvalue(list_ptr, pnext, CICLINKLIST_COMPARE);
cnt = 0;
}
p = p->next;
cnt++;
}
}
//销毁链表
void list_destory(STCicLinklist_def* list_ptr)
{
if (list_ptr == NULL)
{
return;
}
free(list_ptr);
list_ptr = NULL;
}
//自定义数据类型
typedef struct Student{
STNode_def node;
int num;
}STStudent_def;
//自定义数据输出函数
void student_info_print(STStudent_def* node)
{
printf("num=%d\n", node->num);
}
int student_compare(STNode_def* var1, STNode_def* var2)
{
STStudent_def* p1 = (STStudent_def*)var1;
STStudent_def* p2 = (STStudent_def*)var2;
if (p1->num == p2->num)
{
printf("num[%d] delete\n", p2->num);
return 1;
}
return 0;
}
int main()
{
STStudent_def p1;
STStudent_def p2;
STStudent_def p3;
STStudent_def p4;
STStudent_def p5;
STStudent_def p6;
STStudent_def p7;
STStudent_def p8;
p1.num = 1;
p2.num = 2;
p3.num = 3;
p4.num = 4;
p5.num = 5;
p6.num = 6;
p7.num = 7;
p8.num = 8;
STCicLinklist_def * list_ptr = ciclinklist_init();
ciclinklist_push_back(list_ptr, &p1);
ciclinklist_push_back(list_ptr, &p2);
ciclinklist_push_back(list_ptr, &p3);
ciclinklist_push_back(list_ptr, &p4);
ciclinklist_push_back(list_ptr, &p5);
ciclinklist_push_back(list_ptr, &p6);
ciclinklist_push_back(list_ptr, &p7);
ciclinklist_push_back(list_ptr, &p8);
ciclinklist_print(list_ptr, student_info_print);
printf("size:%d\n", list_ptr->size);
ciclinklist_ysfprint(list_ptr, student_info_print, student_compare);
ciclinklist_print(list_ptr, student_info_print);
printf("size:%d\n", list_ptr->size);
list_destory(list_ptr);
return 0;
}
数据结构 约瑟夫环问题
最新推荐文章于 2021-08-18 16:16:35 发布