diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7059ea4..7f9d514 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,7 +16,7 @@ jobs: strategy: matrix: # Add your projects from 'data-structures' - project: [LinkedList, DoublyLinkedList, Queue, Stack] + project: [Array, LinkedList, DoublyLinkedList, Queue, Stack] steps: # Step 1: Checkout the repository diff --git a/README.md b/README.md index f35cb48..2184e1b 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Remember that each data has its own trade-offs. And you need to pay attention mo `B` - Beginner, `A` - Advanced +* `B` [Array](data-structures/Array) * `B` [Linked List](data-structures/LinkedList) * `B` [Doubly Linked List](data-structures/DoublyLinkedList) * `B` [Queue](data-structures/Queue) diff --git a/README.zh-CN.md b/README.zh-CN.md index 6c8e9d2..309b645 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -18,6 +18,7 @@ `B` - 初学者, `A` - 进阶 +* `B` [数组](data-structures/Array) * `B` [单链表](data-structures/LinkedList) * `B` [双链表](data-structures/DoublyLinkedList) * `B` [队列](data-structures/Queue) diff --git a/data-structures/Array/CMakeLists.txt b/data-structures/Array/CMakeLists.txt new file mode 100644 index 0000000..e86ea54 --- /dev/null +++ b/data-structures/Array/CMakeLists.txt @@ -0,0 +1,21 @@ +# Specify the minimum version of CMake required +cmake_minimum_required(VERSION 3.10) + +# Project name and version +project(ArrayProject VERSION 1.0) + +# Set C++ standard +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +# Include directories +include_directories(include) + +# Output executables to the 'bin' directory +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin) + +# Add the test executable +add_executable(test_Array __test__/test_Array.cpp) + +# Link the Array library to the test executable +target_link_libraries(test_Array) diff --git a/data-structures/Array/README.md b/data-structures/Array/README.md new file mode 100644 index 0000000..6acaf79 --- /dev/null +++ b/data-structures/Array/README.md @@ -0,0 +1,3 @@ +# 数组 + +![数组](./assets/array.png) \ No newline at end of file diff --git a/data-structures/Array/__test__/test_Array.cpp b/data-structures/Array/__test__/test_Array.cpp new file mode 100644 index 0000000..c53899f --- /dev/null +++ b/data-structures/Array/__test__/test_Array.cpp @@ -0,0 +1,49 @@ +#include "../include/Array.h" +#include +#include + +void test_array() { + Array arr_0; + assert(arr_0.size() == 0); + assert(arr_0.empty() == true); + try{ + arr_0.front(); + } catch(const std::underflow_error &e){ + std::cout << "front() throw an exception PASSED! " << e.what() << std::endl; // Expected + } + try{ + arr_0.back(); + } catch(const std::underflow_error &e){ + std::cout << "back() throw an exception PASSED! " << e.what() << std::endl; // Expected + } + + Array arr_5; + assert(arr_5.size() == 5); + assert(arr_5.empty() == false); + try { + assert(arr_5[0] == 0); + assert(arr_5.at(4) == 0); + arr_5.at(5); + } catch (const std::out_of_range& e) { + std::cout << "at() throw an exception PASSED! " << e.what() << std::endl; // Expected + } + for (int i = 0; i < 5; ++i) { + arr_5[i] = i + 1; + } + assert(arr_5.front() == 1); + assert(arr_5.back() == 5); + assert(arr_5.at(2) == 3); + assert(arr_5[3] == 4); + arr_5[3] = 9; + assert(arr_5[3] == 9); + for(int i = 0; i < arr_5.size(); ++i){ + std::cout << arr_5[i] << " "; + } + std::cout << std::endl; + std::cout << "All tests passed!" << std::endl; +} + +int main() { + test_array(); + return 0; +} \ No newline at end of file diff --git a/data-structures/Array/assets/array.png b/data-structures/Array/assets/array.png new file mode 100644 index 0000000..88ad96c Binary files /dev/null and b/data-structures/Array/assets/array.png differ diff --git a/data-structures/Array/build.sh b/data-structures/Array/build.sh new file mode 100755 index 0000000..896590c --- /dev/null +++ b/data-structures/Array/build.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +# Create the build directory if it doesn't exist +if [ ! -d "build" ]; then + mkdir build +fi + +# Run CMake in the build directory +cd build +cmake .. + +# Build the project using make +make + +# Return to the project root directory +cd .. + +# Inform the user where the executable can be found +echo "Build complete. Executable located in the ./bin directory." diff --git a/data-structures/Array/clean.sh b/data-structures/Array/clean.sh new file mode 100755 index 0000000..4659604 --- /dev/null +++ b/data-structures/Array/clean.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# Remove build and bin directories +rm -rf build +rm -rf bin + +echo "Clean complete. Build and bin directories removed." diff --git a/data-structures/Array/include/Array.h b/data-structures/Array/include/Array.h new file mode 100644 index 0000000..c8052d6 --- /dev/null +++ b/data-structures/Array/include/Array.h @@ -0,0 +1,63 @@ +#ifndef ARRAY_H +#define ARRAY_H + +#include +#include + +template +class Array{ + public: + Array(); + ~Array() = default; + public: + // Capacity + bool empty() const {return N == 0;} + size_t size() const {return N;} + // Element access + T& front(); + T& back(); + T& at(const size_t index); + T& operator[](const size_t index); + private: + T _data[N]; +}; + +// Element access +template +T& Array::front(){ + if(N == 0){ + throw std::underflow_error("front(): This Array is empty."); + } + return _data[0]; +} + +template +T& Array::back(){ + if(N == 0){ + throw std::underflow_error("back(): This Array is empty."); + } + return _data[N - 1]; +} + +template +T& Array::at(const size_t index){ + if(index >= N){ + throw std::out_of_range("at(): Index out of range."); + } + return _data[index]; +} + +template +T& Array::operator[](const size_t index){ + return _data[index]; +} + +// Constructor +template +Array::Array(){ + for(size_t i = 0; i < N; ++i){ + _data[i] = T(); + } +} + +#endif // ARRAY_H \ No newline at end of file diff --git a/data-structures/Queue/CMakeLists.txt b/data-structures/Queue/CMakeLists.txt index 7d75f77..f9c0d78 100644 --- a/data-structures/Queue/CMakeLists.txt +++ b/data-structures/Queue/CMakeLists.txt @@ -17,5 +17,5 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin) # Add the test executable add_executable(test_Queue __test__/test_Queue.cpp) -# Link the LinkedList library to the test executable +# Link the Queue library to the test executable target_link_libraries(test_Queue) diff --git a/data-structures/Stack/CMakeLists.txt b/data-structures/Stack/CMakeLists.txt index fe52cb2..5f89ae3 100644 --- a/data-structures/Stack/CMakeLists.txt +++ b/data-structures/Stack/CMakeLists.txt @@ -17,5 +17,5 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin) # Add the test executable add_executable(test_Stack __test__/test_Stack.cpp) -# Link the LinkedList library to the test executable +# Link the Stack library to the test executable target_link_libraries(test_Stack)