Advertisement
imk0tter

List shit

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