Skip to content
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
12 changes: 1 addition & 11 deletions .github/workflows/build-and-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,4 @@ jobs:
- uses: actions/checkout@v5
- name: Check format
run: |
find . -path ./build -prune -o -type f -name '*.[c|h]' -print | xargs clang-format-18 --style=file --dry-run -Werror
- name: Configure
run: |
# -DCMAKE_EXPORT_COMPILE_COMMANDS=ON is important for clang-tidy
cmake . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
- name: Build
run: |
cmake --build build
- name: Lint
run: |
run-clang-tidy-18 -p=build
find . -path ./build -prune -o -type f -name '*.[c|h]' -print | xargs clang-format-18 --style=file --dry-run -Werror
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ project(C_Homework C)

add_subdirectory(src/CheckHowLintersWork)

add_subdirectory(src/ListTasks1)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_compile_options(-Wall -Wextra -Wpedantic)
5 changes: 5 additions & 0 deletions src/ListTasks1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_library(listTask list.c)

add_executable(sortedListTask sortedListTask.c)

target_link_libraries(sortedListTask PRIVATE listTask)
169 changes: 169 additions & 0 deletions src/ListTasks1/list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
#include "list.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct ListNode ListNode;

// структура обычного элемента в списке
struct ListNode {
int value;
struct ListNode* next;
};

// структура содержащая указатель на первый элемент списка
struct List {
ListNode* head;
};

List* newList()
{
List* list = calloc(1, sizeof(*list));
return list;
}

bool insert(List* list, int index, int value)
{
if (list == NULL || index < 0) {
return false;
}
if (index == 0) {
ListNode* newNode = malloc(sizeof(ListNode));
newNode->value = value;
newNode->next = list->head;
list->head = newNode;
return true;
}
ListNode* current = list->head;
int counter = 0;
while (current != NULL) {
if (counter == index - 1) {
ListNode* newNode = malloc(sizeof(ListNode));
newNode->value = value;
newNode->next = current->next;
current->next = newNode;
return true;
}
current = current->next;
counter++;
}
return false;
}

int get(List* list, int index)
{
if (list == NULL || isEmpty(list) || index < 0)
return -1;
if (index == 0) {
return list->head->value;
}
ListNode* current = list->head;
int counter = 0;
while (current != NULL) {
if (counter == index) {
return current->value;
}
current = current->next;
counter++;
}
return -1;
}

bool removeElement(List* list, int index)
{
if (list == NULL || isEmpty(list) || index < 0) {
return false;
}
ListNode* current = list->head;
if (index == 0) {
list->head = current->next;
free(current);
return true;
}
int counter = 0;
while (current != NULL) {
if (counter == index - 1) {
if (current->next == NULL)
return false;
ListNode* nodeToDelete = current->next;
current->next = nodeToDelete->next;
free(nodeToDelete);
return true;
}
current = current->next;
counter++;
}
return false;
}

bool deleteList(List* list)
{
if (list == NULL || isEmpty(list)) {
return false;
}
while (!isEmpty(list)) {
removeElement(list, 0);
}
free(list);
return true;
}

void printList(List* list)
{
if (list == NULL || isEmpty(list)) {
printf("Список пустой\n");
return;
}

ListNode* current = list->head;
while (current != NULL) {
printf("%d ", current->value);
current = current->next;
}
printf("\n");
}

bool isEmpty(List* list) { return list->head == NULL; }

bool insert_sorted_in_ascending_order(List* list, int value)
{
if (list == NULL) {
return false;
}
// случай еще незаполненного списка
if (isEmpty(list)) {
return insert(list, 0, value);
}
ListNode* current = list->head;
int counter = 0;
// если элемент самый маленький и его надо вставить в начало
if (current->value > value) {
return insert(list, 0, value);
}
while (current != NULL && current->next != NULL) {
if ((current->next->value > value) && (current->value <= value)) {
return (insert(list, counter + 1, value));
}
current = current->next;
counter++;
}
// если программа достигла этой строчки, то значит что элемент больше всех
// остальных
return insert(list, counter + 1, value);
}

int getLength(List* list)
{
if (list == NULL || isEmpty(list)) {
return -1;
}
int counter = 0;
ListNode* current = list->head;
do {
counter++;
current = current->next;
} while (current != NULL);
return counter;
}

bool oneElement(List* list) { return list->head->next == NULL; }
81 changes: 81 additions & 0 deletions src/ListTasks1/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#pragma once
#include <stdbool.h>

// структура содержащая указатель на список
typedef struct List List;

/*
* функция создания нового списка
* ничего не принимает
* возвращает указатель на список
*/
List* newList();

/*
* функция вставки элемента по заданному индексу
* принимает на вход указатель на список, желаемый индекс и значение
* возвращает булевое значение, сообщающее, удачно ли прошла операция
*/
bool insert(List* list, int index, int value);

/*
* функция просмотра элемента списка по индексу
* принимает указатель на список и индекс
* возвращает value элемента
* если что-то пошло не так, вернет -1
*/
int get(List* list, int index);

/*
* функция удаления элемента по индексу
* принимает указатель на список и индекс элемента
* возвращает булевое значние, сообщающее, удачно ли прошла операция
* внутри себя освобождает память удаляемого элемента
*/
bool removeElement(List* list, int index);

/*
* функция удаления всего списка
* принимает указатель на список
* внутри себя вызывает функцию removeElement
* возвращает булевое значение, удачно ли прошла операция
*/
bool deleteList(List* list);

/*
* функция вывода списка в консоль
* принимает только указатель на список
* ничего не возвращает
* печатает элементы списка через пробел
* в случае переданного пустого или несуществующего списка сообщает об этом пользователю
*/
void printList(List* list);

/*
* функция которая вставляет элемент в определенное место так, чтобы список всегда оставался отсортированным
* принимает значение и список
* возвращает булевое значение об успешной или неуспешной вставке
*/
bool insert_sorted_in_ascending_order(List* list, int value);

/*
* функция проверки, пустой ли список
* возвращает true если пустой, false, если нет
* на вход принимает указатель на список
*/
bool isEmpty(List* list);

/*
* функция, возвращающая длину списка
* принимает на вход указатель на список
* проходится по всем элементам
* возвращает его длину
*/
int getLength(List* list);

/*
* функция проверки, один ли элемент в списке
* возвращает true если да, false, если нет
* на вход принимает указатель на список
*/
bool oneElement(List* list);
Loading