其他分享
首页 > 其他分享> > 约瑟夫环问题 --链表 C语言

约瑟夫环问题 --链表 C语言

作者:互联网

总共有m个人在圆桌上,依次报名,数到第n个数的人退出圆桌,下一个由退出人下一个开始继续报名,循环直到最后一个停止将编号输出

#include <stdio.h>
#include <stdlib.h>

typedef struct Head * PHead;
typedef struct Node * PNode;

struct Head{
PNode Next;
int length;
};

struct Node {
int data;
PNode Next;
//PNode Prev;
};

 

PHead creat_clist()
{
PHead TempList ;
TempList = (PHead)malloc(sizeof(struct Head));
if(TempList == NULL)
{
printf("\nInitial failed,can't malloc the head\n");
return TempList;
}
TempList -> length = 0;
TempList -> Next = NULL;
return TempList;
}

void Insert_List(PHead CList,int val,int pos)
{
PNode TempNode;
//printf("enter insert func \n");
TempNode = (PNode)malloc(sizeof(struct Node));
TempNode -> data = val;
//printf("debug..\n");

if(NULL == TempNode)
{
printf("initial Node failed\n");
return ;
}
if(CList -> Next == NULL)//当前链表为空
{
CList -> Next = TempNode;
TempNode -> Next = TempNode;//此时最后一个节点的指针指向自己本身
CList -> length ++;//长度自加一

//TempNode -> Prev = NULL;
}
else if (1 == pos)//插入到链表头部
{
PNode tmp_node = CList -> Next;//
TempNode -> Next = tmp_node;//
while (tmp_node -> Next != CList -> Next)
{
tmp_node = tmp_node->Next;
}
tmp_node -> Next = TempNode;
//printf("delete num is :%d\n",tmp_node -> Next);
CList -> Next = TempNode;
CList -> length++;
}
else
{
PNode tmp_Node = CList -> Next;
for(int circle = 1;circle < pos - 1;circle++)
{
tmp_Node = tmp_Node -> Next;
}
TempNode -> Next = tmp_Node -> Next;
tmp_Node -> Next = TempNode;
CList -> length ++ ;
}
return ;
}

PNode pop_List(PHead CList,int pos)
{
printf("CList length is %d,pop number is : %d\n",CList -> length,pos);
if(pos <= 0)
{
printf("pos select error!\n");
exit(1);
}
if(1 == pos)
{
PNode tmp_node = CList -> Next; // 用于作为最后一个的判断和返回的参数
printf("delete num is : %d\n",(CList -> Next) -> data);
PNode Temp_Node = (CList -> Next) -> Next;//
while(Temp_Node -> Next != tmp_node)
{
//printf("skip data is :%d\n",Temp_Node -> data);
Temp_Node = Temp_Node -> Next;
}
CList -> Next = tmp_node -> Next;
Temp_Node -> Next = CList -> Next;
CList -> length -- ;
return tmp_node;
}
else if(pos == CList->length)
{
printf("pos == length\n");
PNode Temp_Node = CList->Next;
for(int circle = 0;circle < pos - 1;circle++)
{
//printf("skip data is : %d\n",Temp_Node -> data);
Temp_Node = Temp_Node -> Next;
}
printf("delete num is : %d\n",(Temp_Node -> Next) -> data);
Temp_Node -> Next = CList -> Next;
CList -> length--;
}
else
{
//printf("normal\n");
PNode Temp_Node = CList -> Next;
for(int circle = 1;circle < pos - 1; circle++)
{
Temp_Node = Temp_Node -> Next;
}
PNode tmp_node = Temp_Node -> Next;
Temp_Node -> Next = tmp_node -> Next;
printf("delete num is : %d\n",tmp_node -> data);
CList -> length --;
printf("length is : %d\n",CList -> length);
return tmp_node;
}
}

void freeClist(PHead CList)
{
PNode Temp_Node = CList -> Next;
for(int circle = 0; circle < CList -> length; circle++)
{
PNode tmp_node = Temp_Node;
Temp_Node = Temp_Node -> Next;
free(tmp_node);
}
free(CList);
printf("release successfully!\n");
return ;
}

void show_CList(PHead CList)
{
PNode temp_node = CList -> Next;
//while(temp_node -> Next != CList -> Next)
for(int circle = 0;circle < CList -> length; circle++)
{
printf("%5d",temp_node -> data);
temp_node = temp_node -> Next;
}
printf("\n");
}

 

int main()
{
PHead CList = creat_clist();
int num ;
int length;
printf("请输入约瑟夫环长度: \n");
scanf("%d",&length);
printf("\n请输入筛选的序号:\n");
scanf("%d",&num);
for(int circle = 0;circle < length; circle ++)
{
Insert_List(CList,circle+1,circle+1);
}
printf( "Original: \n" );
show_CList(CList);
//show_CList( );
printf("length:%d \n",CList -> length);

PNode tmp_node = CList -> Next;
while(CList -> length != 1)
{

for (int circle = 1;circle < num - 1;circle++)
{
tmp_node = tmp_node ->Next;
}
if(tmp_node -> Next == CList -> Next)
{
printf("delete data is : %d\n",(CList -> Next) -> data);
//free(tmp_node -> Next);
tmp_node -> Next = (tmp_node -> Next) -> Next;
CList -> Next = tmp_node -> Next;
CList -> length --;
}
else
{
printf("delete data is : %d \n",(tmp_node -> Next) -> data);
//free(tmp_node -> Next);
tmp_node -> Next = (tmp_node -> Next) -> Next;
CList -> length --;
}
tmp_node = tmp_node -> Next;
}
printf("Last is:\n");
show_CList(CList);
freeClist(CList);
return 0;
}

标签:tmp,node,C语言,约瑟夫,链表,CList,printf,Next,Node
来源: https://www.cnblogs.com/Jetson-xie/p/10472035.html