数据结构C语言循环链表练习之俄罗斯轮盘赌

2023-11-09 11:20

本文主要是介绍数据结构C语言循环链表练习之俄罗斯轮盘赌,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

编译器:

/*****************************
*project :数据结构
*function :循环链表之俄罗斯赌盘
*Author :Rookie Uzz
*****************************
*copyright:2019.2.27 by UZT
****************************/

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "CListTest.h"
 4 #include <time.h>
 5 #define MAX_NUM 1000    //最大容量
 6 
 7 int Bet();
 8 
 9 int main()
10 {
11     Bet();
12     return 0;
13 }
14 
15 int Bet()
16 {
17     /** 被杀死的赌徒地址 */
18     Gambler * n = (Gambler*)malloc(sizeof(Gambler));
19     /** 被删除的结点 */
20     lineNode * nodee;
21     /** 链表删除位置 */
22     int pos = 0;
23     /** 赌徒个数 */
24     int num;
25     /** 轮数 */
26     int round = 1;
27     /** 赌徒数据 */
28     Gambler gab[MAX_NUM];
29     //先设置随机数种子,time(0)是得到当前时时间值
30     srand((int)time(NULL));
31     /** 子弹 */
32     int shoot;
33     /** 定义链表 */
34     linelist * line = (linelist*)malloc(sizeof(linelist));
35     printf("请输入赌徒个数:");
36     scanf("%d",&num);
37     //初始化链表
38     for(int i = 0;i < num;i++)
39     {
40         gab[i].id = i+1;
41     }
42     InitLine(line,gab,num);
43     printf("按任意键继续......\n");
44     getchar();
45     getchar();
46     printf("初始化后赌徒分别是:\n");
47     PrintLine(line);
48     /*
49     nodee = deleteLine(line,7);
50     printf("删除后链表为:\n");
51     PrintLine(line);
52     int n = SearchPos(line,nodee);
53     printf("该结点的位置是:%d",n);
54     */
55 
56     nodee = line -> next;
57     while(num != 1)
58     {
59         printf("\n*********************************\n");
60         pos = SearchPos(line,nodee);
61         printf("第%d轮,从%d号赌徒开始\n",round,nodee -> persons.id);
62         shoot = rand()%num + 1;
63         pos += (shoot - 1);
64         printf("枪在第 %d 次扣动扳机时会响\n",shoot);
65         nodee = deleteLine(line,pos,n);
66         printf("编号为 %d 的赌徒被杀死,剩余赌徒编号依次为:\n",n -> id);
67         PrintLine(line);
68         num--;
69         round++;
70         printf("*********************************\n");
71     }
72     printf("最终胜利的赌徒编号是:%d\n",line -> next -> persons.id);
73     free(line);
74     free(n);
75     return 0;
76 }
main.c文件
 1 #ifndef ELEMENT_H_INCLUDED
 2 #define ELEMENT_H_INCLUDED
 3 #define OK 1
 4 #define FALSE -1
 5 
 6 /** 定义赌徒 */
 7 typedef struct Gambler{
 8     int id;
 9     //char * name;
10 }Gambler;
11 
12 #endif
Element.h文件
 1 #ifndef CLISTTEST_H_INCLUDED
 2 #define CLISTTEST_H_INCLUDED
 3 #include "Element.h"
 4 
 5 /** 定义一个循环链表结点 */
 6 typedef struct lineNode{
 7     Gambler persons;     //数据域
 8     struct lineNode * next; //指针域
 9 }lineNode;
10 
11 /** 定义一个头指针 */
12 typedef struct linelist{
13     lineNode * next;
14     int length;
15 }linelist;
16 
17 /** 初始化链表 */
18 void InitLine(linelist * line,Gambler * person,int length);
19 
20 /** 插入链表 */
21 void InsertLine(linelist * line,Gambler person,int pos);
22 
23 /** 删除并返回删除节点的下一结点 */
24 lineNode * deleteLine(linelist * line,int pos,Gambler * num);
25 
26 /** 打印 */
27 void PrintLine(linelist * line);
28 
29 /** 通过结点查找位置 */
30 int SearchPos(linelist * line,lineNode * nodee);
31 
32 
33 #endif // CLISTTEST_H_INCLUDED
CListTest.h文件
  1 /*****************************
  2 *project    :数据结构
  3 *function   :循环链表之俄罗斯赌盘
  4 *Author        :rookie uzz
  5 *****************************
  6 *copyright:2019.2.27 by UZT
  7 ****************************/
  8 #include <stdio.h>
  9 #include <stdlib.h>
 10 #include "CListTest.h"
 11 
 12 /** 初始化链表 */
 13 void InitLine(linelist * line,Gambler * person,int length)//应该引入*person
 14 {
 15     line -> next = NULL;
 16     line -> length = 0;
 17     for(int  i  = 0;i < length;i++)
 18     {
 19         InsertLine(line,person[i],i+1);
 20     }
 21     printf("长度为:%d\n",line -> length);
 22 }
 23 
 24 /** 插入链表 */
 25 void InsertLine(linelist * line,Gambler person,int pos)
 26 {
 27     lineNode * node = (lineNode*)malloc(sizeof(lineNode));
 28     node -> persons = person;
 29     node -> next = NULL;
 30     if(pos == 1)
 31     {
 32         if(line -> length == 0)
 33         {
 34             line -> next = node;
 35             node -> next = node;
 36         }
 37         else
 38         {
 39             lineNode * lastNode = line -> next;
 40             for(int i = 0;i < ((line -> length) - 1);i++)
 41             {
 42                 lastNode = lastNode -> next;
 43             }
 44             node -> next = line -> next;
 45             line -> next = node;
 46             lastNode -> next = node;
 47         }
 48         line -> length++;//不要忘了加长!!!!
 49         return;
 50     }
 51     lineNode * currentNode = line -> next;
 52     for(int i = 1;i < pos - 1;i++)
 53     {
 54         currentNode = currentNode -> next;
 55     }
 56     node -> next = currentNode -> next;
 57     currentNode -> next = node;
 58     line -> length++;
 59 }
 60 
 61 /** 删除并返回删除节点的下一结点 */
 62 lineNode * deleteLine(linelist * line,int pos,Gambler * num)
 63 {
 64     lineNode * returnNode;
 65     lineNode * node = line -> next;
 66     if((pos % (line -> length)) == 1)//关键,最大bug
 67     {
 68         if(node)
 69         {
 70             *num = node -> persons;
 71             lineNode * lastNode = line -> next;
 72             for(int i = 1;i < line -> length;i++)
 73             {
 74                 lastNode = lastNode -> next;
 75             }
 76             line -> next  = node -> next;
 77             lastNode -> next = line -> next;
 78             free(node);
 79         }
 80         line -> length--;
 81         returnNode = line -> next;
 82         return returnNode;
 83     }
 84     lineNode * preNode;
 85     for(int i = 1;node&&i < pos;i++)
 86     {
 87         preNode = node;
 88         node  = node -> next;
 89     }
 90     if(node)
 91     {
 92         *num = node -> persons;
 93         returnNode = node -> next;
 94         preNode -> next = node -> next;
 95         line -> length--;
 96         free(node);
 97     }
 98     return returnNode;
 99 }
100 
101 /** 打印 */
102 void PrintLine(linelist * line)
103 {
104     if(line -> length == 0 || !line -> next)
105     {
106         printf("链表为空!\n");
107         return;
108     }
109     lineNode * node = line -> next;
110     for(int i = 0;i < line -> length;i++)
111     {
112         printf("%d号赌徒\t",node -> persons.id);
113         node = node -> next;
114     }
115     printf("\n");
116 }
117 
118 /** 通过结点查找位置 */
119 int SearchPos(linelist * line,lineNode * nodee)
120 {
121     lineNode * node = line -> next;
122     if(node == nodee)
123         return 1;
124     for(int i = 1;i < line -> length;i++)
125     {
126         node = node -> next;
127         if(node == nodee)
128         {
129             return i + 1;
130         }
131     }
132     return FALSE;
133 }
CListTest.c文件

转载于:https://www.cnblogs.com/cnlntr/p/10458877.html

这篇关于数据结构C语言循环链表练习之俄罗斯轮盘赌的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/375799

相关文章

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

Go语言中三种容器类型的数据结构详解

《Go语言中三种容器类型的数据结构详解》在Go语言中,有三种主要的容器类型用于存储和操作集合数据:本文主要介绍三者的使用与区别,感兴趣的小伙伴可以跟随小编一起学习一下... 目录基本概念1. 数组(Array)2. 切片(Slice)3. 映射(Map)对比总结注意事项基本概念在 Go 语言中,有三种主要

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

Go语言利用泛型封装常见的Map操作

《Go语言利用泛型封装常见的Map操作》Go语言在1.18版本中引入了泛型,这是Go语言发展的一个重要里程碑,它极大地增强了语言的表达能力和灵活性,本文将通过泛型实现封装常见的Map操作,感... 目录什么是泛型泛型解决了什么问题Go泛型基于泛型的常见Map操作代码合集总结什么是泛型泛型是一种编程范式,允

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

C语言小项目实战之通讯录功能

《C语言小项目实战之通讯录功能》:本文主要介绍如何设计和实现一个简单的通讯录管理系统,包括联系人信息的存储、增加、删除、查找、修改和排序等功能,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录功能介绍:添加联系人模块显示联系人模块删除联系人模块查找联系人模块修改联系人模块排序联系人模块源代码如下

Python判断for循环最后一次的6种方法

《Python判断for循环最后一次的6种方法》在Python中,通常我们不会直接判断for循环是否正在执行最后一次迭代,因为Python的for循环是基于可迭代对象的,它不知道也不关心迭代的内部状态... 目录1.使用enuhttp://www.chinasem.cnmerate()和len()来判断for