#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//定義結點
typedef struct LinkList
{
int data;
struct LinkList *next;
}node;
//初始化鏈表
void Initialise(node **pNode,int item)//**pNode 指向指標的指標 指向的物件pNode還是指標
{
node *tmp;
node *target;
int i=1;
while(i <= item)
{
if(* pNode == NULL)
{
*pNode = (node *)malloc(sizeof(LinkList));
if(!(* pNode))
{
exit(0);//退出
}
(*pNode)->data = i;
(*pNode)->next = *pNode ;
i++;
}
else
{
target = *pNode;
while(target->next != *pNode)
{
target = target->next ;
}
tmp = (node*)malloc(sizeof(LinkList ));
if(! tmp)
{
exit(0);
}
tmp->data = i;
i++;
tmp->next = *pNode ;
target->next = tmp;
}
}
}
//遍歷鏈表
void Traversal(node *pNode)
{
node *tmp;
tmp = pNode;
printf("\n\t\t*****************************鏈表元素*********************************\n\n");
do
{
printf("\t%d",tmp->data );
tmp = tmp->next ;
}while(tmp->next != pNode);
printf("\t%d",tmp->data );
puts("");
}
//洗掉結點(殺人)
void DeleteNode(node **pNode,int x,int y)
{
int people = 1;
int Key[80];
printf("請依次輸入每人的密碼(使用空格隔開):\n\n");
for(int i=1;i<=y;++i)
{
scanf("%d",&Key[i]);
}
printf("出列順序:\n\n");
node *target;
target = *pNode ;
while(people <= y)
{
node *tmp;
if(people != 1)
{
target = target->next ;
}
if(x != 1)
{
for(int i=1;i<x-1;++i)
{
target = target->next ;
}
tmp = target->next ;
printf("%d\t",tmp->data );//列印洗掉結點標號
x = Key[tmp->data ];
target->next = tmp ->next ;
free(tmp);
}
else
{
printf("%d\t",target->data );
}
people++;
}
}
int main()
{
printf("Designer by MaLong\n");
printf("\n\t\t**********************************************************************\n");
printf("\t\t* *\n");
printf("\t\t* *\n");
printf("\t\t********************歡迎來到約瑟夫殺人游戲****************************\n");
printf("\t\t* *\n");
printf("\t\t* *\n");
printf("\t\t**********************************************************************\n\n\n\n");
int x,y;
printf("請輸入參與游戲的人數:\n");
scanf("%d",&y);
printf("請輸入初始密碼:\n");
scanf("%d",&x);
node *head = NULL;
Initialise (&head,y);
Traversal (head);
DeleteNode (&head,x,y);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/19105.html
標籤:基礎類
上一篇:求方法與思路
下一篇:C++Builder安裝時報錯!
