DS-006 32 数据结构:双向循环链表

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.
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Tinky2013/article/details/87284446
今日推荐