数据结构 - 单循环链表:选首领(C)

来源:互联网 发布:dota2 多核优化 编辑:程序博客网 时间:2024/05/22 09:44
/*ChiefElection.c - by Chimomo*//*选首领。N个游戏者围成一圈,从第一个人开始顺序报数1,2,3。凡报到3者退出圈子,最后留在圈子里的人为首领。*/#include <stdio.h>#include <stdlib.h>typedef struct node{    /* 游戏者的编号 */    int code;    struct node * next;} NODE, * LinkList;/* 创建一个结点数为n的单循环链表,返回值为游戏编号为1的结点的指针 */LinkList createList(int n){    LinkList head, p;    int i;    /* 创建循环链表的第一个结点 */    head = (NODE *)malloc(sizeof(NODE));    if(!head)    {        printf("Memory allocation error!\n");        return NULL;    }    head->code = 1;    head->next = head;    /* 尾插法创建循环链表的其余n-1个结点 */    for(i = n; i > 1; --i)    {        p = (NODE *)malloc(sizeof(NODE));        if(!p)        {            printf("Memory allocation error!\n");            return NULL;        }        p->code = i;        p->next = head->next;        head->next = p;    }    return head;}/* 输出链表中结点的数据 */void output(LinkList head){    LinkList p;    p = head;    do    {        printf("%-4d",p->code);        p = p->next;    }    while(p != head);}/* 选首领 */void play(LinkList head, int n){    LinkList p, q;    int c = 0, k;    p = head;    c = 1;    k = n;    while(k > 1)    {        /* 当c等于2时,p指向的结点的后继即为将被删除的结点 */        if(c == 2)        {            q = p->next;            p->next = q->next;            printf("%-4d",q->code);            free(q);            c = 0;            k--;        }        c++;        p = p->next;    }    /* 输出最后留在圈子里的人的编号 */    printf("\nThe winner is [%-4d].\n", p->code);}int main(void){    LinkList head;    int n;    printf("Input the number of players: ");    scanf("%d", &n);    /* 创建单循环链表 */    head = createList(n);    if(head)    {        /* 输出单循环链表中结点的信息 */        printf("The link list is:\r\n");        output(head);        printf("\r\nThe chief election process is:\r\n");        play(head, n);    }    return 0;}/*Output:Input the number of players: 369The link list is:1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  5051  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99  100101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369The chief election process is:3   6   9   12  15  18  21  24  27  30  33  36  39  42  45  48  51  54  57  60  63  66  69  72  75  78  81  84  87  90  93  96  99  102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147 150153 156 159 162 165 168 171 174 177 180 183 186 189 192 195 198 201 204 207 210 213 216 219 222 225 228 231 234 237 240 243 246 249 252 255 258 261 264 267 270 273 276 279 282 285 288 291 294 297 300303 306 309 312 315 318 321 324 327 330 333 336 339 342 345 348 351 354 357 360 363 366 369 4   8   13  17  22  26  31  35  40  44  49  53  58  62  67  71  76  80  85  89  94  98  103 107 112 116 121125 130 134 139 143 148 152 157 161 166 170 175 179 184 188 193 197 202 206 211 215 220 224 229 233 238 242 247 251 256 260 265 269 274 278 283 287 292 296 301 305 310 314 319 323 328 332 337 341 346350 355 359 364 368 5   11  19  25  32  38  46  52  59  65  73  79  86  92  100 106 113 119 127 133 140 146 154 160 167 173 181 187 194 200 208 214 221 227 235 241 248 254 262 268 275 281 289 295 302308 316 322 329 335 343 349 356 362 1   10  20  29  41  50  61  70  82  91  101 110 122 131 142 151 163 172 182 191 203 212 223 232 244 253 263 272 284 293 304 313 325 334 344 353 365 7   23  37  5568  83  97  115 128 145 158 176 190 205 218 236 250 266 280 298 311 326 340 358 2   28  47  74  95  118 137 164 185 209 230 257 277 299 320 347 367 34  64  104 136 169 199 239 271 307 338 14  56  109155 217 259 317 361 77  149 226 290 16  124 245 352 178 331 196 88  286The winner is [43  ].Press any key to continue . . .*/
7 0