环境:gcc
目的:单向循环链表练习
功能如下:
1. 创建一个单向循环链表
2. 在单向循环链表中每隔六个存储节点删掉一个存储节点
3. 一直循环2的规则,直至剩下最后一个节点
/*************************************************************************
@Author: wanghao@Created Time : Wed 09 May 2018 06:08:28 PM PDT
@File Name: ysfh.c
@Description:
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define N 33
#define M 7
#define K 1
typedef int data_t;
typedef struct node_t
{
data_t data;
struct node_t *pnext;
}Node;
Node *creat_empty_list(void)
{
Node *p;
p = (Node *)malloc(sizeof(Node));
if(p == NULL)
{
printf("fail!\n");
return NULL;
}
p->data = 0;
p->pnext = NULL;
return p;
}
int main(int argc, const char *argv[])
{
int i;
Node *start = NULL;
Node *next = NULL;
/*Create Joseph Ring list from 1 to 33*/
start = creat_empty_list();
if(!start)
{
printf("Create head node fail!\n");
return -1;
}
start->data = 1;
next = start;
for(i = 2; i <= N; i++)
{
next->pnext = creat_empty_list();
if(!next->pnext)
{
printf("Creat list node fail!\n");
return -2;
}
next->pnext->data = i;
next = next->pnext;
}
/*Form a loop list*/
next->pnext = start;
/*Determine the location of the homicide*/
next = start;
while(next->pnext != start)
{
if(next->data == K)
{
break;
}
next = next->pnext;
}
/*The game begain the position K*/
start = next;
while((start->pnext != start))
{
for(i = 1; i < M-1; i++)
{
start = start->pnext;
}
/*Find the killed position*/
next = start->pnext;
start->pnext = next->pnext;
start = next->pnext;
printf("%d was killed\n",next->data);
free(next);
next = NULL;
sleep(1);
}
printf("%d was alive\n",start->data);
return 0;
}