-
Notifications
You must be signed in to change notification settings - Fork 0
Домашнее задание 10. Тесты к Сортированному списку. Разгуляева А.И. #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Инструкция по сборке | ||
|
|
||
| Домашнее задание 10 Сортировочная станция | ||
|
|
||
| 1. Откройте терминал и перейдите в директорию с файлами приложения (hw_10_testing) | ||
| 2. Выполните команду компиляции: | ||
| ```console | ||
| gcc -o hw_10_testing hw_10_testingSortList.c hw_10_testingSortListMain.c | ||
| ``` | ||
| 3. Запустите программу: | ||
| ```console | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: так получается довольно странное форматирование. Обычно пишут " |
||
| ./hw_10_testing | ||
| ``` | ||
| - для запуска тестов добавьте флаг --test: | ||
| ```console | ||
| ./hw_10_testing --test | ||
| ``` | ||
| ### Использование | ||
| 1. Выберите нужную команду в меню операций цифрами от 0 до 3 | ||
| 2. Нажмите Enter для завершения ввода | ||
| 3. Программа выполнит команду | ||
| - при вводе "0" программа завершит работу | ||
| - при вводе "1" программа запросит значение для добавления, нужно ввести целое число | ||
| - при вводе "2" программа запросит значение для удаления, нужно ввести целое число | ||
| - при вводе "3" программа выведет содержимое списка | ||
| 4. Продолжайте работу | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не надо копировать файлы. Нужно было вырастить одну ветку от другой и правильно прописать |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| #include "hw_10_testingSortList.h" | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| // создание нового пустого списка | ||
| List* listCreate() | ||
| { | ||
| List* list = (List*)malloc(sizeof(List)); | ||
| if (list == NULL) { | ||
| printf("Ошибка выделения памяти для списка\n"); | ||
| exit(1); | ||
| } | ||
| list->head = NULL; | ||
| list->size = 0; | ||
| return list; | ||
| } | ||
|
|
||
| // вставка элемента в список по заданному индексу | ||
| void listInsert(List* list, int index, int value) | ||
| { | ||
| // проверка корректности индекса | ||
| if (index < 0 || index > list->size) { | ||
| printf("Некорректный индекс для вставки\n"); | ||
| return; | ||
| } | ||
|
|
||
| // создаем новый узел | ||
| Node* newNode = (Node*)malloc(sizeof(Node)); | ||
| if (newNode == NULL) { | ||
| printf("Ошибка выделения памяти для узла\n"); | ||
| return; | ||
| } | ||
| newNode->data = value; | ||
|
|
||
| // вставка в начало списка | ||
| if (index == 0) { | ||
| newNode->next = list->head; | ||
| list->head = newNode; | ||
| } else { | ||
| // поиск позиции для вставки | ||
| Node* current = list->head; | ||
| for (int i = 0; i < index - 1; i++) { | ||
| current = current->next; | ||
| } | ||
| // вставка между узлами | ||
| newNode->next = current->next; | ||
| current->next = newNode; | ||
| } | ||
| list->size++; | ||
| } | ||
|
|
||
| // получение элемента по заданному индексу | ||
| int listGet(List* list, int index) | ||
| { | ||
| // проверка корректности индекса | ||
| if (index < 0 || index >= list->size) { | ||
| printf("Некорректный индекс для получения элемента\n"); | ||
| return -1; | ||
| } | ||
|
|
||
| // поиск нужного узла | ||
| Node* current = list->head; | ||
| for (int i = 0; i < index; i++) { | ||
| current = current->next; | ||
| } | ||
| return current->data; | ||
| } | ||
|
|
||
| // удаление элемента по заданному индексу | ||
| void listRemove(List* list, int index) | ||
| { | ||
| // проверка корректности индекса | ||
| if (index < 0 || index >= list->size) { | ||
| printf("Некорректный индекс для удаления\n"); | ||
| return; | ||
| } | ||
|
|
||
| Node* toDelete; | ||
| // удаление из начала списка | ||
| if (index == 0) { | ||
| toDelete = list->head; | ||
| list->head = list->head->next; | ||
| } else { | ||
| // поиск узла перед удаляемым | ||
| Node* current = list->head; | ||
| for (int i = 0; i < index - 1; i++) { | ||
| current = current->next; | ||
| } | ||
| toDelete = current->next; | ||
| current->next = toDelete->next; | ||
| } | ||
| // освобождение памяти | ||
| free(toDelete); | ||
| list->size--; | ||
| } | ||
|
|
||
| // распечатывание содержимое списка | ||
| void listPrint(List* list) | ||
| { | ||
| if (list->size == 0) { | ||
| printf("Список пуст\n"); | ||
| return; | ||
| } | ||
|
|
||
| Node* current = list->head; | ||
| printf("Содержимое списка: "); | ||
| while (current != NULL) { | ||
| printf("%d ", current->data); | ||
| current = current->next; | ||
| } | ||
| printf("\n"); | ||
| } | ||
|
|
||
| // удаление всего списка (освобождает память) | ||
| void listDelete(List* list) | ||
| { | ||
| Node* current = list->head; | ||
| // последовательное удаление всех узлов | ||
| while (current != NULL) { | ||
| Node* temp = current; | ||
| current = current->next; | ||
| free(temp); | ||
| } | ||
| // освобождение структуры списка | ||
| free(list); | ||
| } | ||
|
|
||
| // находит позицию для вставки в сортированный список | ||
| int listPosition(List* list, int value) | ||
| { | ||
| if (list->size == 0) | ||
| return 0; | ||
|
|
||
| Node* current = list->head; | ||
| int position = 0; | ||
|
|
||
| // ищем первую позицию, где следующий элемент больше вставляемого значения | ||
| while (current != NULL && current->data < value) { | ||
| current = current->next; | ||
| position++; | ||
| } | ||
| return position; | ||
| } | ||
|
|
||
| // проверяет наличие значения в списке | ||
| int listContain(List* list, int value) | ||
| { | ||
| Node* current = list->head; | ||
| int position = 0; | ||
|
|
||
| while (current != NULL) { | ||
| if (current->data == value) { | ||
| return position; | ||
| } | ||
| current = current->next; | ||
| position++; | ||
| } | ||
| return -1; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #ifndef SORTLIST_H | ||
| #define SORTLIST_H | ||
|
|
||
| // структура узла списка | ||
| typedef struct Node { | ||
| int data; // данные узла | ||
| struct Node* next; // указатель на следующий узел | ||
| } Node; | ||
|
|
||
| // структура списка | ||
| typedef struct { | ||
| Node* head; // указатель на начало списка | ||
| int size; // размер списка | ||
| } List; | ||
|
|
||
| // создание нового пустого списка | ||
| List* listCreate(); | ||
|
|
||
| // вставка элемента в список по заданному индексу | ||
| void listInsert(List* list, int index, int value); | ||
|
|
||
| // получение элемента по заданному индексу | ||
| int listGet(List* list, int index); | ||
|
|
||
| // удаление элемента по заданному индексу | ||
| void listRemove(List* list, int index); | ||
|
|
||
| // распечатывание содержимое списка | ||
| void listPrint(List* list); | ||
|
|
||
| // удаление всего списка (освобождает память) | ||
| void listDelete(List* list); | ||
|
|
||
| // находит позицию для вставки в сортированный список | ||
| int listPosition(List* list, int value); | ||
|
|
||
| // проверяет наличие значения в списке | ||
| int listContain(List* list, int value); | ||
|
|
||
| // основная функция программы для работы со списком в диалоговом режиме | ||
| int main(int argc, char* argv[]); | ||
|
|
||
| #endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Здесь уже нужно использовать CMake.