Advertisement
WizartCraftCode

lists.c

Apr 15th, 2024
779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.50 KB | None | 0 0
  1. //
  2. // Created by merlin on 4/4/2022.
  3. //
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <stdbool.h>
  7. #include "lists.h"
  8.  
  9. list_t *create_empty_list() {
  10.     list_t *list = calloc(1, sizeof(list_t));
  11.     //calloc will already have zeroed these, but this documents why the zeros are good
  12.     list->head = NULL;
  13.     list->tail = NULL;
  14.     return list;
  15. }
  16.  
  17. player_node *create_node(int player_id, int player_exp_points) {
  18.     player_node *player = calloc(1, sizeof(player_node));
  19.     player->player_id = player_id;
  20.     player->player_exp_points = player_exp_points;
  21.     player->next = NULL;
  22.     return player;
  23. }
  24.  
  25. void add_at_head(list_t *list, int player_id,
  26.                  int player_exp_points) {
  27.     player_node *player =
  28.             create_node(player_id, player_exp_points);
  29.  
  30.     if (list->head != NULL) {
  31.         player->next = list->head;
  32.         list->head = player;
  33.     } else {
  34.         list->head = player;
  35.         list->tail = player;
  36.     }
  37. }
  38.  
  39. void add_at_tail(list_t *list, int player_id,
  40.                  int player_exp_points) {
  41.     player_node *player =
  42.             create_node(player_id, player_exp_points);
  43.  
  44.     if (list->head != NULL) {
  45.         list->tail->next = player;
  46.         list->tail = player;
  47.     } else {
  48.         list->head = player;
  49.         list->tail = player;
  50.     }
  51. }
  52.  
  53. bool delete(list_t *list, int target_player_id) {
  54.     player_node *curr = list->head;
  55.     player_node *prev = NULL;
  56.     while ((curr != NULL) && (curr->player_id != target_player_id)) {
  57.         prev = curr;
  58.         curr = curr->next;
  59.     }
  60.     if (curr == NULL) {
  61.         // we didn't find what we were supposed to delete
  62.         fprintf(stderr,
  63.                 "Tried to delete non-existent player id = %d\n", target_player_id);
  64.         return false;
  65.     }
  66.     if (prev == NULL) {
  67.         // deleting the head node
  68.         list->head = list->head->next;
  69.     } else {
  70.         // deleting not at the head
  71.         prev->next = curr->next;
  72.         // fix up the tail pointer if we are deleting the last node
  73.         if (curr == list->tail) {
  74.             list->tail = prev;
  75.         }
  76.     }
  77.     free(curr);
  78.     return true;
  79. }
  80.  
  81.  
  82. player_node *find(list_t *list, int target_player_id) {
  83.     player_node *p = list->head;
  84.     while ((p != NULL) && (p->player_id != target_player_id)) {
  85.         p = p->next;
  86.     }
  87.     return p;
  88. }
  89.  
  90.  
  91. void insert(list_t *list, int player_id, int player_exp_points) {
  92.     player_node *player = create_node(player_id, player_exp_points);
  93.     if (list->head != NULL) {
  94.         player_node *curr = list->head;
  95.         player_node *prev = NULL;
  96.         while ((curr != NULL) && (curr->player_id < player_id)) {
  97.             prev = curr;
  98.             curr = curr->next;
  99.         }
  100.         if (prev == NULL) {
  101.             // inserting at the head of the list
  102.             player->next = list->head;
  103.             list->head = player;
  104.         } else if (curr == NULL) {
  105.             // inserting at the tail of the list
  106.             prev->next = player;
  107.             list->tail = player;
  108.         } else {
  109.             // inserting in the middle of the list
  110.             prev->next = player;
  111.             player->next = curr;
  112.         }
  113.  
  114.     } else {
  115.         list->head = player;
  116.         list->tail = player;
  117.     }
  118. }
  119.  
  120. void print_list(list_t *list) {
  121.     player_node *p = list->head;
  122.     if (p == NULL) {
  123.         printf("The list is empty\n");
  124.     }
  125.     while (p != NULL) {
  126.         printf("%d: %d\n", p->player_id, p->player_exp_points);
  127.         p = p->next;
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement