Last active
May 10, 2019 02:32
-
-
Save KristupasSavickas/d4303faecd528d3304a821ce6225bdc1 to your computer and use it in GitHub Desktop.
LinkedList
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 "linked_list.h" | |
LinkedList::Node::Node(int value_) { | |
next = nullptr; | |
value = value_; | |
} | |
LinkedList::LinkedList() { | |
head_ = nullptr; | |
tail_ = nullptr; | |
size_ = 0; | |
} | |
LinkedList::LinkedList(std::initializer_list<int> list) { | |
head_ = nullptr; | |
tail_ = nullptr; | |
size_ = 0; | |
for (auto x: list) { | |
insert_tail(x); | |
} | |
} | |
LinkedList::LinkedList(const LinkedList &other) { | |
LinkedList::Node *walk = other.head_; | |
head_ = nullptr; | |
tail_ = nullptr; | |
size_ = 0; | |
while (walk != nullptr) { | |
insert_tail(walk->value); | |
walk = walk->next; | |
} | |
} | |
LinkedList& LinkedList::operator=(const LinkedList &other) { | |
LinkedList::Node *walk = other.head_; | |
head_ = nullptr; | |
tail_ = nullptr; | |
size_ = 0; | |
while (walk != nullptr) { | |
insert_tail(walk->value); | |
walk = walk->next; | |
} | |
return *this; | |
} | |
LinkedList::LinkedList(LinkedList &&other) noexcept { | |
head_ = other.head_; | |
tail_ = other.tail_; | |
size_ = other.size_; | |
other.head_ = nullptr; | |
other.tail_ = nullptr; | |
other.size_ = 0; | |
} | |
LinkedList& LinkedList::operator=(LinkedList &&other) noexcept { | |
head_ = other.head_; | |
tail_ = other.tail_; | |
size_ = other.size_; | |
other.head_ = nullptr; | |
other.tail_ = nullptr; | |
other.size_ = 0; | |
return *this; | |
} | |
LinkedList::~LinkedList() { | |
LinkedList::Node *walk = head_; | |
while (walk != nullptr) { | |
LinkedList::Node *temp = walk; | |
walk = walk->next; | |
delete(temp); | |
} | |
} | |
void LinkedList::add_to_empty(Node *add) { | |
head_ = add; | |
tail_ = add; | |
} | |
void LinkedList::insert_tail(int value) { | |
auto *add = new Node(value); | |
if (head_ == nullptr) { | |
add_to_empty(add); | |
} else { | |
tail_->next = add; | |
tail_ = add; | |
} | |
size_++; | |
} | |
void LinkedList::insert_head(int value) { | |
auto add = new Node(value); | |
if (head_ == nullptr) { | |
add_to_empty(add); | |
} else { | |
add->next = head_; | |
head_ = add; | |
} | |
size_++; | |
} | |
void LinkedList::insert_after(LinkedList::Node *node, int value) { | |
if (node == tail_) { | |
insert_tail(value); | |
} else { | |
auto add = new LinkedList::Node(value); | |
add->next = node->next; | |
node->next = add; | |
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
#ifndef LAB1_LINKED_LIST_H | |
#define LAB1_LINKED_LIST_H | |
#include <cstddef> | |
#include <initializer_list> | |
class LinkedList { | |
public: | |
struct Node { | |
public: | |
int value; | |
Node *next; | |
explicit Node(int value); | |
}; | |
private: | |
Node *head_; | |
Node *tail_; | |
std::size_t size_; | |
void add_to_empty(Node* add); | |
public: | |
LinkedList(); | |
~LinkedList(); | |
// List initializer constructor | |
LinkedList(std::initializer_list<int> list); | |
// Copy constructor | |
LinkedList(const LinkedList &other); | |
// Move constructor | |
LinkedList(LinkedList &&other) noexcept; | |
// Copy assignment operator | |
LinkedList& operator=(const LinkedList &other); | |
// Move assignment opeartor | |
LinkedList& operator=(LinkedList &&other) noexcept; | |
void insert_head(int value); | |
void insert_after(Node *node, int value); | |
void insert_tail(int value); | |
inline Node *head() const { | |
return head_; | |
} | |
inline Node *tail() const { | |
return tail_; | |
} | |
inline std::size_t size() const { | |
return size_; | |
} | |
}; | |
#endif //LAB1_LINKED_LIST_H |
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 "linked_list.h" | |
#include "gtest/gtest.h" | |
TEST(LinkedListTest, Initialization) { | |
LinkedList list; | |
ASSERT_EQ(list.size(), 0); | |
} | |
TEST(LinkedListTest, ListInitialization) { | |
LinkedList list = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
LinkedList::Node *walk; | |
walk = list.head(); | |
for (int i = 0; i < 10; i++) { | |
ASSERT_EQ(walk->value, i); | |
walk = walk->next; | |
} | |
} | |
TEST(LinkedListTest, CopyConstructor) { | |
LinkedList org = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
std::size_t size = org.size(); | |
LinkedList copy(org); | |
LinkedList::Node *org_walk = org.head(); | |
LinkedList::Node *copy_walk = copy.head(); | |
ASSERT_EQ(org.size(), copy.size()); | |
while (org_walk != nullptr && | |
copy_walk != nullptr && | |
org_walk->value == copy_walk->value) { | |
org_walk = org_walk->next; | |
copy_walk = copy_walk->next; | |
size--; | |
} | |
ASSERT_EQ(size, 0) << "Copied array is not equal"; | |
} | |
TEST(LinkedListTest, AddLast) { | |
std::vector<int> elems = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | |
LinkedList list; | |
for (int i = 0; i < elems.size(); i++) { | |
LinkedList::Node *walk; | |
list.insert_tail(elems[i]); | |
ASSERT_EQ(list.size(), i + 1); | |
walk = list.head(); | |
for (int j = 0; j < list.size(); j++) { | |
ASSERT_EQ(walk->value, elems[j]); | |
walk = walk->next; | |
} | |
} | |
} | |
TEST(LinkedListTest, AddFirst) { | |
std::vector<int> elems = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | |
LinkedList list; | |
for (int i = 0; i < elems.size(); i++) { | |
LinkedList::Node *walk; | |
list.insert_head(elems[i]); | |
ASSERT_EQ(list.size(), i + 1); | |
walk = list.head(); | |
for (int j = (int) list.size() - 1; j >= 0; j--) { | |
ASSERT_EQ(walk->value, elems[j]); | |
walk = walk->next; | |
} | |
} | |
} | |
TEST(LinkedListTest, AddAfter) { | |
LinkedList list = {1}; | |
LinkedList::Node *node; | |
node = list.head(); | |
list.insert_after(node, 2); | |
ASSERT_EQ(list.size(), 2); | |
ASSERT_EQ(list.head()->value, 1); | |
ASSERT_EQ(list.head()->next->value, 2); | |
node = list.head()->next; | |
list.insert_after(node, 3); | |
ASSERT_EQ(list.size(), 3); | |
ASSERT_EQ(list.head()->value, 1); | |
ASSERT_EQ(list.head()->next->value, 2); | |
ASSERT_EQ(list.head()->next->next->value, 3); | |
node = list.head(); | |
list.insert_after(node, 4); | |
ASSERT_EQ(list.size(), 4); | |
ASSERT_EQ(list.head()->value, 1); | |
ASSERT_EQ(list.head()->next->value, 4); | |
ASSERT_EQ(list.head()->next->next->value, 2); | |
ASSERT_EQ(list.head()->next->next->next->value, 3); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment