commit 8a80b3f8b97dbde5dc65c61bdf763750f28136fb Author: clzhao20 Date: Sun Apr 7 18:44:19 2024 +0800 🎉 init: cmake build for data structures diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..e5d2ff6 --- /dev/null +++ b/.clang-format @@ -0,0 +1,4 @@ +--- +Language: Cpp +BasedOnStyle: LLVM +TabWidth: 2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a0aa113 --- /dev/null +++ b/.gitignore @@ -0,0 +1,36 @@ +build/ +dist/ + + +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..7a4e53d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,17 @@ +cmake_minimum_required(VERSION 3.15.0) +project(linux-data-structure VERSION 0.1.0 LANGUAGES C CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + + +set(INCLUDE_DIR "${CMAKE_SOURCE_DIR}/include") +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +include_directories( + ${INCLUDE_DIR} + ${CMAKE_BINARY_DIR}/include +) + +add_subdirectory(src) +add_subdirectory(include) diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt new file mode 100644 index 0000000..ed43def --- /dev/null +++ b/include/CMakeLists.txt @@ -0,0 +1,4 @@ +target_sources( + list + PUBLIC list.h +) \ No newline at end of file diff --git a/include/list.h b/include/list.h new file mode 100644 index 0000000..98f2c95 --- /dev/null +++ b/include/list.h @@ -0,0 +1,35 @@ +#ifndef __LIST__H__ +#define __LIST__H__ + +#include +struct Node { + size_t data_p; + Node *next; +}; + +class List { + List(); + ~List(); + void push_back(int data); + void push_front(int data); + void insert(int data, int index); + void pop_back(); + void pop_front(); + void remove(int index); + void print(); + int size(); + bool empty(); + void clear(); + int front(); + int back(); + + +private: + Node *head_; + Node *tail_; + int count_; + + +}; + +#endif //!__LIST__H__ \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..2efb8b9 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,6 @@ +## 添加list library +## 设置include + +add_library(list STATIC) +target_sources(list PRIVATE list.cpp) +target_include_directories(list PUBLIC ${INCLUDE_DIR}) diff --git a/src/list.cpp b/src/list.cpp new file mode 100644 index 0000000..4423c4a --- /dev/null +++ b/src/list.cpp @@ -0,0 +1,32 @@ +#include "list.h" + +List::List(): head_(nullptr), tail_(nullptr), count_(0) {} + +List::~List() { + clear(); +} + +void List::push_back(int data) { + Node *new_node = new Node; + new_node->data_p = data; + new_node->next = nullptr; + if (head_ == nullptr) { + head_ = new_node; + tail_ = new_node; + } else { + tail_->next = new_node; + tail_ = new_node; + } + count_++; +} + +void List::push_front(int data) { + Node *new_node = new Node; + new_node->data_p = data; + new_node->next = head_; + head_ = new_node; + if (tail_ == nullptr) { + tail_ = new_node; + } + count_++; +}