From 994d968ecd2f651186afa8de3cc1d0c9286422c9 Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 26 Oct 2025 20:11:09 +0300 Subject: [PATCH 1/4] Add sorted list struct --- HW5/sortingList/list/list.c | 73 +++++++++++++++++++++++++++++++++++++ HW5/sortingList/list/list.h | 35 ++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 HW5/sortingList/list/list.c create mode 100644 HW5/sortingList/list/list.h diff --git a/HW5/sortingList/list/list.c b/HW5/sortingList/list/list.c new file mode 100644 index 0000000..8cf45a7 --- /dev/null +++ b/HW5/sortingList/list/list.c @@ -0,0 +1,73 @@ +#include "list.h" +#include +#include + +int addToList(int data, SortedList **head) { + SortedList *newNode = (SortedList *)malloc(sizeof(SortedList)); + if (newNode == NULL) + return -1; + + newNode->data = data; + newNode->next = NULL; + + if (*head == NULL || data <= (*head)->data) { + newNode->next = *head; + *head = newNode; + return 0; + } + + SortedList *current = *head; + while (current->next != NULL && current->next->data < data) + current = current->next; + newNode->next = current->next; + current->next = newNode; + return 0; +} + +int removeFormList(int data, SortedList **head) { + if (*head == NULL) + return -1; + + SortedList *current = *head; + SortedList *prev = NULL; + + while (current != NULL && current->data != data) { + prev = current; + current = current->next; + } + + if (current == NULL) + return -1; + + if (prev == NULL) + *head = current->next; + else + prev->next = current->next; + + free(current); + return 0; +} + +void printList(SortedList *head) { + if (head == NULL) { + printf("List is empty"); + return; + } + + SortedList *current = head; + printf("List:\n"); + while (current != NULL) { + printf("%d\n", current->data); + current = current->next; + } +} + +void freeList(SortedList *head) { + List *current = head; + while (current != NULL) { + List *next = current->next; + free(current); + current = next; + } +} + diff --git a/HW5/sortingList/list/list.h b/HW5/sortingList/list/list.h new file mode 100644 index 0000000..5313804 --- /dev/null +++ b/HW5/sortingList/list/list.h @@ -0,0 +1,35 @@ +#pragma once + +typedef struct SortedList { + int data; + struct SortedList *next; +} SortedList; + +/** + * @brief Add element to list + * @param data Integer to add + * @param head Pointer to pointer of list head + * @return -1 if couldn't allocate memory, 0 otherwise + */ +int addToList(int data, SortedList **head); + +/** + * @brief Remove element from list + * @param data Integer to remove + * @param head Pointer to pointer of list head + * @return -1 if couldn't find element, 0 otherwise + */ +int removeFormList(int data, SortedList **head); + +/** + * @brief Print list data + * @param head Pointer to list head + */ +void printList(SortedList *head); + +/** + * @brief Free list data + * @param head Pointer to list head + */ +void freeList(SortedList *head); + From c65c9b85e71bf7d544f2774d7a59a583c9c2e28f Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 26 Oct 2025 20:49:41 +0300 Subject: [PATCH 2/4] Format list struct --- HW5/sortingList/list/list.c | 8 ++++---- HW5/sortingList/list/list.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/HW5/sortingList/list/list.c b/HW5/sortingList/list/list.c index 8cf45a7..ea93d22 100644 --- a/HW5/sortingList/list/list.c +++ b/HW5/sortingList/list/list.c @@ -24,7 +24,7 @@ int addToList(int data, SortedList **head) { return 0; } -int removeFormList(int data, SortedList **head) { +int removeFromList(int data, SortedList **head) { if (*head == NULL) return -1; @@ -50,7 +50,7 @@ int removeFormList(int data, SortedList **head) { void printList(SortedList *head) { if (head == NULL) { - printf("List is empty"); + printf("List is empty\n"); return; } @@ -63,9 +63,9 @@ void printList(SortedList *head) { } void freeList(SortedList *head) { - List *current = head; + SortedList *current = head; while (current != NULL) { - List *next = current->next; + SortedList *next = current->next; free(current); current = next; } diff --git a/HW5/sortingList/list/list.h b/HW5/sortingList/list/list.h index 5313804..4ab5674 100644 --- a/HW5/sortingList/list/list.h +++ b/HW5/sortingList/list/list.h @@ -19,7 +19,7 @@ int addToList(int data, SortedList **head); * @param head Pointer to pointer of list head * @return -1 if couldn't find element, 0 otherwise */ -int removeFormList(int data, SortedList **head); +int removeFromList(int data, SortedList **head); /** * @brief Print list data From 96db44359ca00acb6a41d4adc37e4d5463d441fb Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 26 Oct 2025 20:51:33 +0300 Subject: [PATCH 3/4] Add interactive dialog system for data operations --- HW5/sortingList/src/main.c | 79 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 HW5/sortingList/src/main.c diff --git a/HW5/sortingList/src/main.c b/HW5/sortingList/src/main.c new file mode 100644 index 0000000..291deed --- /dev/null +++ b/HW5/sortingList/src/main.c @@ -0,0 +1,79 @@ +#include "../list/list.h" +#include + +void printMenu(); + +int main() { + SortedList *head = NULL; + int errorCode = 0; + int dialogCode = -1; + int value = 0; + + do { + printMenu(); + + if (scanf("%d", &dialogCode) != 1) { + printf("Wrong input\n"); + while (getchar() != '\n'); + continue; + } + + switch (dialogCode) { + case 0: + break; + + case 1: + printf("Element to add: "); + + if (scanf("%d", &value) != 1) { + printf("Error: not an integer!\n"); + while (getchar() != '\n'); + break; + } + + errorCode = addToList(value, &head); + if (errorCode == -1) + printf("Error: couldn't allocate memory\n"); + else + printf("Add %d to list\n", value); + break; + + case 2: + printf("Element to remove: "); + + if (scanf("%d", &value) != 1) { + printf("Error: not an integer!\n"); + while (getchar() != '\n'); + break; + } + + errorCode = removeFromList(value, &head); + if (errorCode == -1) + printf("Element was not found in the list"); + else + printf("Remove %d from list\n", value); + break; + + case 3: + printList(head); + break; + + default: + printf("Wrong input!\n"); + break; + } + } while (dialogCode != 0); + + freeList(head); + return 0; +} + +void printMenu() { + printf("Operations:\n"); + printf("0 - exit\n"); + printf("1 - add to list\n"); + printf("2 - remove from list\n"); + printf("3 - print list\n"); + printf("choose operation\n"); +} + From 77b3fb2e52aac0f57ceec9be589c8c35f4ad7c9b Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 26 Oct 2025 21:00:02 +0300 Subject: [PATCH 4/4] Format main.c --- HW5/sortingList/src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HW5/sortingList/src/main.c b/HW5/sortingList/src/main.c index 291deed..b78c3ed 100644 --- a/HW5/sortingList/src/main.c +++ b/HW5/sortingList/src/main.c @@ -49,7 +49,7 @@ int main() { errorCode = removeFromList(value, &head); if (errorCode == -1) - printf("Element was not found in the list"); + printf("Element was not found in the list\n"); else printf("Remove %d from list\n", value); break;