Menu
Coddy logo textTech

Practice #4

Lesson 12 of 12 in Coddy's Queue - Data Structures Series #2 course.

The next challenges are designed to use a queue. The Queue data structure is already provided for you — use it!

challenge icon

Challenge

Easy

Write a function named hotPotato that gets a list of names and a number of tosses, and simulates the game of hot potato: pass the potato tosses times (each pass moves the front person to the back), then the person now at the front is eliminated. Repeat until one person remains, and return their name.

Use the provided Queue to solve this problem!

Try it yourself

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "solution.h"

int main() {
    char line[4096];
    char** names = (char**)malloc(sizeof(char*) * 1000);
    int n = 0;
    if (fgets(line, sizeof(line), stdin)) {
        char* tok = strtok(line, " \t\r\n");
        while (tok) { names[n++] = strdup(tok); tok = strtok(NULL, " \t\r\n"); }
    }
    int tosses = 0;
    scanf("%d", &tosses);
    printf("%s\n", hotPotato(names, n, tosses));
    return 0;
}

All lessons in Queue - Data Structures Series #2