diff --git a/HW5/sortingList/list/list.c b/HW5/sortingList/list/list.c new file mode 100644 index 0000000..ea93d22 --- /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 removeFromList(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\n"); + return; + } + + SortedList *current = head; + printf("List:\n"); + while (current != NULL) { + printf("%d\n", current->data); + current = current->next; + } +} + +void freeList(SortedList *head) { + SortedList *current = head; + while (current != NULL) { + SortedList *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..4ab5674 --- /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 removeFromList(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); + diff --git a/HW5/sortingList/src/main.c b/HW5/sortingList/src/main.c new file mode 100644 index 0000000..b78c3ed --- /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\n"); + 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"); +} +