// 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
);
int 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
);
void freeList(struct player *head);
int main(void) {
struct player *head = NULL;
head = insertAlpha("Marc", head);
head = insertAlpha("AndrewB", head);
head = insertAlpha("Batman", head);
head = insertAlpha("Leonardo", head);
head = insertAlpha("Tom", head);
// Game loop where players are removed one by one
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 = removePlayer(koName, head);
printf("----------\n");
}
printf("has won the Battle Royale!\n");
freeList(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;
}
int printPlayers(struct player *p) {
int numPlayers = 0;
while (p != NULL) {
fputs(p->name, stdout);
putchar('\n');
p = p->next;
numPlayers++;
}
return numPlayers;
}
// 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;
}
void freeList(struct player *head) {
while (head != NULL) {
struct player * freeNode = head;
head = head->next;
free(freeNode);
}
}
Resource created Thursday 25 July 2019, 01:54:03 AM.
file: battleLec.c