You are on page 1of 15

Name: Sweta Kumari Roll number: 2006088

Course Code: CSL3401 Mini Project [Team -19]

Q . 1 :- In the children’s game “hot potato,” a group of n children sit in a


circle passing an object, called the “potato,” around the circle (say in a
clockwise direction). The children continue passing the potato until a leader
rings a bell, at which point the child holding the potato must leave the
game, and the other children close up the circle. This process is then
continued until there is only one child remaining, who is declared the
winner. Using any two different suitable data structures, describe and
implement this game (two implementations, one with the first chosen
data structure and second one with the second chosen data
structure). Suppose the leader always rings the bell immediately after
the potato has been passed k times. (Determining the last child remaining
in this variation of hot potato is known as the Josephus problem). What is
the running time of your method if you implement with the chosen first data
structure and terms of n and k? What is the running time of your method if
you implement with the chosen second data structure and terms of n and
k?

:-

METHOD-1 :-
#include <stdlib.h>

#include <stdio.h>

void mainMenu();

struct Node // creating node of list

int data;

struct Node *next;


} * Head;

void create(int n) // create list

int i = 1;

struct Node *t, *last;

Head = (struct Node *)malloc(sizeof(struct Node));

Head->data = i;

Head->next = Head;

last = Head->next;

for (i = 2; i <= n; i++)

t = (struct Node *)malloc(sizeof(struct Node));

t->data = i;

t->next = last->next;

last->next = t;

last = t;

void Display(struct Node *h) // display output

do

printf("%d ", h->data);

h = h->next;
} while (h != Head);

printf("\n");

int Length(struct Node *p) // length calculation

int len = 0;

do

len++;

p = p->next;

} while (p != Head);

return len;

int Delete(struct Node *p, int index) // deleting node

struct Node *q;

int i, x;

if (index < 0)

return -1;

if (index == 1)

while (p->next != Head)

p = p->next;

x = Head->data;

p->next = Head->next;

free(Head);
Head = p->next;

else

for (i = 0; i < index - 2; i++)

p = p->next;

q = p->next;

Head = p->next = q->next;

x = q->data;

free(q);

return x;

void play(struct Node *p, int k)

int i = 1;

while (Length(Head) > 1)

printf("Welcome to level %d\n", i++);

printf("Player eliminated : %d\n", Delete(Head, k));

printf("Player(s) remaining : ");

Display(Head);

printf("Press Enter to level up.\n");

getchar();

}
printf("The winner of this game is ");

Display(Head);

void gameRules()

printf("GAME RULES\n");

printf("1. It is a multi-player game. Player needs to be more than 1.\n");

printf("2. Total number of levels will be one less than the total number of players.\n");

printf("3. In each level one player will be eliminated based on the value of k
(Josephus Problem).\n");

printf("4. The last remaining player will be declared as the winner.\n");

printf("Press Enter key to go back to main menu.\n");

getchar();

mainMenu();

void credits()

printf("CREDITS\n");

printf("2006030 Ashish Alok\n");

printf("2006031 Abhishek chandra\n");

printf("2006057 Anand Kumar\n");

printf("2006080 Ravi Shankar\n");

printf("2006079 Gautam Babita\n");

printf("2006088 Sweta Kumari\n");

printf("Press Enter key to go back to main menu.\n");


getchar();

mainMenu();

void gameSetup()

int n, k;

printf("Enter the number of participants for the game : ");

scanf("%d", &n);

printf("Enter the value of k : ");

scanf("%d", &k);

printf("Thank you for your response.\n");

printf("Press Enter key to begin the game");

getchar();

create(n);

getchar();

printf("Players Playing : ");

Display(Head);

printf("Press Enter to continue\n");

getchar();

play(Head, k);

void intro()

{
printf("__________________________________");

printf("__________________________________");

printf("WELCOME TO HOT-POTATO GAME\n");

printf("__________________________________");

printf("__________________________________");

printf("Play With Friends And Enjoy The Time Spent\n");

printf("Press Enter to continue\n");

getchar();

void mainMenu()

int num = 0;

printf("Main Menu\n");

printf("Press 1 to start the game.\n");

printf("Press 2 to know the game rules.\n");

printf("Press 3 to know the credits.\n");

printf("Press 4 to exit the game.\n");

printf("Enter the key of your choice : ");

scanf("%d", &num);

if (num == 1)

gameSetup();

else if (num == 2)

gameRules();

else if (num == 3)

credits();
else if (num == 4)

return;

else

printf("Invalid response!");

printf("\n");

printf("Press Enter key to go back to main menu.\n");

getchar();

mainMenu();

int main()

intro();

mainMenu();

return 0;

}
METHOD-2 :-
#include <stdio.h>

#include <stdlib.h>

int length = 0, *number, k;

void delete (int *number, int startpos) // deleting by shifting it to the last

int i;

for (i = startpos; i < length; i++)

number[i] = number[i + 1];

void play(int *number, int startpos) // O()

if (length == 1)

printf("The winner is %d \n", number[0]);

return;

else

startpos = ((startpos + k - 1) % length); // updating position


printf("Eliminated player is %d \n", number[startpos]);

length--;

delete (number, startpos);

play(number, startpos);

void create(int n) // creating dynamically array

int i;

number = (int *)malloc(n * sizeof(int));

for (i = 0; i < n; i++)

number[i] = i + 1;

printf("Players Playing : ");

for (i = 0; i < n; i++)

printf("%d ", number[i]);

length++;

printf("\n");

printf("Press Enter to continue\n");

getchar();

}
int main()

int n;

printf("WELCOME TO HOT-POTATO GAME\n");

printf("Play With Friends And Enjoy The Time Spent\n");

printf("Press Enter to continue\n");

getchar();

printf("Enter the number of participants for the game : ");

scanf("%d", &n);

printf("Enter the value of k : ");

scanf("%d", &k);

printf("Thank you for your response.\n");

printf("Press Enter key to begin the game\n");

getchar();

create(n);

int startpos = 0;

play(number, startpos);

return 0;

}
1. What is the running time of your method if you
implement with the chosen first data structure
and terms of n and k?
 O(n^2)
2. What is the running time of your method if you
implement with the chosen second data structure
and terms of n and k?
 O(n^2)

You might also like