// A simple program for tracking the players in a game
// A demonstration of some of the uses of a Linked List
// Marc Chee, April 2019 (marcchee@cse.unsw.edu.au)

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

#define MAX_NAME_LENGTH 50

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

struct node *createNode (char newName[], struct node *newNext);
int printPlayers(struct node* listNode);
struct node *insert(struct node* listNode, char newName[]);
struct node *insertAlphabetical(struct node* head, char newName[]);
struct node *removeNode(struct node* head, char name[]);

int main(void) {
    // create the list of players
    struct node *head = NULL;
    head = insertAlphabetical(head, "AndrewT");
    head = insertAlphabetical(head, "Jashank");
    head = insertAlphabetical(head, "Marc");
    head = insertAlphabetical(head, "AndrewB");
    head = insertAlphabetical(head, "-BPINK- Someone");
    head = insertAlphabetical(head, "Tactical Marc");
    head = insertAlphabetical(head, "COMP1511 Pass Cut-off");
    
    // Old list creation code
    /*
    struct node *head = NULL;
    head = insert(head, "AndrewT");
    head = insert(head, "Jashank");
    head = insert(head, "Marc");
    head = insert(head, "AndrewB");
    head = insert(head, "-BPINK- Someone");
    head = insert(head, "Tactical Marc");
    head = insert(head, "COMP1511 Pass Cut-off");
    */
    /*
    struct node *head = createNode("AndrewT", NULL);
    head = createNode("Jashank", head);
    head = createNode("Marc", head);
    head = createNode("AndrewB", head);
    head = createNode("-BPINK- Someone", head);
    head = createNode("Tactical Marc", head);
    head = createNode("COMP1511 Pass Cut-off", head);
    */
    
    // A game loop that runs until only one player is left
    while (printPlayers(head) > 1) {
        printf("Who just got knocked out?\n");
        char koName[MAX_NAME_LENGTH];
        fgets(koName, MAX_NAME_LENGTH, stdin);
        koName[strlen(koName) - 1] = '\0';
        head = removeNode(head, koName);
    }
    printf("The winner is: %s\n", head->name); 
    
    return 0;
}

// Create a node using the name and next pointer provided
// Return a pointer to this node
struct node *createNode(char newName[], struct node *newNext) {
    struct node *n;
    n = malloc(sizeof (struct node));
    if (n == NULL) {
        printf("Malloc failed, out of memory\n");
        exit(1);
    }
    strcpy(n->name, newName);
    n->next = newNext;
    return n;
}

// Loop through the list and print out the player names
int printPlayers(struct node* listNode) {
    printf("\nThe Players Left in the Game:\nar");
    int numPlayers = 0;
    while (listNode != NULL) {
        printf("%s\n", listNode->name);
        listNode = listNode->next;
        numPlayers++;
    }
    return numPlayers;
}

// Create and insert a new node into a list after a given listNode
struct node *insert(struct node* listNode, char newName[]) {
    struct node *n = createNode(newName, NULL);
    if (listNode == NULL) {
        // List is empty, n becomes the only element in the list
        listNode = n;
        n->next = NULL;
    } else {
        n->next = listNode->next;
        listNode->next = n;    
    }
    return listNode;
}

// Insert a node alphabetically
// Return a pointer to the head (possibly a new node)
struct node *insertAlphabetical(struct node* head, char newName[]) {
    struct node *previous = NULL;
    struct node *n = head;
    // Loop through the list and find the right place for the new name
    while (n != NULL && newName[0] > n->name[0]) {
        previous = n;
        n = n->next;
    }
    struct node *insertionPoint = insert(previous, newName);
    if(previous == NULL) {
        // we inserted at the start of the list
        insertionPoint->next = n;
        return insertionPoint;
    } else {
        return head;
    }
}

// Remove a node with the same name as the input string
struct node *removeNode(struct node* head, char name[]) {
    struct node *previous = NULL;
    struct node *n = head;
    // Loop through to try to find the correct node
    while (n != NULL && strcmp(name, n->name) != 0) {
        previous = n;
        n = n->next;
    }
    if (n != NULL) { // found the node
        if (previous == NULL) { // it's the first node
            head = n->next;
        } else {
            previous->next = n->next;
        }        
        free(n);
    }
    return head;
}

Resource created Thursday 11 April 2019, 12:58:25 AM, last modified Sunday 14 April 2019, 03:07:49 PM.

file: battleList.c


Back to top

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