// Battle Royale
// A Linked List demo that simulates a list of
// players in a battle royale style game

// We want the ability to build a list
// print out all the players currently in the game
// and knock out individual players

// Later we're going to add players in alphabetical order

// Marc Chee (cs1511@cse.unsw.edu.au), July 2020

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

#define MAX_NAME 100

// Struct(s)
struct player {
    struct player *next;
    char name[MAX_NAME];
};

// Functions
struct player *createPlayer(char inputName[MAX_NAME], struct player *inputNext);
int printPlayers(struct player *playerList);
struct player *insertAfter(char inputName[MAX_NAME], struct player *insertPos);
struct player *insertAlpha(char inputName[MAX_NAME], struct player *head);
struct player *removePlayer(char inputName[MAX_NAME], struct player *head);
void freeList(struct player *head);

int main(void) {
    struct player *head = NULL;
    head = insertAlpha("Chicken", head);
    head = insertAlpha("Marc", head);
    head = insertAlpha("Frankie", head);
    
    // Game Loop
    while (head->next != NULL) {
        printPlayers(head);        
        printf("Who was knocked out?\n");
        char koName[MAX_NAME];
        scanf("%s", koName);
        head = removePlayer(koName, head);
    }
    printPlayers(head);
    printf("There's only one player left, they win!\n");
    freeList(head);
    
    return 0;
}

// Creates a struct player variable in allocated memory
// uses the given inputName and inputNext to populate
// the fields of the struct
// Returns a pointer to the memory that was allocated
struct player *createPlayer(char inputName[MAX_NAME], struct player *inputNext) {
    struct player *p = malloc(sizeof(struct player));
    strcpy(p->name, inputName);
    p->next = inputNext;
    return p;
}

// Print out the names of all the players in playerList
// Returns how many players were printed out
int printPlayers(struct player *playerList) {
    printf("Players currently in the game:\n");
    int total = 0;
    struct player *current = playerList;
    while (current != NULL) {
        fputs(current->name, stdout);
        printf("\n");
        total++;
        current = current->next;
    }
    return total;
}

// Insert a player into a list as the next node after insertPos
// This will not remove any elements from the list
// Returns a pointer to the inserted node
struct player *insertAfter(char inputName[MAX_NAME], struct player *insertPos) {    
    struct player *newPlayer = createPlayer(inputName, NULL);
    if (insertPos != NULL) {
        // insert after a node that exists
        newPlayer->next = insertPos->next;
        insertPos->next = newPlayer;
    }
    return newPlayer;
}

// Insert a player into a list in the correct alphabetical order
// position
// This assumes that the list is already in alphabetical order
// Returns a pointer to the head of the list (it may have changed)
struct player *insertAlpha(char inputName[MAX_NAME], struct player *head) {
    struct player *current = head;
    struct player *prev = NULL;
    // Find the position that we want to insert into
    // Check for when we should be before something alphabetically
    while (current != NULL && strcmp(inputName, current->name) > 0) {
        prev = current;
        current = current->next;
    }
    
    struct player *newHead = head;    
    if (prev == NULL) {
        // inserting at the head of the list
        newHead = createPlayer(inputName, head);
    } else {
        // inserting anywhere else in the list
        insertAfter(inputName, prev);
    }        
    return newHead;
}

// Find a player in the list (head) that matches
// the inputName. Remove them from the list.
// Returns a pointer to the head of the list (it may have changed)
struct player *removePlayer(char inputName[MAX_NAME], struct player *head) {
    // Loop through the list looking for a matching name
    struct player *current = head;
    struct player *prev = NULL;
    while (current != NULL && strcmp(inputName, current->name) != 0) {
        prev = current;
        current = current->next;
    } // current now aims at the node we want to remove or NULL
    
    // if we found a matching name, remove it from the list
    struct player *newHead = head;
    if (current != NULL) {
        // we definitely found a match
        if (prev == NULL) {
            // we're trying to remove the head of the list
            newHead = head->next;
            free(head);
        } else {
            // we're removing something that isn't the head
            prev->next = current->next;
            free(current);
        }
    }
    
    return newHead;
}

// Loop through an entire list, freeing all the nodes
void freeList(struct player *head) {
    struct player *prev = NULL;
    while (head != NULL) {
        prev = head;
        head = head->next;
        free(prev);
    }
}


Resource created Thursday 23 July 2020, 06:42:55 PM.

file: battleRoyale.c


Back to top

COMP1511 20T2 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G