Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
14 changes: 14 additions & 0 deletions hw.sortList/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.25)
project(rewrite C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

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

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

# Линкуем библиотеку
target_link_libraries(main PRIVATE sortList)
86 changes: 86 additions & 0 deletions hw.sortList/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "sortList.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

bool testDeleteEmptyNode()
{
List* list = newList();
return !deleteNode(list, 123313);
}

bool testDeleteNode()
{
List* list = newList();
insert(list, 5);
return deleteNode(list, 5);
}

bool testSorted1(){
List* list = newList();
insert(list, 5);
insert(list, 6);
insert(list, -1111);
int array[] = {-1111, 5, 6};
return equalToArray(list, array, 3);
}

bool testSorted2(){
List* list = newList();
insert(list, 5);
insert(list, 6);
insert(list, -1111);
int array[] = {-1111, 5};
return !equalToArray(list, array, 2);
}

bool test()
{
return (testSorted1() && testDeleteEmptyNode() && testSorted2() && testDeleteNode());
}

int main(int argc, char** argv)
{

if (argc == 2 && strcmp(argv[1], "--test") == 0) {
if (!test()) {
printf("Test failed\n");
return 1;
} else {
printf("Test passed\n");
return 0;
}
}

List* list = newList();
int value = 0;

const char* helpMassage = "Ведите номер операции: 0 - выйти, 1 - добавить значение, 2 - удалить значение, 3 - распечатать список: ";
int operation = 0;
do {
printf("%s", helpMassage);
int operation = 0;
scanf("%d", &operation);
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");
}
} while (operation != 0);
printf("Вы вышли\n");
deleteList(list);
return 0;
}
131 changes: 131 additions & 0 deletions hw.sortList/sortList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include "sortList.h"
#include <stdio.h>
#include <stdlib.h>

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

typedef struct List {
ListNode* head;
int size;
} List;

List* newList()
{
List* list = (List*)malloc(sizeof(List));
list->head = NULL;
list->size = 0;
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;
list->size++;
return;
}

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

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

// проверка на возможность удаления элемента
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);
list->size--;
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);
list->size--;
return true;
}

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

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

// удаление списка
void deleteList(List* list)
{
ListNode* current = list->head;
while (current != NULL) {
ListNode* temprery = current;
current = current->next;
free(temprery);
}
free(list);
}

bool equalToArray(List* list, int* array, int arrayLength){
ListNode* current = list->head;
int i = 0;
if (list->size != arrayLength){
return false;
}

while (i < arrayLength) {
if (current-> value != array[i]){
return false;
}
current = current->next;
i++;
}
return true;
}
12 changes: 12 additions & 0 deletions hw.sortList/sortList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#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);
bool isEmpty(List* list);
bool equalToArray(List* list, int* array, int arrayLength);