Advertisement
imk0tter

Primative List

Jun 27th, 2023 (edited)
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. //////////////////////
  2. // Primative_list.h //
  3. //////////////////////
  4.  
  5. typedef long POINTER;
  6.  
  7. class Primative_List {
  8. private:
  9. protected:
  10.   POINTER GetEntry(int index);
  11.   POINTER list_ptr;
  12. public:
  13.   Primative_List();
  14.   virtual ~Primative_List();
  15.  
  16.   int Count();
  17.  
  18.   void Add(POINTER object_ptr);
  19.  
  20.   void Delete();
  21.  
  22.   POINTER Get(int index);
  23. };
  24.  
  25.  
  26. ////////////////////////
  27. // Primative_List.cpp //
  28. ////////////////////////
  29. #include "Primative_List.h"
  30. #include <iostream>
  31.  
  32. POINTER Primative_List::GetEntry(int index) {
  33.   // TODO: FROG CODE THAT EVALUATES POINTER MORE THAN ONCE (more than one set of
  34.   // brackets)
  35.  
  36.   // Points to head of list (this->list_ptr)
  37.     POINTER current = this->list_ptr;
  38.  
  39.   //Iterates to the element at index 'index'
  40.   for (int i = 0; i < index; ++i) {
  41.  
  42.     // Current points to the current element in the list, the next element is at the address stored in the 'current' variable
  43.     // the address at 'current' contains an address to the next element in the list which contains an address to the next element in the list
  44.     current = *(POINTER*)current;
  45.   }
  46.   // Return the next element in the list
  47.   return *(POINTER*)current;
  48. }
  49.  
  50. int Primative_List::Count()
  51. {
  52.     // points to the second element in the head of the list (stores the number of elements in the list)
  53.     return ((POINTER *)this->list_ptr)[1];
  54. }
  55.  
  56. Primative_List::Primative_List() {
  57.   // allocates enough memory for 2 integers (sizeof(int) * 2)
  58.     //this->list_ptr = ((int)malloc(sizeof(int) * 2));
  59.     this->list_ptr = (POINTER)(POINTER*)new POINTER[2];
  60.  
  61.   //sets the first node in the list to this->list_ptr
  62.   *((POINTER*)this->list_ptr) = this->list_ptr;
  63.  
  64.   //sets the second element of the head of the list to 0 (the number of elements in the list)
  65.   ((POINTER*)this->list_ptr)[1] = 0;
  66. }
  67.  
  68. void Primative_List::Add(POINTER object_ptr) {
  69.   // allocates enough memory for 2 integers for a new entry in the list
  70.     POINTER new_entry = (POINTER)(POINTER*)new POINTER[2];
  71.  
  72.   // sets the first element in the new_entry list element to the address at the head of the list
  73.   *(POINTER*)new_entry = *(POINTER*)this->list_ptr;
  74.   ((POINTER*)new_entry)[1] = object_ptr;
  75.   *(POINTER*)this->list_ptr = new_entry;
  76.   // increments the counter at the second node of the head of the list (this->list_ptr) array (contains the number of objects in the list)
  77.   ++((POINTER*)this->list_ptr)[1];
  78. }
  79.  
  80. void Primative_List::Delete() {
  81.     //std::cout << "Primative_List->Delete() called.\n";
  82.   // TODO: FROG CODE DELETE ALL AT SAME TIME (more than one set of bracket )
  83.  
  84.     // point the variable 'current' to the address at this->list_ptr
  85.   int current = *(POINTER*)this->list_ptr;
  86.   for (int i = 0; i < this->Count(); ++i) {
  87.      POINTER old = current;
  88.     current = *(POINTER*)old; // deobfuscated
  89.     delete (POINTER*)old;
  90.   }
  91.   delete (POINTER*)list_ptr;
  92. }
  93.  
  94. POINTER Primative_List::Get(int index) {
  95.   // Gets the list entry at index 'index' and returns the object it points to
  96.   return ((POINTER*)this->GetEntry(index))[1];
  97. }
  98.  
  99. Primative_List::~Primative_List() {
  100.     //std::cout << "Primative_list dtor called.\n";
  101.     //Calls the function that deletes all the list elements in the list when user attempts to delete the Primative_List object
  102.     this->Delete();
  103. }
  104.  
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement