日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python约瑟夫环单向循环链表_约瑟夫环的单向循环链表的实现代码

發布時間:2023/12/18 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python约瑟夫环单向循环链表_约瑟夫环的单向循环链表的实现代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/*************************************************************************

> File Name: JosephCirle.c

> Author: Wenfei6316

> Mail: wenfei6316@163.com

> Created Time: 2018年06月18日 星期一 09時42分28秒

************************************************************************/

/*約瑟夫環說明:N(N>1) 個人組成一圈,從第 num 個人開始報數(從 1 開始報數)

*當誰報到數字 count 時將執行槍決,然后繼續從后面一位開始接著循環,

*直到最后只剩下一個人,利用單向循環鏈表模擬出每次被殺的人以及最后存活的人*/

#include

#include

#include

typedef enum{false, true}bool;

typedef int data_t;

typedef struct Node

{

data_t data;

struct Node *next;

}JosephNode, *JosephLise;

JosephLise CreateList(int num);

void PrintList(JosephLise cyclelist);

void KillGame(JosephLise cyclelist, int num, int killer);

int main(int argc, const char *argv[])

{

int count = 10;

int num = 8;

int killer = 15;

JosephLise josephcirle;

josephcirle = CreateList(count);

PrintList(josephcirle);

printf("%d is beginning!\n", num);

printf("Calling %d will be killed!\n", killer);

KillGame(josephcirle, num, killer);

return 0;

}

JosephLise CreateList(int num)

{

int i;

JosephLise p, q, s;

if (num < 2)

{

printf("It is insignificance!\n");

exit(EXIT_FAILURE);

}

p = (JosephLise)malloc(sizeof(JosephNode));

if (p == NULL)

{

perror("Create JosephCirle failed!\n");

exit(EXIT_FAILURE);

}

p->data = 1;

p->next = NULL;

q = p;

for (i=2; i<=num; i++)

{

s = (JosephLise)malloc(sizeof(JosephNode));

if (s == NULL)

{

perror("Create JosephCirle failed!\n");

exit(EXIT_FAILURE);

}

s->data = i;

q->next = s;

q = s;

}

q->next = p;

return p;

}

void PrintList(JosephLise cyclelist)

{

bool count = false;

JosephLise p, q;

if (cyclelist == NULL)

{

printf("cyclelist is NULL!\n");

return ;

}

p = cyclelist;

while (!count)

{

printf("%2d ", p->data);

if ((p=p->next) == cyclelist)

count = true;

}

printf("\n");

}

void KillGame(JosephLise cyclelist, int num, int killer)

{

int i;

JosephLise p, q;

if (cyclelist == NULL)

{

printf("Joseph Cirle is error!\n");

return ;

}

if (num<2 || killer<2)

{

printf("It is insignificance!\n");

return ;

}

p = cyclelist;

for (i=1; i

p = p->next;

while (p->next != p)

{

for (i=1; i

p = p->next;

q = p->next;

printf("%d is killed!\n", q->data);

p->next = q->next;

free(q);

q = NULL;

}

printf("Congratulations, you are free, No%d!\n", p->data);

free(p);

return ;

}

總結

以上是生活随笔為你收集整理的python约瑟夫环单向循环链表_约瑟夫环的单向循环链表的实现代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。