// Battle Royale Linked List Demo
// Part 1: Creating a linked list using a function
// that creates a single node.
// Looping through the list and printing out the names
// Part 2: Inserting into the linked list.
// Also finding specific insertion points and
// inserting into an ordered list
// Marc Chee (cs1511@cse.unsw.edu.au) November 2020
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LENGTH 100
struct player {
struct player *next;
char name[MAX_NAME_LENGTH];
};
struct player *create_player(char *newName, struct player *newNext);
void printPlayers(struct player *playerList);
struct player *insert_after(struct player *insert_pos, char *newName);
struct player *insert_alpha(struct player *list, char *newName);
int main(void) {
struct player *head = NULL;
head = insert_alpha(head, "Marc");
printPlayers(head);
printf("--------------\n");
head = insert_alpha(head, "Chicken");
printPlayers(head);
printf("--------------\n");
head = insert_alpha(head, "Aang");
printPlayers(head);
printf("--------------\n");
head = insert_alpha(head, "Zuko");
printPlayers(head);
printf("--------------\n");
head = insert_alpha(head, "Katara");
printPlayers(head);
return 0;
}
// create player allocates memory for a new player and
// populates it with newName and newNext.
// returns a pointer the the newly created player
struct player *create_player(char *newName, struct player *newNext) {
struct player *newPlayer = malloc(sizeof (struct player));
strcpy(newPlayer->name, newName);
newPlayer->next = newNext;
return newPlayer;
}
// takes the pointer to the head of a list of players
// prints their names, one per line
void printPlayers(struct player *playerList) {
struct player *curr = playerList;
while (curr != NULL) {
//fputs(curr->name, stdout);
//putchar('\n');
printf("%s\n", curr->name);
curr = curr->next;
}
}
// Insert a newly created player after insert_pos in a list
// If the list was empty, return a pointer to the new list
// Otherwise, return insert_pos
struct player *insert_after(struct player *insert_pos, char *newName) {
// create a new player
struct player *new_player = create_player(newName, NULL);
if (insert_pos == NULL) {
// inserting into an empty list
insert_pos = new_player;
} else {
// inserting into a list that exists
// set the new player's next to be insert_pos' next
new_player->next = insert_pos->next;
// set insert_pos' next to aim at the new player
insert_pos->next = new_player;
}
return insert_pos;
}
// Insert an element into an already alphabetically ordered list
// Will insert into the correct position, then return the head
// of the list, even if it has changed
struct player *insert_alpha(struct player *list, char *newName) {
// loop through looking for the insertion point
struct player *curr = list;
struct player *prev = NULL;
// stop when we see something that's after us in the alphabet
while (curr != NULL && strcmp(newName, curr->name) > 0) {
prev = curr;
curr = curr->next;
}
// prev now aims at where the insertion should happen
// Use insert_after
struct player *new_head = insert_after(prev, newName);
if (prev == NULL) {
// the new player is the head of the list
new_head->next = list;
} else {
// the new player is inserted somewhere other than the head
new_head = list;
}
return new_head;
}
Resource created Tuesday 03 November 2020, 01:25:34 PM.
file: battleRoyale.c