Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
73 changes: 73 additions & 0 deletions HW5/sortingList/list/list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "list.h"
#include <stdio.h>
#include <stdlib.h>

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;
}
}

35 changes: 35 additions & 0 deletions HW5/sortingList/list/list.h
Original file line number Diff line number Diff line change
@@ -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);

79 changes: 79 additions & 0 deletions HW5/sortingList/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "../list/list.h"
#include <stdio.h>

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");
}