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
/** | |
* 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) { |
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
/** | |
* 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); |
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
template<typename T> | |
LinkedList<T>::LinkedList(): | |
_head(nullptr), | |
_tail(nullptr), | |
_size(0) { | |
// Initialize all class variables. | |
} |
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
template<typename T> | |
class LinkedList { | |
public: | |
LinkedList(); | |
void add(const T &item); | |
T get(int index) const; | |
void insert(const T &item, int index); |
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
template<typename T> | |
struct Node { | |
explicit Node(T elem) { | |
this->elem = elem; | |
this->next = nullptr; | |
} | |
T elem; | |
Node *next; | |
}; |
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
/** | |
* 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) { |
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
/** | |
* 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) { |
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
/** | |
* 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]; |
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
/** | |
* 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) { |
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
/** | |
* 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 |