Skip to content

Commit

Permalink
update(linkedlist.h): use typedes
Browse files Browse the repository at this point in the history
Signed-off-by: Rohith-Raju <[email protected]>

update(LinkedList): added more tests
Signed-off-by: Rohith-Raju <[email protected]>
  • Loading branch information
Rohith-Raju committed May 11, 2024
1 parent 1cba9f1 commit e61d85c
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 43 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Build
on:
push:
jobs:
build-project:
name: Build Project
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
with:
submodules: true

- name: Configure Project
uses: threeal/[email protected]
with:
c-compiler: gcc
cxx-compiler: g++
cxx-flags: "--std=c++17"

- name: Build Project
run: cmake --build build

- name: Run-Tests
run: ./build/testing/LinkedList_test
30 changes: 30 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/opt/homebrew/opt/llvm/bin/clang",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"--std=c++17"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.27)

project(CppTemplation)
project(LinkedList)

set(CMAKE_CXX_STANDARD 17)

Expand All @@ -11,4 +11,4 @@ include_directories(include)
add_subdirectory(testing)

# Sources
add_executable(CppTemplation main.cpp)
add_executable(LinkedList main.cpp)
127 changes: 94 additions & 33 deletions include/LinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ template <class T> class LinkedList {
};
};
size_t size_counter = 0;
LinkedList<T>::Node *middle(LinkedList<T>::Node *head);
LinkedList<T> split(LinkedList<T>::Node *);
template <class function>
LinkedList<T> &merge(LinkedList<T> &list, function &&func);

template <class function>
LinkedList<T>::Node *merge_sort(Node *left, Node *right, function &&func);
void deep_clean(Node *&list);
LinkedList<T> &clean();

public:
Node *head;
Expand All @@ -36,6 +27,15 @@ template <class T> class LinkedList {
class forward_it;
class reverse_it;

/* Type definations */
typedef LinkedList<T> __self;
typedef typename LinkedList<T>::Node node_type;
typedef T value_type;
typedef T &refernece;
typedef T *pointer;
typedef forward_it iterator;
typedef reverse_it reverse_iterator;

// Default constructor
LinkedList();

Expand All @@ -48,32 +48,48 @@ template <class T> class LinkedList {
// Move constructor
LinkedList(LinkedList &&other);

// Destructor
~LinkedList();

/* Iterators */
forward_it begin();
forward_it end();
iterator begin();
iterator end();

reverse_it rbegin();
reverse_it rend();
reverse_iterator rbegin();
reverse_iterator rend();

/* Mutator methods */
void reverse();

void insert_first(Node *node);
bool is_empty();
void push_back(T data);
LinkedList<T> &sort();
template <class function> LinkedList<T> &sort(function &&func);
bool delete_at(size_t pos);

/* Getters */
T get_head_val();
T get_tail_val();
Node *at(size_t pos);
size_t size();

/* LinkedList operators */
/* Merge sort and helper */
LinkedList<T> &sort();
template <class function> LinkedList<T> &sort(function &&func);

LinkedList<T>::Node *middle(LinkedList<T>::Node *head);
LinkedList<T> split(LinkedList<T>::Node *);
template <class function>
LinkedList<T> &merge(LinkedList<T> &list, function &&func);

template <class function>
LinkedList<T>::Node *merge_sort(Node *left, Node *right, function &&func);
void deep_clean(Node *&list);
LinkedList<T> &clean();

/* Genaral purpose */
bool is_empty();
bool delete_at(size_t pos);

/* LinkedList operator overloads */
bool operator==(LinkedList<T> &right);
bool operator!=(LinkedList<T> &right);

Node *operator[](size_t pos);

class forward_it {
Expand Down Expand Up @@ -118,26 +134,30 @@ template <class T> class LinkedList {
};
};

template <typename T>
typename LinkedList<T>::forward_it LinkedList<T>::begin() {
return forward_it(head);
/* LinkedList implementation starts from here */

template <typename T> typename LinkedList<T>::iterator LinkedList<T>::begin() {
return iterator(head);
}

template <typename T> typename LinkedList<T>::forward_it LinkedList<T>::end() {
return forward_it(nullptr);
template <typename T> typename LinkedList<T>::iterator LinkedList<T>::end() {
return iterator(nullptr);
}

template <typename T>
typename LinkedList<T>::reverse_it LinkedList<T>::rbegin() {
typename LinkedList<T>::reverse_iterator LinkedList<T>::rbegin() {
LinkedList<T> *temp = new LinkedList<T>(*this);
temp->reverse();
return reverse_it(temp);
return reverse_iterator(temp);
}

template <typename T> typename LinkedList<T>::reverse_it LinkedList<T>::rend() {
template <typename T>
typename LinkedList<T>::reverse_iterator LinkedList<T>::rend() {
return reverse_it();
}

/******** Constructors *************/

template <typename T>
LinkedList<T>::LinkedList() : head(nullptr), tail(nullptr) {}

Expand Down Expand Up @@ -180,6 +200,11 @@ template <typename T> LinkedList<T> &LinkedList<T>::clean() {
return *this;
}

/*
* reverse() reverses all the elements where head = prev_tail
* and tail = prev_head
*/

template <typename T> void LinkedList<T>::reverse() {
Node *first_ptr = nullptr;
Node *second_ptr = head;
Expand All @@ -193,6 +218,9 @@ template <typename T> void LinkedList<T>::reverse() {
head = first_ptr;
}

/*
* deep_clean() drills into the entire list and removes all the elements
*/
template <typename T> void LinkedList<T>::deep_clean(Node *&list) {
if (list != tail) {
deep_clean(list->next);
Expand All @@ -203,17 +231,28 @@ template <typename T> void LinkedList<T>::deep_clean(Node *&list) {
return;
}

/*
* insert_first() inserts element only when there are no elements in the list
*/
template <typename T> void LinkedList<T>::insert_first(Node *node) {
head = node;
size_counter = 1;
if (tail == nullptr) {
tail = head;
if (!is_empty()) {
head = node;
size_counter = 1;
if (tail == nullptr) {
tail = head;
}
}
return;
}

/*
* is_empty() returns true if the list is empty
*/
template <typename T> bool LinkedList<T>::is_empty() { return head == nullptr; }

/*
* push_back() append new element to the end of the list
*/
template <typename T> void LinkedList<T>::push_back(T data) {
Node *node = new Node(data);
if (is_empty()) {
Expand All @@ -224,9 +263,14 @@ template <typename T> void LinkedList<T>::push_back(T data) {
tail->next = node;
tail = node;
}

/*
* size() returns the size of the list
*/
template <typename T> size_t LinkedList<T>::size() { return size_counter; }

/*
* at() returns the node containing the data at a given position
*/
template <typename T>
typename LinkedList<T>::Node *LinkedList<T>::at(size_t pos) {
if (pos >= size())
Expand All @@ -244,6 +288,9 @@ typename LinkedList<T>::Node *LinkedList<T>::operator[](size_t pos) {
return at(pos);
}

/*
* delete_at() remove element in the given pos
*/
template <typename T> bool LinkedList<T>::delete_at(size_t pos) {
if (pos >= size()) {
throw std::out_of_range("Cannot delete. Index out of bounds");
Expand Down Expand Up @@ -279,13 +326,27 @@ template <typename T> bool LinkedList<T>::delete_at(size_t pos) {
}
}

/*
* get_head_val() return the value of head
*/
template <typename T> T LinkedList<T>::get_head_val() { return head->data; }

/*
* get_tail_val() return the value of tail
*/
template <typename T> T LinkedList<T>::get_tail_val() { return tail->data; }

/*
* sort() sorts all the elements in the list (default accending)
*/
template <typename T> LinkedList<T> &LinkedList<T>::sort() {
return sort([](T leftVal, T rightVal) { return leftVal < rightVal; });
}

/*
* sort() sorts all the elements in the list by passing in a anonymous function
*/

template <typename T>
template <class function>
LinkedList<T> &LinkedList<T>::sort(function &&func) {
Expand Down
6 changes: 0 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
//
// Created by Rohith on 1/10/24.
//
#include "LinkedList.h"
#include "iostream"

int main() {
LinkedList<int> list = {1, 2, 3, 4, 5};
for (auto it = list.rbegin(); it != list.rend(); it++) {
std::cout << *it;
}
}
8 changes: 7 additions & 1 deletion testing/TestLinkedList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ TEST(LinkedList, TestDeleteAtPosition) {
ASSERT_EQ(ptr->data, 3);
list.delete_at(2);
ASSERT_NE(ptr->data, 3);
// todo: write SIGSEGV test case
}

TEST(LinkedList, TestDeleteAtEnd) {
LinkedList list = {1, 2, 3, 4, 5};
ASSERT_EQ(list.get_tail_val(), 5);
list.delete_at(4);
ASSERT_EQ(list.get_tail_val(), 4);
}

TEST(LinkedList, TestCustomSort) {
Expand Down
2 changes: 1 addition & 1 deletion testing/lib
Submodule lib updated 32 files
+0 −43 .github/workflows/gtest-ci.yml
+4 −0 MODULE.bazel
+8 −9 docs/primer.md
+2 −2 docs/reference/mocking.md
+4 −2 docs/reference/testing.md
+33 −0 fake_fuchsia_sdk.bzl
+4 −3 googlemock/CMakeLists.txt
+6 −6 googlemock/include/gmock/gmock-actions.h
+74 −74 googlemock/include/gmock/gmock-matchers.h
+3 −2 googlemock/include/gmock/gmock-more-actions.h
+8 −6 googlemock/include/gmock/internal/gmock-internal-utils.h
+4 −4 googlemock/include/gmock/internal/gmock-port.h
+3 −2 googlemock/src/gmock-internal-utils.cc
+26 −0 googlemock/test/gmock-more-actions_test.cc
+1 −1 googletest/include/gtest/gtest-assertion-result.h
+4 −4 googletest/include/gtest/gtest-param-test.h
+65 −61 googletest/include/gtest/gtest-typed-test.h
+19 −3 googletest/include/gtest/gtest.h
+7 −1 googletest/include/gtest/internal/gtest-filepath.h
+24 −62 googletest/include/gtest/internal/gtest-internal.h
+76 −74 googletest/include/gtest/internal/gtest-param-util.h
+2 −0 googletest/include/gtest/internal/gtest-port-arch.h
+34 −13 googletest/include/gtest/internal/gtest-port.h
+10 −10 googletest/src/gtest-death-test.cc
+28 −16 googletest/src/gtest-internal-inl.h
+34 −19 googletest/src/gtest-port.cc
+119 −98 googletest/src/gtest.cc
+1 −0 googletest/test/googletest-color-test.py
+15 −0 googletest/test/googletest-json-output-unittest.py
+3 −0 googletest/test/gtest_json_test_utils.py
+10 −22 googletest/test/gtest_unittest.cc
+6 −0 googletest_deps.bzl

0 comments on commit e61d85c

Please sign in to comment.