This file contains hidden or 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
#include <iostream> | |
#include <string> | |
#include <type_traits> | |
#include <memory> | |
namespace helper | |
{ | |
// these expressions will be evaluated at compile time | |
template <typename T> | |
constexpr typename std::enable_if<!std::is_convertible<T, std::string>::value, std::string>::type |
This file contains hidden or 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
Iterator insert(const Iterator &pos, const Data &value) | |
{ | |
if (pos.current_node != nullptr) | |
{ | |
node_ptr newNode = std::make_unique<Node>(value); | |
newNode->next = std::move(pos.current_node->next); | |
if (pos.previous_node == nullptr) | |
{ | |
head = std::move(newNode); | |
size++; |
This file contains hidden or 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
int main() | |
{ | |
LinkedList<unsigned long long> longList = {12, 3123, 4324532, 4324231313, 54654645, 654768768, 4333423545, 12123423}; | |
for (const auto &item : longList) // each item returned will call the dereferencing operator on the iterator thus getting back the data from the node | |
{ | |
std::cout << item << '\n'; | |
} | |
// we can get an iterator that can be incremented | |
auto iterator = longList.begin(); |
This file contains hidden or 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
// returning a const pointer to the back - the back is always null because it marks the end of the list | |
Iterator end() const noexcept | |
{ | |
return Iterator(); // this just return a nullptr that represents the end | |
}; |
This file contains hidden or 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
// returning a const pointer to the front | |
Iterator begin() const noexcept | |
{ | |
return Iterator(this->head); | |
}; |
This file contains hidden or 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
struct Iterator | |
{ | |
// constructor that takes in a pointer from the linked list | |
Iterator() noexcept : current_node(nullptr){}; | |
Iterator(const node_ptr &node) noexcept : current_node(node.get()){}; | |
// incrementing means going through the list | |
Iterator &operator++() noexcept | |
{ | |
if (current_node != nullptr) |
This file contains hidden or 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
int main() | |
{ | |
// will make a list and add three elements to it | |
LinkedList<std::string> myFirstList = {"one", "two", "three"}; | |
// now we just print | |
myFirstList.printList(); | |
} |
This file contains hidden or 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
namespace helper | |
{ | |
// these expressions will be evaluated at compile time | |
template <typename T> | |
constexpr typename std::enable_if<!std::is_convertible<T, std::string>::value, std::string>::type | |
to_string(const T &val) // cannot be converted directly thus we have to use type traits to determine if we can use to_string | |
{ | |
return std::to_string(val); | |
}; |
This file contains hidden or 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
operator std::string() const | |
{ | |
return helper::to_string(this->data); | |
} |
This file contains hidden or 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
void printList() const | |
{ | |
if (isEmpty()) | |
{ | |
std::cout << "Empty list" << '\n'; | |
} | |
else | |
{ | |
Node *current = head.get(); | |
while (current != nullptr) |
NewerOlder