Last active
March 1, 2024 22:10
-
-
Save FormerlyZeroCool/0e30c9c4fcb7cd66434989d98ec96186 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//dynamic arrays in python (python lists) | |
my_list = [1, 2, 3] | |
my_list.append(4) | |
my_list.append(5) | |
my_list.append(6) | |
[1, 2, 3, 4, 5, 6] | |
my_list.append(1) | |
[1, 2, 3, 4, 5, 6, 1] | |
my_list[3] | |
//static array C or C style C++ | |
int my_array[6] = {1, 2, 3, 4, 5, 6}; | |
my_array[3]; | |
//dynamic array C++ | |
vector<int> my_vec = {1, 2, 3, 4, 5, 6}; | |
my_vec.push_back(1); | |
my_vec.pop_back(); | |
my_vec.back(); | |
my_vec[3]; | |
//Dynamically sized array C or C style C++ | |
int size = 6; | |
int *my_dynamic_array = new int[size]; | |
my_dynamic_array[0] = 1; | |
my_dynamic_array[1] = 2; | |
my_dynamic_array[2] = 3; | |
my_dynamic_array[3] = 4; | |
my_dynamic_array[4] = 5; | |
my_dynamic_array[5] = 6; | |
//example copying one array to a new one twice the size | |
int *larger_dynamic_array = new int[size * 2]; | |
for(int i = 0; i < size; i++) | |
{ | |
larger_dynamic_array[i] = my_dynamic_array[i]; | |
} | |
my_dynamic_array[4] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3] | |
[1, next] -> [2, next] -> [3, next] -> null | |
struct Node { | |
public: | |
int data; | |
Node* next; | |
Node(int data) { | |
this->data = data; | |
this->next = nullptr; | |
} | |
}; | |
class LinkedList { | |
private: | |
Node* head; | |
Node* tail; | |
public: | |
LinkedList(){ | |
head = nullptr; | |
tail = nullptr; | |
} | |
int get_front() | |
{ | |
if(is_empty()) | |
throw "Error the list is empty, cannot get front element!"; | |
return head->data; | |
} | |
bool is_empty() | |
{ | |
return head == nullptr; | |
} | |
void remove_front() | |
{ | |
//if list is empty throw an error, and let the user of the linked list figure out what to do | |
if(is_empty()) | |
throw "Error list is empty, cannot remove an element from an empty list.\n"; | |
//if there's only one element in the list then we need to update the head, and the tail | |
if(head->next == nullptr) | |
{ | |
delete head; | |
head = nullptr; | |
tail = nullptr; | |
} | |
//all other cases | |
else | |
{ | |
Node* prev_head = head; | |
head = head->next; | |
delete prev_head; | |
} | |
} | |
void push_front(int data) | |
{ | |
Node* node = new Node(data); | |
//covers the case of an empty list | |
if(is_empty()) | |
{ | |
head = node; | |
tail = node; | |
} | |
//all other cases | |
else | |
{ | |
node->next = head; | |
head = node; | |
} | |
} | |
void push_back(int data) | |
{ | |
Node* node = new Node(data); | |
//covers the case of an empty list | |
if(is_empty()) | |
{ | |
head = node; | |
tail = node; | |
} | |
//cover all other cases | |
else | |
{ | |
tail->next = node; | |
tail = node; | |
} | |
} | |
~LinkedList() | |
{ | |
while(!is_empty()) | |
remove_front(); | |
} | |
}; | |
//head tail | |
//Head -> node -> node -> Tail | |
//Head -> node -> node -> node -> Tail | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//uses our vector implementation to implement a stack of integers | |
class Stack { | |
private: | |
vector data; | |
public: | |
void pop() | |
{ | |
if(data.size() > 0) | |
data.pop_back(); | |
} | |
void push(int val) | |
{ | |
data.push_back(val); | |
} | |
int top() | |
{ | |
if(data.size() > 0) | |
{ | |
return data.back();//data[data.size() - 1]; | |
} | |
} | |
int size() | |
{ | |
return data.size(); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//dynamic array of integers implementation | |
class vector { | |
int capacity = 2; | |
int *data = new int[capacity]; | |
int length = 0; | |
void realloc() | |
{ | |
int *larger_data = new int[capacity * 2]; | |
for(int i = 0; i < capacity; i++) | |
larger_data[i] = data[i]; | |
delete[] data; | |
data = larger_data; | |
capacity = capacity * 2; | |
} | |
public: | |
void pop_back() | |
{ | |
if(length > 0) | |
length = length - 1; | |
} | |
void push_back(int val) | |
{ | |
if(length == capacity) { | |
realloc(); | |
} | |
data[length] = val; | |
length = length + 1; | |
} | |
int back() | |
{ | |
return data[size() - 1]; | |
} | |
void size() | |
{ | |
return length; | |
} | |
int& operator[](int index) | |
{ | |
if(index > 0 && index < this->size()) | |
return data[index]; | |
throw string("index out of bounds exception"); | |
} | |
~vector() | |
{ | |
delete[] data; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment