// A demo of reversing a linked list
// This demo will show a possible procedural implementation
// of printing out the data from a linked list in reverse
// It will also show a recursive implementation
// Marc Chee (cs1511@cse.unsw.edu.au), November 2020
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 100
struct player {
struct player *next;
char name[MAX_NAME_LENGTH];
};
struct player *createPlayer(char *newName, struct player *newNext);
int main(void) {
// create a list of players
// In a game, we'd expect this list to be created during the game based
// on who was knocked out
struct player *list = NULL;
list = createPlayer("Marc", list);
list = createPlayer("Zuko", list);
list = createPlayer("Katara", list);
list = createPlayer("Chicken", list);
list = createPlayer("Aang", list);
}
// 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 *createPlayer(char *newName, struct player *newNext) {
struct player *newPlayer = malloc(sizeof (struct player));
strcpy(newPlayer->name, newName);
newPlayer->next = newNext;
return newPlayer;
}
Resource created Thursday 12 November 2020, 07:18:16 PM.
file: reverseList.c