1. 程式人生 > >數據結構C語言循環鏈表練習之俄羅斯輪盤賭

數據結構C語言循環鏈表練習之俄羅斯輪盤賭

lis c語言 sed time tchar 分享圖片 技術分享 頭指針 node

編譯器:

技術分享圖片

/*****************************
*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文件

數據結構C語言循環鏈表練習之俄羅斯輪盤賭