Skip to content
This repository was archived by the owner on Apr 6, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/collections/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ cc_library(
hdrs = ["vector.h"],
visibility = ["//visibility:public"],
)

cc_library(
name = "linked-list",
hdrs = ["linked-list.h"],
visibility = ["//visibility:public"],
)
50 changes: 50 additions & 0 deletions src/collections/linked-list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef _LINKED_LIST_
#define _LINKED_LIST_

#include <algorithm>

template <typename T>
class LinkedList {
public:
LinkedList() = default;
void push(T data) {
auto node = new Node(data);
if (this->tail == nullptr) {
this->tail = node;
this->head = this->tail;
} else {
this->tail->next = node;
this->tail = node;
}
}

T& operator[](size_t idx) {
auto walk = this->head;
if (walk == nullptr) {
throw "index out of range";
}

while (walk != nullptr && idx != 0) {
walk = walk->next;
idx--;
}

if (idx != 0) {
throw "index out of range";
}
return walk->data;
}

private:
struct Node {
T data;
Node* next = nullptr;
Node* prev = nullptr;
Node() = default;
Node(T data) : data(data) {}
};

Node* head = nullptr;
Node* tail = nullptr;
};
#endif /* ifndef _LINKED_LIST_ */
1 change: 1 addition & 0 deletions tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cc_test(
name = "tests",
srcs = glob(["*.cc"]),
deps = [
"//src/collections:linked-list",
"//src/collections:vector",
"//src/math:number-theory",
"@googletest//:gtest_main",
Expand Down
18 changes: 18 additions & 0 deletions tests/test-linked-list.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <gtest/gtest.h>

#include <vector>

#include "src/collections/linked-list.h"

TEST(TestLinkedList, TestPush) {
LinkedList<int> list;
auto cases = std::vector<int>({1, 2, 3, 4, 5});

for (auto i : cases) {
list.push(i);
}

for (size_t i = 0; i < cases.size(); ++i) {
EXPECT_EQ(cases[i], list[i]);
}
}
5 changes: 3 additions & 2 deletions tests/test-number-theory.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "src/math/number-theory.h"

#include <gtest/gtest.h>

#include <utility>
#include <vector>

#include "src/math/number-theory.h"

TEST(TestGcd, TestCoprime) {
std::vector<std::pair<int, int>> cases = {
std::pair<int, int>(1, 7), std::pair<int, int>(13, 7),
Expand Down
1 change: 1 addition & 0 deletions tests/test-vector.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <gtest/gtest.h>

#include "src/collections/vector.h"

TEST(TestVec, TestSize) {
Expand Down