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
19 changes: 19 additions & 0 deletions hw.sortList/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.25)
project(rewrite C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

# Создаём библиотеку из файла в поддиректории
add_library(sortList sortList/sortList.c)

# Создаём исполняемый файл из файла в другой поддиректории
add_executable(main hw.sortList/main.c)

# Линкуем библиотеку
target_link_libraries(main PRIVATE sortList)

# Добавляем путь к заголовочным файлам библиотеки
target_include_directories(main PRIVATE
${CMAKE_SOURCE_DIR}/sortList
)
38 changes: 38 additions & 0 deletions hw.sortList/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "../sortList/sortList.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
List* list = newList();
int value = 0;

printf("Ведите номер операции: 0 - выйти, 1 - добавить значение, 2 - удалить значение, 3 - распечатать список: ");
int operation = 0;
scanf("%d", &operation);
while (operation != 0) {
if (operation == 1) {
printf("Ведите, какое значение вставить: ");
scanf("%d", &value);
insert(list, value);
} else if (operation == 2) {
printf("Ведите, какое значение удалить: ");
scanf("%d", &value);
if (deleteNode(list, value)) {
printf("Элемент удален\n");
} else {
printf("Удалить элемент невозможно\n");
}
} else if (operation == 3) {
printList(list);
} else {
printf("Такой операции нет\n");
}
printf("Ведите номер операции: 0 - выйти, 1 - добавить значение, 2 - удалить значение, 3 - распечатать список: ");
scanf("%d", &operation);
}
printf("Вы вышли\n");
deleteList(list);
return 0;
}
104 changes: 104 additions & 0 deletions sortList/sortList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "sortList.h"
#include <stdio.h>
#include <stdlib.h>

// элемент списка
typedef struct ListNode {
// значение
int value;
// ссылка на следующую "ячейку"
struct ListNode* next;
} ListNode;

typedef struct List {
ListNode* head;
} List;

List* newList()
{
List* list = (List*)malloc(sizeof(List));
list->head = NULL;
return list;
}

// положить элемент в соответствии с сортировкой
void insert(List* list, int value)
{
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
newNode->value = value;
newNode->next = NULL;

// Если список пуст или новый элемент меньше головы
if (list->head == NULL || value < list->head->value) {
newNode->next = list->head;
list->head = newNode;
return;
}

// поиск того, куда вставить новый элемент
ListNode* current = list->head;
while (current->next != NULL && current->next->value < value) {
current = current->next;
}

// вставка
newNode->next = current->next;
current->next = newNode;
}

// проверка на возможность удаления элемента
bool deleteNode(List* list, int value)
{
// если список пустой
if (list->head == NULL) {
return false;
}

// если элемент это голова
if (list->head->value == value) {
ListNode* temprery = list->head;
list->head = list->head->next;
free(temprery);
return true;
}

// поиск элемента для удаления
ListNode* current = list->head;
while (current->next != NULL && current->next->value != value) {
current = current->next;
}

// Если элемент не найден
if (current->next == NULL) {
return false;
}

// удаление элемента
ListNode* temprery = current->next;
current->next = current->next->next;
free(temprery);
return true;
}

// вывод списка
void printList(List* list)
{
ListNode* current = list->head;
while (current != NULL) {
printf("%d ", current->value);
current = current->next;
}
printf("\n");
}

// удаление списка
void deleteList(List* list)
{
ListNode* current = list->head;
while (current != NULL) {
ListNode* temprery = current;
current = current->next;
free(temprery);
}
list->head = NULL;
}
10 changes: 10 additions & 0 deletions sortList/sortList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include <stdbool.h>

typedef struct List List;

List* newList();
void insert(List* list, int value);
bool deleteNode(List* list, int value);
void printList(List* list);
void deleteList(List* list);