Advertisement
imk0tter
Mar 30th, 2023
53
0
Never
This is comment for paste List shit
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "List.h"
  2.  
  3. #include <stdlib.h>
  4.  
  5. struct List * ListCreate() {
  6.   struct List *list_ptr = malloc(sizeof(struct List));
  7.   list_ptr->list_entry = list_ptr;
  8.   list_ptr->count = 0;
  9. }
  10. long ListCount(struct List *list_ptr) { return list_ptr->count; }
  11.  
  12. struct ListEntry *ListGetEntry(struct List *list_ptr, long index) {
  13.   // TODO: Get Current List Object without iterating; dynamic number of
  14.   // dereference brackets frog code
  15.   struct ListEntry *current_list_entry_ptr = (struct ListEntry *)list_ptr;
  16.   for (int i = 0; i <= index; ++i) {
  17.     current_list_entry_ptr = current_list_entry_ptr->list_entry;
  18.   }
  19.   return current_list_entry_ptr;
  20. }
  21.  
  22. void ListRemove(struct List *list_ptr, long index) {
  23.   long list_count = list_ptr->count;
  24.  
  25.   if (index == 0) {
  26.     struct ListEntry *current_list_entry_ptr = list_ptr->list_entry;
  27.     list_ptr->list_entry = current_list_entry_ptr->list_entry;
  28.     --list_ptr->count;
  29.     free(current_list_entry_ptr);
  30.   } else {
  31.     struct ListEntry *current_list_entry_ptr = ListGetEntry(list_ptr, index);
  32.  
  33.     if (index <= list_count - 1) {
  34.       struct ListEntry *previous_list_entry_ptr =
  35.           ListGetEntry(list_ptr, index - 1);
  36.       struct ListEntry *next_list_entry_ptr = ListGetEntry(list_ptr, index + 1);
  37.  
  38.       previous_list_entry_ptr->list_entry = next_list_entry_ptr;
  39.       --list_ptr->count;
  40.       free(current_list_entry_ptr);
  41.     } else {
  42.       --list_ptr->count;
  43.       free(current_list_entry_ptr);
  44.     }
  45.   }
  46. }
  47.  
  48. void ListDelete(struct List *list_ptr) {
  49.   // TODO: Delete all list objects at same time with frog code
  50.   long list_count = list_ptr->count;
  51.   struct ListEntry *next_list_entry_ptr = (struct ListEntry *)list_ptr;
  52.   while (ListCount(list_ptr) > 0) {
  53.     ListRemove(list_ptr, 0);
  54.   }
  55.   free(list_ptr);
  56. }
  57.  
  58. void ListAdd(struct List *list_ptr, long obj_ptr) {
  59.   long list_count = list_ptr->count;
  60.  
  61.   struct ListEntry *current_list_entry_ptr = malloc(sizeof(struct ListEntry));
  62.   struct ListEntry *last_list_entry_ptr =
  63.       ListGetEntry(list_ptr, list_count - 1);
  64.  
  65.   last_list_entry_ptr->list_entry = current_list_entry_ptr;
  66.   current_list_entry_ptr->object_ptr = obj_ptr;
  67.  
  68.   ++list_ptr->count;
  69. }
  70.  
  71. long ListGet(struct List *list_ptr, long index) {
  72.   return ListGetEntry(list_ptr, index)->object_ptr;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement