单向循环链表:
DS5CLIst2head.h:
#ifndef _CLINKLIST_
#define _CLINKLIST_ 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//======================================================================
typedef float ElemType;
// 单向循环链表结构体定义
typedef struct CLNode
{
union
{
int length;
ElemType data;
};
struct CLNode *next;
}CLNode, *CLinkList;
//======================================================================
// 单向循环链表创建头结点
CLinkList CListCreatHead(void);
// 单向循环链表创建其它结点
CLNode *CListCreatNode(void);
// 单向循环链表的初始化
CLinkList CListInit(int mode);
// 单向循环链表遍历
int CListOutPut(CLinkList pHead);
// 单向循环链表空间释放
CLNode *CListFree(CLinkList pHead);
//======================================================================
// 单向循环链表头插
int CListInsertHead(CLinkList pHead, CLNode *pNode);
// 单向循环链表尾插
int CListInsertTail(CLinkList pHead, CLNode *pNode);
//======================================================================
// 单向循环链表头删
int CListDeleteHead(CLinkList pHead);
// 单向循环链表尾删
int CListDeleteTail(CLinkList pHead);
//======================================================================
// 单向循环链表任意位置插入
// 单向循环链表任意位置删除
// 单向循环链表任意位置修改
// 单向循环链表任意位置查找
//======================================================================
// 约瑟夫环
int JosephRing(CLinkList pHead);
#endif
DS5CLIst2.c:
#include "DS5CLIst2head.h"
int main(int argc, char const *argv[])
{
#if 1
{
CLinkList pHead = CListInit(1);
#if 0
{
// 单向循环链表头删
CListDeleteHead(pHead);
}
#endif
#if 0
{
// 单向循环链表尾删
CListDeleteTail(pHead);
}
#endif
#if 1
{
// 约瑟夫环
JosephRing(pHead);
pHead = NULL;
}
#endif
CListOutPut(pHead);
pHead = CListFree(pHead);
}
#endif
return 0;
}
DS5CLIst2func.c:
/*
* @Descripttion:
* @version:
* @Author: George
* @Date: 2023-03-17 01:54:33
* @LastEditors: George
* @LastEditTime: 2023-03-17 18:31:49
*/
#include "DS5CLIst2head.h"
//======================================================================
// 单向循环链表创建头结点
// 单向循环链表创建其它结点
// 单向循环链表的初始化
// 单向循环链表遍历
// 单向循环链表空间释放
CLinkList CListCreatHead(void)
{
CLinkList pHead = (CLinkList)malloc(sizeof(CLNode));
if(NULL == pHead)
{
printf("\nCListCreatHead : Malloc failed exit.\n");
return NULL;
}
pHead->length = 0;
pHead->next = pHead;
return pHead;
}
CLNode *CListCreatNode(void)
{
CLNode *pNode = (CLNode *)malloc(sizeof(CLNode));
if(NULL == pNode)
{
printf("\nCListCreatNode : Malloc failed exit.\n");
return NULL;
}
ElemType elem = 0;
printf("PLease input elem : ");
scanf("%f", &elem);
pNode->data = elem;
pNode->next = NULL;
return pNode;
}
CLinkList CListInit(int mode)
{
CLinkList pHead = CListCreatHead();
if(NULL == pHead)
{
printf("\nCListInit : Head illegal exit.\n");
return NULL;
}
int len = 0;
printf("Please set the len : ");
Circle :
scanf("%d", &len);
if(len < 0)
{
goto Circle;
}
for(int i = 0; i < len; i++)
{
printf("Init Node[%d] : \n", i+1);
CLNode *pNode = CListCreatNode();
switch (mode)
{
case 1 :
CListInsertTail(pHead, pNode);
break;
default:
CListInsertHead(pHead, pNode);
break;
}
}
return pHead;
}
int CListOutPut(CLinkList pHead)
{
if(NULL == pHead)
{
printf("\nCListOutPut : Head illegal exit.\n");
return -1;
}
if(0 == pHead->length)
{
printf("\nCListOutPut : Length zero exit.\n");
return -2;
}
printf("\nCListOutPut : \n");
CLNode *pFlag = pHead->next;
for(int i = 0; i < pHead->length; i++, pFlag=pFlag->next)
{
printf("Node[%d] : %.2f\n", i+1, pFlag->data);
}
return 0;
}
CLNode *CListFree(CLinkList pHead)
{
if(NULL == pHead)
{
printf("\nDListFree : Head illegal exit.\n");
return NULL;
}
int len = pHead->length;
for(int i = 0; i < len; i++)
{
CListDeleteHead(pHead);
}
// free
free(pHead);
pHead = NULL;
return pHead;
}
//======================================================================
// 单向循环链表头插
// 单向循环链表尾插
int CListInsertHead(CLinkList pHead, CLNode *pNode)
{
if(NULL == pHead)
{
printf("\nCListInsertHead : Head illegal exit.\n");
return -1;
}
if(NULL == pNode)
{
printf("\nCListInsertHead : Node illegal exit.\n");
return -2;
}
// insert
pNode->next = pHead->next;
pHead->next = pNode;
pHead->length++;
return 0;
}
int CListInsertTail(CLinkList pHead, CLNode *pNode)
{
if(NULL == pHead)
{
printf("\nCListInsertTail : Head illegal exit.\n");
return -1;
}
if(NULL == pNode)
{
printf("\nCListInsertTail : Node illegal exit.\n");
return -2;
}
// find tail
CLNode *pTail = pHead;
for(int i = 0; i < pHead->length; i++, pTail=pTail->next);
// Iinsert
pNode->next = pTail->next;
pTail->next = pNode;
pHead->length++;
return 0;
}
//======================================================================
// 单向循环链表头删
// 单向循环链表尾删
int CListDeleteHead(CLinkList pHead)
{
if(NULL == pHead)
{
printf("\nCListDeleteHead : Head illegal exit.\n");
return -1;
}
if(0 == pHead->length)
{
printf("\nCListDeleteHead : Length zero exit.\n");
return -2;
}
// delete
CLNode *pDel = pHead->next;
pHead->next = pDel->next;
pHead->length--;
// free
free(pDel);
pDel = NULL;
return 0;
}
int CListDeleteTail(CLinkList pHead)
{
if(NULL == pHead)
{
printf("\nCListDeleteTail : Head illegal exit.\n");
return -1;
}
if(0 == pHead->length)
{
printf("\nCListDeleteTail : Length zero exit.\n");
return -2;
}
// find tail-prior
CLNode *pDelPrior = pHead;
for(int i = 1; i < pHead->length; i++, pDelPrior=pDelPrior->next);
// delete
CLNode *pDel = pDelPrior->next;
pDelPrior->next = pDel->next;
pHead->length--;
// free
free(pDel);
pDel = NULL;
return 0;
}
//======================================================================
// 单向循环链表任意位置插入
// 单向循环链表任意位置删除
// 单向循环链表任意位置修改
// 单向循环链表任意位置查找
//======================================================================
// 约瑟夫环
int JosephRing(CLinkList pHead)
{
if(NULL == pHead)
{
printf("\nJosephRing : Head illegal exit.\n");
return -1;
}
if(0 == pHead->length)
{
printf("\nJosephRing : Length zero exit.\n");
return -2;
}
// set step[correct]
int step = 0;
printf("Please set a step[>0] : ");
Circle :
scanf("%d", &step);
if(step <= 0)
{
goto Circle;
}
printf("Your step : %d\n", step);
// find tail[correct]
CLNode *pTail = pHead->next;
for(int i = 1; i < pHead->length; i++, pTail=pTail->next);
// init[correct]
CLNode *pOutPrior = pHead->next;
CLNode *pOut = NULL;
pTail->next = pOutPrior;
// main
while(pHead->length)// pOutPrior != NULL, !0
{
// find out-prior
for(int i = 1; i < step; i++, pOutPrior = pOutPrior->next);
pOut = pOutPrior->next;
// print
printf("%.2f\t", pOut->data);
// safety
if(pOutPrior == pOutPrior->next)
{
pOutPrior = NULL;
break;
}
// out
pOutPrior->next = pOut->next;
pHead->length--;
// free
free(pOut);
pOut = NULL;
// init
pOutPrior = pOutPrior->next;
}
// safety
pTail = NULL;
pOut = NULL;
pHead->length = 0;
free(pHead);
pHead = NULL;
return 0;
}
约瑟夫环: