🎉 init: cmake build for data structures

This commit is contained in:
clzhao20 2024-04-07 18:44:19 +08:00
commit 8a80b3f8b9
7 changed files with 134 additions and 0 deletions

4
.clang-format Normal file
View File

@ -0,0 +1,4 @@
---
Language: Cpp
BasedOnStyle: LLVM
TabWidth: 2

36
.gitignore vendored Normal file
View File

@ -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

17
CMakeLists.txt Normal file
View File

@ -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)

4
include/CMakeLists.txt Normal file
View File

@ -0,0 +1,4 @@
target_sources(
list
PUBLIC list.h
)

35
include/list.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef __LIST__H__
#define __LIST__H__
#include <cstddef>
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__

6
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
## list library
## include
add_library(list STATIC)
target_sources(list PRIVATE list.cpp)
target_include_directories(list PUBLIC ${INCLUDE_DIR})

32
src/list.cpp Normal file
View File

@ -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_++;
}