Skip to content

Instantly share code, notes, and snippets.

View danielgospodinow's full-sized avatar
🔊
I convert chalga to code

Daniel Gospodinow danielgospodinow

🔊
I convert chalga to code
View GitHub Profile
@danielgospodinow
danielgospodinow / linkedListInsertOperation.hpp
Created September 22, 2019 20:09
Linked List insert operation
/**
* Insert an element to the data structure at a specified index.
*
* @tparam T type
* @param item item to insert
* @param index index at which to insert
*/
template<typename T>
void LinkedList<T>::insert(const T &item, const int index) {
if (index < 0 || index >= _size) {
@danielgospodinow
danielgospodinow / linkedListAddOperation.hpp
Created September 22, 2019 20:07
LinkedList add operation
/**
* Add a new item to the data structure.
*
* @tparam T type
* @param item the new item
*/
template<typename T>
void LinkedList<T>::add(const T &item) {
// Create a box which wraps the new element.
auto *const newNode = new Node<T>(item);
@danielgospodinow
danielgospodinow / linkedListConstructor.hpp
Created September 22, 2019 20:05
Linked List constructor
template<typename T>
LinkedList<T>::LinkedList():
_head(nullptr),
_tail(nullptr),
_size(0) {
// Initialize all class variables.
}
@danielgospodinow
danielgospodinow / linkedListDefinition.hpp
Created September 22, 2019 20:01
Linked List Definition
template<typename T>
class LinkedList {
public:
LinkedList();
void add(const T &item);
T get(int index) const;
void insert(const T &item, int index);
@danielgospodinow
danielgospodinow / linkedListNode.hpp
Created September 22, 2019 19:59
Linked List Node implementation
template<typename T>
struct Node {
explicit Node(T elem) {
this->elem = elem;
this->next = nullptr;
}
T elem;
Node *next;
};
@danielgospodinow
danielgospodinow / arrayListRemoveAt.hpp
Created September 22, 2019 19:47
ArrayList removeAt operation
/**
* Remove an element from a specified index.
*
* @tparam T type
* @param index index of an element
*/
template<typename T>
void ArrayList<T>::removeAt(const int index) {
if (index < 0 || index >= _size) {
@danielgospodinow
danielgospodinow / arrayListGetOperation.hpp
Last active September 22, 2019 19:51
Array List get operation
/**
* Finds and returns an element by its index.
*
* @tparam T type
* @param index index of the required element.
* @return the element required
*/
template<typename T>
T ArrayList<T>::get(const int index) const {
if (index < 0 || index >= _size) {
@danielgospodinow
danielgospodinow / arrayListResize.hpp
Last active September 22, 2019 19:50
Array List resize operation
/**
* Double the size of the data structure.
*
* @tparam T type
*/
template<typename T>
void ArrayList<T>::resize() {
// Create the new bigger array with double the size of the old one.
T *const newStorage = new T[_capacity *= 2];
@danielgospodinow
danielgospodinow / arrayListInsert.hpp
Last active September 22, 2019 19:49
Array List insert operation
/**
* Insert an element to the data structure at a specified index.
*
* @tparam T type
* @param item item to insert
* @param index index at which to insert
*/
template<typename T>
void ArrayList<T>::insert(const T &item, const int index) {
if (index < 0 || index >= _size) {
@danielgospodinow
danielgospodinow / arrayListRemove.hpp
Last active September 22, 2019 19:46
Array List remove operation
/**
* Remove all elements from the data structure which match
* the item provided.
*
* @tparam T type
* @param item item to be removed
*/
template<typename T>
void ArrayList<T>::remove(const T &item) {
// Iterate through all elements and remove those