Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////
- // Primative_list.h //
- //////////////////////
- typedef long POINTER;
- class Primative_List {
- private:
- protected:
- POINTER GetEntry(int index);
- POINTER list_ptr;
- public:
- Primative_List();
- virtual ~Primative_List();
- int Count();
- void Add(POINTER object_ptr);
- void Delete();
- POINTER Get(int index);
- };
- ////////////////////////
- // Primative_List.cpp //
- ////////////////////////
- #include "Primative_List.h"
- #include <iostream>
- POINTER Primative_List::GetEntry(int index) {
- // TODO: FROG CODE THAT EVALUATES POINTER MORE THAN ONCE (more than one set of
- // brackets)
- // Points to head of list (this->list_ptr)
- POINTER current = this->list_ptr;
- //Iterates to the element at index 'index'
- for (int i = 0; i < index; ++i) {
- // Current points to the current element in the list, the next element is at the address stored in the 'current' variable
- // 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
- current = *(POINTER*)current;
- }
- // Return the next element in the list
- return *(POINTER*)current;
- }
- int Primative_List::Count()
- {
- // points to the second element in the head of the list (stores the number of elements in the list)
- return ((POINTER *)this->list_ptr)[1];
- }
- Primative_List::Primative_List() {
- // allocates enough memory for 2 integers (sizeof(int) * 2)
- //this->list_ptr = ((int)malloc(sizeof(int) * 2));
- this->list_ptr = (POINTER)(POINTER*)new POINTER[2];
- //sets the first node in the list to this->list_ptr
- *((POINTER*)this->list_ptr) = this->list_ptr;
- //sets the second element of the head of the list to 0 (the number of elements in the list)
- ((POINTER*)this->list_ptr)[1] = 0;
- }
- void Primative_List::Add(POINTER object_ptr) {
- // allocates enough memory for 2 integers for a new entry in the list
- POINTER new_entry = (POINTER)(POINTER*)new POINTER[2];
- // sets the first element in the new_entry list element to the address at the head of the list
- *(POINTER*)new_entry = *(POINTER*)this->list_ptr;
- ((POINTER*)new_entry)[1] = object_ptr;
- *(POINTER*)this->list_ptr = new_entry;
- // 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)
- ++((POINTER*)this->list_ptr)[1];
- }
- void Primative_List::Delete() {
- //std::cout << "Primative_List->Delete() called.\n";
- // TODO: FROG CODE DELETE ALL AT SAME TIME (more than one set of bracket )
- // point the variable 'current' to the address at this->list_ptr
- int current = *(POINTER*)this->list_ptr;
- for (int i = 0; i < this->Count(); ++i) {
- POINTER old = current;
- current = *(POINTER*)old; // deobfuscated
- delete (POINTER*)old;
- }
- delete (POINTER*)list_ptr;
- }
- POINTER Primative_List::Get(int index) {
- // Gets the list entry at index 'index' and returns the object it points to
- return ((POINTER*)this->GetEntry(index))[1];
- }
- Primative_List::~Primative_List() {
- //std::cout << "Primative_list dtor called.\n";
- //Calls the function that deletes all the list elements in the list when user attempts to delete the Primative_List object
- this->Delete();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement