数据结构教程 | 双向循环链表

Code Implementation of Double Circular Link list


1 Construction

1.1 Construct the link list

typedef struct line{
    struct line * prior;
    int data;
    struct line * next;
}line;

Double circular link list can be considered as an improvement of double link list in some cases. The only difference is that the list has a double-end-to-end connection.

1.2 Initialization

line* initLine(line * head){
    head=(line*)malloc(sizeof(line));
    head->prior=NULL;
    head->next=NULL;
    head->data=1;
    line * list=head;
    for (int i=2; i<=3; i++) {
        line * body=(line*)malloc(sizeof(line));
        body->prior=NULL;
        body->next=NULL;
        body->data=i;
        list->next=body;
        body->prior=list;
        list=list->next;
    }
    // Bipolarly connect the head and tail
    list->next=head;
    head->prior=list;
    return head;
}

1.3 Check the code

void display(line * head){
    line * temp=head;
    while (temp->next!=head) {		// If point to 'head' means to the last node of the list
        if (temp->next==NULL) {
            printf("%d\n",temp->data);
        }else{
            printf("%d->",temp->data);
        }
        temp=temp->next;
    }
    printf("%d",temp->data);
}
int main() {
    line * head=NULL;
    head=initLine(head);
    display(head);
    return 0;
}

Output:
在这里插入图片描述

2 Russian Roulette

Rules of the game: n participants will form a ring, Loaded a bullet to revolver each time, and randomly turn around the wheel. Started from the first person, participants take turns to take the gun. The person who gets shot quits the game, and the next person starts the next round as the first person. The last person will be the winner.
Requirements: simulation roulette game rules, find the final winner of the game. By using chain storage structure we can solve the problem.

Complete code with C:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct line{
    int No;
    struct line * next;
}line;

// Initialize the circular link list according to the total number of people
void initLine(line ** head, int n){
	*head=(line*)malloc(sizeof(line));
	(*head)->next=NULL;
	(*head)->No=1;
	line * list=*head;
	for (int i=1;i<n;i++){
		line * body=(line*)malloc(sizeof(line));
		body->next=NULL;
		body->No=i+1;
		list->next=body;
		list=list->next;
	}
	list->next=*head; // Become a loop
}
void display(line * head){
    line * temp=head;
    while (temp->next!=head) {
        printf("%d ",temp->No);
        temp=temp->next;
    }
    printf("%d\n",temp->No);
}
int main() {
    line * head=NULL;
    srand((int)time(0));
    int n,shootNum,round=1;
    printf("The total number of person:");
    scanf("%d",&n);
    initLine(&head,n);
    line* lineNext=head;			// Note the start position of every loop
    while (head->next!=head) {		// When only one node left(The head node), break the loop
        printf("Round %d start,from person %d ,",round,lineNext->No);
        shootNum=rand()%n+1;
        printf("The gunshot will be at number %d \n",shootNum);
        line *temp=lineNext;
        // Find the last node of the deleted node
        for (int i=1; i<shootNum-1; i++) {
            temp=temp->next;
        }
        // Delete the node
        printf("No. %d out, the remain persons are:\n",temp->next->No);
        line * del=temp->next;
        temp->next=temp->next->next;
        if (del==head) {
            head=head->next;
        }
        free(del);
        display(head);
        lineNext=temp->next;		// The place where the new loop start
        round++;
    }
    printf("The final succeed person is:%d\n",head->No);
    return 0;
}

Output:
We assumed that there are 6 participants and we randomly put the bullet into the gun.
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值