From 994d968ecd2f651186afa8de3cc1d0c9286422c9 Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 26 Oct 2025 20:11:09 +0300 Subject: [PATCH 1/6] 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/6] 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/6] 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/6] 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; From d303dbe7f060754d75d69b1ea6a45d3ec6a8adb2 Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 26 Oct 2025 20:51:33 +0300 Subject: [PATCH 5/6] Add --test flag --- HW5/sortingList/src/main.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/HW5/sortingList/src/main.c b/HW5/sortingList/src/main.c index b78c3ed..376db12 100644 --- a/HW5/sortingList/src/main.c +++ b/HW5/sortingList/src/main.c @@ -1,9 +1,16 @@ #include "../list/list.h" #include +#include void printMenu(); -int main() { +int main(int argc, char *argv[]) { + if (argc > 1 && strcmp(argv[1], "--test") == 0) { + extern void runAllTests(); + runAllTests(); + return 0; + } + SortedList *head = NULL; int errorCode = 0; int dialogCode = -1; @@ -14,7 +21,8 @@ int main() { if (scanf("%d", &dialogCode) != 1) { printf("Wrong input\n"); - while (getchar() != '\n'); + while (getchar() != '\n') + ; continue; } @@ -27,7 +35,8 @@ int main() { if (scanf("%d", &value) != 1) { printf("Error: not an integer!\n"); - while (getchar() != '\n'); + while (getchar() != '\n') + ; break; } @@ -43,7 +52,8 @@ int main() { if (scanf("%d", &value) != 1) { printf("Error: not an integer!\n"); - while (getchar() != '\n'); + while (getchar() != '\n') + ; break; } From 22822dec3493c9056f92b8b155a631a6c3c557b1 Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 16 Nov 2025 20:52:48 +0300 Subject: [PATCH 6/6] Add tests for dialog list --- HW5/sortingList/src/test.c | 247 +++++++++++++++++++++++++++++++++++++ 1 file changed, 247 insertions(+) create mode 100644 HW5/sortingList/src/test.c diff --git a/HW5/sortingList/src/test.c b/HW5/sortingList/src/test.c new file mode 100644 index 0000000..5828a0b --- /dev/null +++ b/HW5/sortingList/src/test.c @@ -0,0 +1,247 @@ +#include "../list/list.h" +#include +#include +#include + +int checkListContent(SortedList *head, int expected[], int size) { + SortedList *current = head; + int count = 0; + + for (int i = 0; i < size; ++i) { + if (current == NULL) + return 0; + if (current->data != expected[i]) + return 0; + current = current->next; + count++; + } + + return (current == NULL) && (count == size); +} + +void testNullSafety() { + printf("Testing NULL ptr safety\n"); + + SortedList *nullList = NULL; + + printList(nullList); + freeList(nullList); + + printf("Test passed.\n"); +} + +void testEmptyList() { + printf("Testing empty list\n"); + + SortedList *head = NULL; + + assert(removeFromList(91, &head) == -1); + assert(head == NULL); + + printf("Test passed.\n"); +} + +void testAddToEmptyList() { + printf("Testing add to empty list\n"); + + SortedList *head = NULL; + + assert(addToList(20, &head) == 0); + assert(head != NULL); + assert(head->data == 20); + assert(head->next == NULL); + + freeList(head); + + printf("Test passed.\n"); +} + +void testAddMultipleElements() { + printf("Testing add multiple elements\n"); + + SortedList *head = NULL; + + assert(addToList(65, &head) == 0); + assert(addToList(29, &head) == 0); + assert(addToList(15, &head) == 0); + assert(addToList(0, &head) == 0); + assert(addToList(10, &head) == 0); + assert(addToList(-2, &head) == 0); + + int expected[] = {-2, 0, 10, 15, 29, 65}; + assert(checkListContent(head, expected, 6)); + + freeList(head); + + printf("Test passed.\n"); +} + +void testAddDuplicates() { + printf("Testing add duplicates\n"); + + SortedList *head = NULL; + + assert(addToList(20, &head) == 0); + assert(addToList(20, &head) == 0); + assert(addToList(20, &head) == 0); + + SortedList *current = head; + int count = 0; + while (current != NULL) { + assert(current->data == 20); + current = current->next; + count++; + } + assert(count == 3); + + freeList(head); + + printf("Test passed.\n"); +} + +void testRemoveFromList() { + printf("Testing remove from list\n"); + + SortedList *head = NULL; + + assert(addToList(20, &head) == 0); + assert(addToList(15, &head) == 0); + assert(addToList(10, &head) == 0); + assert(addToList(-5, &head) == 0); + assert(addToList(-100, &head) == 0); + + assert(removeFromList(10, &head) == 0); + int expected1[] = {-100, -5, 15, 20}; + assert(checkListContent(head, expected1, 4)); + + assert(removeFromList(-100, &head) == 0); + int expected2[] = {-5, 15, 20}; + assert(checkListContent(head, expected2, 3)); + + assert(removeFromList(20, &head) == 0); + int expected3[] = {-5, 15}; + assert(checkListContent(head, expected3, 2)); + + assert(removeFromList(15, &head) == 0); + int expected4[] = {-5}; + assert(checkListContent(head, expected4, 1)); + + assert(removeFromList(-5, &head) == 0); + assert(head == NULL); + + printf("Test passed.\n"); +} + +void testRemoveNonexistent() { + printf("Testing remove nonexistent\n"); + + SortedList *head = NULL; + + assert(addToList(10, &head) == 0); + assert(addToList(20, &head) == 0); + + assert(removeFromList(15, &head) == -1); + + int expected[] = {10, 20}; + assert(checkListContent(head, expected, 2)); + + freeList(head); + + printf("Test passed.\n"); +} + +void testMemoryManagement() { + printf("Testing memory management\n"); + + SortedList *head = NULL; + + for (int i = 0; i < 100; ++i) + assert(addToList(i, &head) == 0); + + for (int i = 0; i < 100; i += 2) + assert(removeFromList(i, &head) == 0); + + SortedList *current = head; + for (int i = 1; i < 100; i += 2) { + assert(current != NULL); + assert(current->data == i); + current = current->next; + } + assert(current == NULL); + + freeList(head); + + head = NULL; + assert(addToList(20, &head) == 0); + freeList(head); + + printf("Test passed.\n"); +} + +void testBoundaryValues() { + printf("Testing boundary values\n"); + + SortedList *head = NULL; + + assert(addToList(0, &head) == 0); + assert(addToList(-1000, &head) == 0); + assert(addToList(1000, &head) == 0); + + assert(addToList(INT_MAX, &head) == 0); + assert(addToList(INT_MIN, &head) == 0); + + assert(head != NULL); + assert(head->data == INT_MIN); + + SortedList *current = head; + while (current->next != NULL) + current = current->next; + + assert(current->data == INT_MAX); + + freeList(head); + + printf("Test passed.\n"); +} + +void testDeterministicBehavior() { + printf("Testing deterministic behavior\n"); + + SortedList *head = NULL; + + int addSequence[] = {6, -10, 32, 28, -90, 3, 0}; + int removeSequence[] = {3, 0, 6, 32, -90, -10, 28}; + + for (int i = 0; i < 7; ++i) + assert(addToList(addSequence[i], &head) == 0); + + int expectedAfterAdd[] = {-90, -10, 0, 3, 6, 28, 32}; + assert(checkListContent(head, expectedAfterAdd, 7)); + + for(int i = 0; i < 7; ++i) + assert(removeFromList(removeSequence[i], &head) == 0); + + assert(head == NULL); + + freeList(head); + + printf("Test passed.\n"); +} + +void runAllTests() { + printf("Running all tests\n"); + + testNullSafety(); + testEmptyList(); + testAddToEmptyList(); + testAddMultipleElements(); + testAddDuplicates(); + testRemoveFromList(); + testRemoveNonexistent(); + testMemoryManagement(); + testBoundaryValues(); + testDeterministicBehavior(); + + printf("All tests passed.\n"); +} +