Last active
November 8, 2015 15:41
-
-
Save manuelmenzella/0fd85280d5051abec2c7 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
#ifndef _LINKED_LIST | |
#define _LINKED_LIST | |
#include <memory> | |
template <class T> | |
struct LinkedListNode { | |
LinkedListNode(T value, std::unique_ptr<LinkedListNode<T>> next = nullptr) | |
: value(value), next(std::move(next)) { }; | |
~LinkedListNode() { | |
std::cout << "destructed for value " << this->value << std::endl; | |
} | |
T value; | |
std::unique_ptr<LinkedListNode<T>> next; | |
}; | |
template <class T> | |
class LinkedList { | |
public: | |
LinkedList() = default; | |
void push_back(const T &value) { | |
std::unique_ptr<LinkedListNode<T>> new_node(new LinkedListNode<T>(value)); | |
if (this->tail) { | |
this->tail->next = std::move(new_node); | |
this->tail = std::move(new_node); | |
} else { | |
this->head = std::move(new_node); | |
this->tail = std::move(new_node); | |
} | |
}; | |
private: | |
std::unique_ptr<LinkedListNode<T>> head; | |
std::unique_ptr<LinkedListNode<T>> tail; | |
}; | |
#endif |
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
#include <iostream> | |
#include "linked_list.h" | |
void linked_list_test() { | |
constexpr int N = 5000000; | |
{ | |
LinkedList<int> ll; | |
for (int i = 0; i < N; ++i) { | |
ll.push_back(i); | |
} | |
} | |
double size = N * sizeof(LinkedListNode<int>) / 1024; | |
std::cout << "Total size: " << size << "KB" << std::endl; | |
} | |
int main() | |
{ | |
std::cout << "Woohoo! :)" << std::endl; | |
linked_list_test(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment