Skip to content

Instantly share code, notes, and snippets.

View BogdanAriton's full-sized avatar
🏠
Working from home

Bogdan Ariton BogdanAriton

🏠
Working from home
View GitHub Profile
#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
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++;
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();
// 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
};
// returning a const pointer to the front
Iterator begin() const noexcept
{
return Iterator(this->head);
};
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)
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();
}
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);
};
operator std::string() const
{
return helper::to_string(this->data);
}
void printList() const
{
if (isEmpty())
{
std::cout << "Empty list" << '\n';
}
else
{
Node *current = head.get();
while (current != nullptr)