// A Battle Royale player tracker
// This is a linked list demo

// Marc Chee (marc.chee@unsw.edu.au), July 2019

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

#define MAX_NAME_LENGTH 1000

struct player {
    char name[MAX_NAME_LENGTH];
    struct player *next;
};

struct player *createPlayer(
    char inputName[MAX_NAME_LENGTH], struct player *inputNext
);
void printPlayers(struct player *p);
struct player *insert(char inputName[MAX_NAME_LENGTH], struct player *listPlayer);
struct player *insertAlpha(
    char inputName[MAX_NAME_LENGTH], struct player *listHead
);
struct player *removePlayer(
    char inputName[MAX_NAME_LENGTH], struct player *listHead
);

int main(void) {
    struct player *head = NULL;
    printPlayers(head);
    putchar('\n');
    head = insertAlpha("Marc", head);
    printPlayers(head);
    putchar('\n');
    head = insertAlpha("AndrewB", head);
    printPlayers(head);
    putchar('\n');
    head = insertAlpha("Batman", head);
    printPlayers(head);
    putchar('\n');
    head = insertAlpha("Leonardo", head);    
    printPlayers(head);
    putchar('\n');
    head = insertAlpha("Tom", head); 
    printPlayers(head);
    return 0;
}

// Mallocs and creates a node using the inputName and inputNext pointer
// returns a pointer to the newly created node
struct player *createPlayer(
    char inputName[MAX_NAME_LENGTH], struct player *inputNext
) {
    struct player *n = malloc(sizeof(struct player));
    
    if (n == NULL) {
        printf("Malloc failed, unable to create player. Program will exit.\n");
        exit(1);
    }
    
    strcpy(n->name, inputName);
    n->next = inputNext;
    return n;
}

void printPlayers(struct player *p) {
    while(p != NULL) {
        fputs(p->name, stdout);
        putchar('\n');
        p = p->next;
    }
}

// Insert a new node after listPlayer
// return a pointer to the listPlayer (or the new Node if the list was empty)
struct player *insert(char inputName[MAX_NAME_LENGTH], struct player *listPlayer) {
    struct player *p = createPlayer(inputName, NULL);
    
    if (listPlayer != NULL) {
        p->next = listPlayer->next;
        listPlayer->next = p;
    } else {
        // listPlayer == NULL, which means it's an empty list
        listPlayer = p;
    }
    return listPlayer;
}

// Insert an element into a list in its correct alphabetical location (using
// strcmp). Return the head of the list (even if it has changed)
struct player *insertAlpha(
    char inputName[MAX_NAME_LENGTH], struct player *listHead
) {
    struct player *prev = NULL;
    struct player *after = listHead;
    while (after != NULL && strcmp(inputName, after->name) > 0) {        
        prev = after;
        after = after->next;
    }
    struct player *insertionPoint = insert(inputName, prev);
    if (prev == NULL) {
        // insertion happened at the start of the list
        listHead = insertionPoint;
        listHead->next = after;
    }
    return listHead;
    
}

// Find a player that matches the inputName and remove it from the list
// return the head of the list (even if it has changed)
struct player *removePlayer(
    char inputName[MAX_NAME_LENGTH], struct player *listHead
) {
    struct player *prev = NULL;
    struct player *r = listHead;
    while (r != NULL && strcmp(inputName, r->name) != 0) {
        prev = r;
        r = r->next;
    }
    if (r != NULL) {
        if (prev == NULL) {
            // r is the first element of the list
            listHead = r->next;
        } else {
            prev->next = r->next;
        }
        free(r);
    }
    return listHead;
}



Resource created Tuesday 23 July 2019, 08:47:26 PM.

file: battleLec.c


Back to top

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