Advertisement
imk0tter

LiTHiuM List

Mar 31st, 2023
861
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.28 KB | None | 0 0
  1. #include <stdlib.h>
  2.  
  3. long ListCreate() {
  4.   long list_ptr = malloc(sizeof(long) * 2);
  5.   ((long *)list_ptr)[0] = list_ptr;
  6.  
  7.   ((long *)list_ptr)[1] = 0;
  8.   return list_ptr;
  9. }
  10. long ListCount(long list_ptr) { return ((long *)list_ptr)[1]; }
  11.  
  12. long ListGetEntry(long list_ptr, long index) {
  13.   // TODO: Get Current List Object without iterating; dynamic number of
  14.   // dereference brackets frog code
  15.   long current_list_entry_ptr = list_ptr;
  16.   for (int i = 0; i <= index; ++i) {
  17.     current_list_entry_ptr = ((long *)current_list_entry_ptr)[0];
  18.   }
  19.   return current_list_entry_ptr;
  20. }
  21.  
  22. long ListRemove(long list_ptr, long index) {
  23.   long list_count = ((long *)list_ptr)[1];
  24.  
  25.   if (index == 0) {
  26.     long current_list_entry_ptr = ((long *)list_ptr)[0];
  27.     ((long *)list_ptr)[0] = ((long *)current_list_entry_ptr)[0];
  28.     --((long *)list_ptr)[1];
  29.     free(current_list_entry_ptr);
  30.   } else {
  31.     long current_list_entry_ptr = ListGetEntry(list_ptr, index);
  32.  
  33.     if (index <= list_count - 1) {
  34.       long previous_list_entry_ptr = ListGetEntry(list_ptr, index - 1);
  35.       long next_list_entry_ptr = ListGetEntry(list_ptr, index + 1);
  36.  
  37.       ((long *)previous_list_entry_ptr)[0] = next_list_entry_ptr;
  38.       --((long *)list_ptr)[1];
  39.       free(current_list_entry_ptr);
  40.     } else {
  41.       --((long *)list_ptr)[1];
  42.       free(current_list_entry_ptr);
  43.     }
  44.   }
  45. }
  46.  
  47. void ListDelete(long list_ptr) {
  48.   // TODO: Delete all list objects at same time with frog code
  49.   long list_count = ((long *)list_ptr)[1];
  50.   long next_list_entry_ptr = list_ptr;
  51.   while (ListCount(list_ptr) > 0) {
  52.     ListRemove(list_ptr, 0);
  53.   }
  54.   free(list_ptr);
  55. }
  56.  
  57. long ListAdd(long list_ptr, long obj_ptr) {
  58.   long list_count = ((long *)list_ptr)[1];
  59.  
  60.   long current_list_entry_ptr = malloc(sizeof(long) * 2);
  61.  
  62.   long last_list_entry_ptr = ListGetEntry(list_ptr, list_count - 1);
  63.   ((long *)last_list_entry_ptr)[0] = current_list_entry_ptr;
  64.   ((long *)current_list_entry_ptr)[1] = obj_ptr;
  65.   ((long *)current_list_entry_ptr)[0] = list_ptr;
  66.  
  67.   long count = ((long *)list_ptr)[1]++;
  68.  
  69.   return count;
  70. }
  71.  
  72. void ListInsert(long list_ptr, long obj_ptr, long index) {
  73.  
  74.   long new_list_entry_ptr = malloc(sizeof(long) * 2);
  75.  
  76.   ((long *)new_list_entry_ptr)[1] = obj_ptr;
  77.   if (index == 0) {
  78.     ((long *)new_list_entry_ptr)[0] = ((long *)list_ptr)[0];
  79.     ((long *)list_ptr)[0] = new_list_entry_ptr;
  80.   } else {
  81.     long list_count = ((long *)list_ptr)[1];
  82.  
  83.     if (index < list_count) {
  84.       long previous_object_entry_ptr = ListGetEntry(list_ptr, index - 1);
  85.       long current_object_entry_ptr = ListGetEntry(list_ptr, index);
  86.  
  87.       ((long *)new_list_entry_ptr)[0] = current_object_entry_ptr;
  88.       ((long *)new_list_entry_ptr)[1] = obj_ptr;
  89.  
  90.       ((long *)previous_object_entry_ptr)[0] = new_list_entry_ptr;
  91.  
  92.     } else {
  93.       long last_entry_in_list_ptr = ListGetEntry(list_ptr, list_count - 1);
  94.       long previous_entry_in_list_ptr = ListGetEntry(list_ptr, list_count - 2);
  95.       ((long *)new_list_entry_ptr)[0] = last_entry_in_list_ptr;
  96.       ((long *)last_entry_in_list_ptr)[0] = list_ptr;
  97.       ((long *)previous_entry_in_list_ptr)[0] = new_list_entry_ptr;
  98.     }
  99.   }
  100.  
  101.   ++((long *)list_ptr)[1];
  102. }
  103.  
  104. long ListGet(long list_ptr, long index) {
  105.   return ((long *)ListGetEntry(list_ptr, index))[1];
  106. }
Advertisement
Comments
  • imk0tter
    1 year
    # C 3.50 KB | 0 0
    1. #include "Primative_List.h"
    2.  
    3. #include <iostream>
    4.  
    5. Primative_List::Primative_List() {
    6.   this->list_ptr = (long)malloc(sizeof(long) * 2);
    7.   ((long *)list_ptr)[0] = this->list_ptr;
    8.   ((long *)list_ptr)[1] = 0;
    9. }
    10.  
    11. long Primative_List::Count() { return ((long *)this->list_ptr)[1]; }
    12.  
    13. long Primative_List::GetEntry(long index) {
    14.   // TODO: Get Current List Object without iterating; dynamic number of
    15.   // dereference brackets frog code
    16.  
    17.   //TODO: index = (index + this.Count()) % this->Count();
    18.   if (index < 0) index = this->Count() + index;
    19.  
    20.   long current_list_entry_ptr = this->list_ptr;
    21.   for (int i = 0; i <= index; ++i) {
    22.     current_list_entry_ptr = ((long *)current_list_entry_ptr)[0];
    23.   }
    24.   return current_list_entry_ptr;
    25. }
    26.  
    27. void Primative_List::Remove(long index) {
    28.   long list_count = ((long *)this->list_ptr)[1];
    29.  
    30.   if (index == 0) {
    31.     long current_list_entry_ptr = ((long *)this->list_ptr)[0];
    32.     ((long *)this->list_ptr)[0] = ((long *)current_list_entry_ptr)[0];
    33.     --((long *)this->list_ptr)[1];
    34.     free((long *)current_list_entry_ptr);
    35.   } else {
    36.     long current_list_entry_ptr = this->GetEntry(index);
    37.  
    38.     if (index <= list_count - 1) {
    39.       long previous_list_entry_ptr = this->GetEntry(index - 1);
    40.       long next_list_entry_ptr = this->GetEntry(index + 1);
    41.  
    42.       ((long *)previous_list_entry_ptr)[0] = next_list_entry_ptr;
    43.       --((long *)this->list_ptr)[1];
    44.       free((long *)current_list_entry_ptr);
    45.     } else {
    46.       --((long *)this->list_ptr)[1];
    47.       free((long *)current_list_entry_ptr);
    48.     }
    49.   }
    50. }
    51.  
    52. void Primative_List::Delete() {
    53.   // TODO: Delete all list objects at same time with frog code
    54.   long list_count = ((long *)this->list_ptr)[1];
    55.   long next_list_entry_ptr = this->list_ptr;
    56.   while (this->Count() > 0) {
    57.     this->Remove(0);
    58.   }
    59.   free((long *)this->list_ptr);
    60.   delete this;
    61. }
    62.  
    63. long Primative_List::Add(long obj_ptr) {
    64.   long list_count = ((long *)this->list_ptr)[1];
    65.  
    66.   long current_list_entry_ptr = (long)malloc(sizeof(long) * 2);
    67.  
    68.   long last_list_entry_ptr = this->GetEntry(list_count - 1);
    69.   ((long *)last_list_entry_ptr)[0] = current_list_entry_ptr;
    70.   ((long *)current_list_entry_ptr)[1] = obj_ptr;
    71.   ((long *)current_list_entry_ptr)[0] = this->list_ptr;
    72.  
    73.   long count = ((long *)this->list_ptr)[1]++;
    74.  
    75.   return count;
    76. }
    77.  
    78. void Primative_List::Insert(long index, long obj_ptr) {
    79.  
    80.   long new_list_entry_ptr = (long)malloc(sizeof(long) * 2);
    81.  
    82.   ((long *)new_list_entry_ptr)[1] = obj_ptr;
    83.   if (index == 0) {
    84.     ((long *)new_list_entry_ptr)[0] = ((long *)this->list_ptr)[0];
    85.     ((long *)this->list_ptr)[0] = new_list_entry_ptr;
    86.   } else {
    87.     long list_count = ((long *)this->list_ptr)[1];
    88.  
    89.     if (index < list_count) {
    90.       long previous_object_entry_ptr = this->GetEntry(index - 1);
    91.       long current_object_entry_ptr = this->GetEntry(index);
    92.  
    93.       ((long *)new_list_entry_ptr)[0] = current_object_entry_ptr;
    94.       ((long *)new_list_entry_ptr)[1] = obj_ptr;
    95.  
    96.       ((long *)previous_object_entry_ptr)[0] = new_list_entry_ptr;
    97.  
    98.     } else {
    99.       long last_entry_in_list_ptr = this->GetEntry(list_count - 1);
    100.       long previous_entry_in_list_ptr = this->GetEntry(list_count - 2);
    101.       ((long *)new_list_entry_ptr)[0] = last_entry_in_list_ptr;
    102.       ((long *)last_entry_in_list_ptr)[0] = this->list_ptr;
    103.       ((long *)previous_entry_in_list_ptr)[0] = new_list_entry_ptr;
    104.     }
    105.   }
    106.  
    107.   ++((long *)this->list_ptr)[1];
    108. }
    109.  
    110. long Primative_List::Get(long index) {
    111.   return ((long *)this->GetEntry(index))[1];
    112. }
Add Comment
Please, Sign In to add comment
Advertisement