Skip to content
Open
10 changes: 10 additions & 0 deletions src/hw6_sortList/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 4.0)
project(hw6_sortList C)

set(CMAKE_C_STANDARD 17)

add_executable(sortList
interactiveMain.c
sortList.c
)

62 changes: 62 additions & 0 deletions src/hw6_sortList/interactiveMain.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdio.h>
#include "sortList.h"

int main(void)
{
struct List* list = createEmptyList();
int command, inputElement;

while (1) {
printf("Команды:\n");
printf("0 - Выйти\n");
printf("1 - Добавить значение в сортированный список\n");
printf("2 - Удалить значение из списка\n");
printf("3 - Распечатать список\n");
printf("Выберите команду (0,1,2,3): ");

if (scanf("%d", &command) != 1) {
while (getchar() != '\n'); // очистка ввода
continue;
}

switch (command) {
case 0:
printf("Совершён выход\n");
deleteList(list);
return 0;

case 1:
printf("Введите значение для добавления: ");

if (scanf("%d", &inputElement) != 1) {
printf("Ошибка: вводите только цифры\n");
while (getchar() != '\n');
continue;
}
insertElement(list, inputElement);
break;

case 2:
printf("Введите значение для удаления:");

if (scanf("%d", &inputElement) != 1) {
printf("Ошибка: вводите только цифры\n");
while (getchar() != '\n');
continue;
}
if (deleteElement(list, inputElement)) {
printf("Значение было удалено\n");
} else {
printf("Элемент не найден\n");
}
break;

case 3:
printList(list);
break;

default:
printf("Введена неподдерживаемая команда: можно только 0,1,2,3\n");
}
}
}
110 changes: 110 additions & 0 deletions src/hw6_sortList/sortList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <stdio.h>
#include <stdlib.h>
#include "sortList.h"

// Создание пустого списка
struct List* createEmptyList()
{
struct List* list = malloc(sizeof(struct List));
if (!list) {
fprintf(stderr, "Ошибка выделения памяти\n");
exit(1);
}
list->head = NULL;
list->size = 0;
return list;
}

// Удаление всего списка
void deleteList(struct List* list)
{
struct Node* curr = list->head;
while (curr) {
struct Node* tempNode = curr;
curr = curr->next;
free(tempNode);
}

free(list);
}

// Вставка элемента
void insertElement(struct List* list, int element)
{
struct Node* newNode = malloc(sizeof(struct Node));
if (!newNode) {
printf("Ошибка выделения памяти\n");
return;
}

newNode->data = element;
newNode->next = NULL;

if (!list->head || list->head->data >= element) {
newNode->next = list->head;
list->head = newNode;
list->size++;
return;
}

// Поиск места
struct Node* curr = list->head;
while (curr->next && curr->next->data < element) {
curr = curr->next;
}

newNode->next = curr->next;
curr->next = newNode;
list->size++;
}

// Удаление элемента
int deleteElement(struct List* list, int element)
{
if (!list->head) {
return 0;
}

if (list->head->data == element) {
struct Node* tempNode = list->head;
list->head = list->head->next;
free(tempNode);
list->size--;
return 1;
}

// Поиск элемента
struct Node* curr = list->head;
while (curr->next && curr->next->data != element) {
curr = curr->next;
}

if (!curr->next) {
return 0; // не найденн
}

struct Node* tempNode = curr->next;
curr->next = tempNode->next;
free(tempNode);
list->size--;
return 1;
}

// Печать списка
void printList(struct List* list)
{
if (!list->head) {
printf("Список пуст\n");
return;
}

struct Node* curr = list->head;
printf("[");
while (curr) {
printf("%d", curr->data);
if (curr->next) printf(", ");
curr = curr->next;
}
printf("]\n");
}

28 changes: 28 additions & 0 deletions src/hw6_sortList/sortList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

struct Node {
int data;
struct Node* next;
};

struct List {
struct Node* head;
int size;
};

// Создание пустого списка
struct List* createEmptyList();

// Реализация команды 0 - выйти:
// Удаление всего списка для освобождения и выхода
void deleteList(struct List* list);

// Реализация команды 1 - добавить значениe в сортированный список:
void insertElement(struct List* list, int element);

// Реализация команды 2 – удалить значение из списка:
int deleteElement(struct List* list, int element);

// Реализация команды 3 – распечатать список:
void printList(struct List* list);