Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by merlin on 4/4/2022.
- //
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include "lists.h"
- list_t *create_empty_list() {
- list_t *list = calloc(1, sizeof(list_t));
- //calloc will already have zeroed these, but this documents why the zeros are good
- list->head = NULL;
- list->tail = NULL;
- return list;
- }
- player_node *create_node(int player_id, int player_exp_points) {
- player_node *player = calloc(1, sizeof(player_node));
- player->player_id = player_id;
- player->player_exp_points = player_exp_points;
- player->next = NULL;
- return player;
- }
- void add_at_head(list_t *list, int player_id,
- int player_exp_points) {
- player_node *player =
- create_node(player_id, player_exp_points);
- if (list->head != NULL) {
- player->next = list->head;
- list->head = player;
- } else {
- list->head = player;
- list->tail = player;
- }
- }
- void add_at_tail(list_t *list, int player_id,
- int player_exp_points) {
- player_node *player =
- create_node(player_id, player_exp_points);
- if (list->head != NULL) {
- list->tail->next = player;
- list->tail = player;
- } else {
- list->head = player;
- list->tail = player;
- }
- }
- bool delete(list_t *list, int target_player_id) {
- player_node *curr = list->head;
- player_node *prev = NULL;
- while ((curr != NULL) && (curr->player_id != target_player_id)) {
- prev = curr;
- curr = curr->next;
- }
- if (curr == NULL) {
- // we didn't find what we were supposed to delete
- fprintf(stderr,
- "Tried to delete non-existent player id = %d\n", target_player_id);
- return false;
- }
- if (prev == NULL) {
- // deleting the head node
- list->head = list->head->next;
- } else {
- // deleting not at the head
- prev->next = curr->next;
- // fix up the tail pointer if we are deleting the last node
- if (curr == list->tail) {
- list->tail = prev;
- }
- }
- free(curr);
- return true;
- }
- player_node *find(list_t *list, int target_player_id) {
- player_node *p = list->head;
- while ((p != NULL) && (p->player_id != target_player_id)) {
- p = p->next;
- }
- return p;
- }
- void insert(list_t *list, int player_id, int player_exp_points) {
- player_node *player = create_node(player_id, player_exp_points);
- if (list->head != NULL) {
- player_node *curr = list->head;
- player_node *prev = NULL;
- while ((curr != NULL) && (curr->player_id < player_id)) {
- prev = curr;
- curr = curr->next;
- }
- if (prev == NULL) {
- // inserting at the head of the list
- player->next = list->head;
- list->head = player;
- } else if (curr == NULL) {
- // inserting at the tail of the list
- prev->next = player;
- list->tail = player;
- } else {
- // inserting in the middle of the list
- prev->next = player;
- player->next = curr;
- }
- } else {
- list->head = player;
- list->tail = player;
- }
- }
- void print_list(list_t *list) {
- player_node *p = list->head;
- if (p == NULL) {
- printf("The list is empty\n");
- }
- while (p != NULL) {
- printf("%d: %d\n", p->player_id, p->player_exp_points);
- p = p->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement