// 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);
void printPlayers(struct player *playerList);

int main(void) {
    struct player *head = createPlayer("Chicken", NULL);
    head = createPlayer("Marc", head);
    head = createPlayer("Dean", head);
    printPlayers(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
void printPlayers(struct player *playerList) {
    struct player *current = playerList;
    while (current != NULL) {
        fputs(current->name, stdout);
        printf("\n");
        current = current->next;
    }
}




Resource created Thursday 16 July 2020, 06:26:52 PM.

file: battleRoyale.c


Back to top

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