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
53 changes: 53 additions & 0 deletions 6/main_sorted_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "test/test_sorted_list.h"

#include <string.h>

int main(int argc, char* argv[])
{
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--test") == 0) {
int res = testRunAll();
return res;
}
}
List list;
initList(&list);

printf("0 – выйти\n1 – добавить значение в сортированный список\n2 – удалить значение из списка\n3 – распечатать список\n\n");

char inp;
printf("Введите команду: ");
scanf("%c", &inp);

while (inp != '0') {
int value;
switch (inp) {
case '1':
printf("Добавить: ");
scanf("%d", &value);

addValue(&list, value);

break;

case '2':
printf("Удалить: ");
scanf("%d", &value);

removeValue(&list, value);

break;

case '3':
printf("Список: ");
printList(&list);
break;
default:
printf("Введите команду: ");
break;
}
scanf("%c", &inp);
}

return 0;
}
106 changes: 106 additions & 0 deletions 6/sorted_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include "sorted_list.h"

void initList(List* list)
{
(*list).head = NULL;
(*list).size = 0;
}

Node* createNode(int value, Node* next)
{
Node* node = malloc(sizeof(Node));
if (node == NULL) {
printf("Ошибка выделения памяти\n");
exit(-1);
}

(*node).next = next;
(*node).value = value;

return node;
}

void addValue(List* list, int value)
{
Node* head = (*list).head;

if (head == NULL) {
Node* node = createNode(value, NULL);

(*list).head = node;
(*list).size++;
return;
}

Node* buff = NULL;

while (head->value < value) {
if ((*head).next == NULL) {
Node* node = createNode(value, NULL);

(*head).next = node;

(*list).size++;
return;
}

buff = head;
head = (*head).next;
}

Node* node = createNode(value, head);

(*list).size++;

if (buff != NULL) {
(*buff).next = node;
}

else {
(*list).head = node;
}
}

void removeValue(List* list, int value)
{
Node* iter = (*list).head;

if (iter == NULL) {
return;
}

Node* buff = NULL;

while (iter != NULL) {
int listValue = (*iter).value;
if (listValue == value) {
if (buff != NULL) {
(*buff).next = (*iter).next;
}

else {
(*list).head = (*iter).next;
}

free(iter);
(*list).size--;

iter = (*list).head;
continue;
}

buff = iter;
iter = (*iter).next;
}
}

void printList(List* list)
{
Node* iter = (*list).head;
while (iter != NULL) {
int value = (*iter).value;
printf("%d ", value);
iter = (*iter).next;
}
printf("\n");
}
22 changes: 22 additions & 0 deletions 6/sorted_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>

struct Node {
int value;
struct Node* next;

} typedef Node;

struct List {
Node* head;
int size;
} typedef List;

void initList(List* list);
Node* createNode(int value, Node* next);

void addValue(List* list, int value);
void removeValue(List* list, int value);

void printList(List* list);
162 changes: 162 additions & 0 deletions 6/test/test_sorted_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include "test_sorted_list.h"

int compareIntAscending(const void* a, const void* b)
{
int x = *((const int*)a);
int y = *((const int*)b);

return x - y;
}

void testInitList()
{
List list;
initList(&list);

int res = (list.head == NULL) && (list.size == 0);

assert(res);
}

void testCreateNode()
{
int value = 5;
Node* node = createNode(value, NULL);

int res = (node->next == NULL) && (node->value == value);

assert(res);
}

void testSize()
{
int arr[] = { 0, 25, 25, -25, 2, 5, 6, 8 };
int sizeArr = sizeof(arr) / sizeof(int);

List list;
initList(&list);

for (int i = 0; i < sizeArr; i++) {
addValue(&list, arr[i]);
}

int sizeList = list.size;

assert(sizeList == sizeArr);
}

void testAddValueInSortedOrder()
{
int arr[] = { 0, 25, 25, -25, 2, 5, 6, 8 };
int sizeArr = sizeof(arr) / sizeof(int);

List list;
initList(&list);

for (int i = 0; i < sizeArr; i++) {
addValue(&list, arr[i]);
}

qsort(arr, sizeArr, sizeof(int), compareIntAscending);

Node* iter = list.head;
for (int i = 0; i < sizeArr; i++) {
assert(iter != NULL);
assert(iter->value == arr[i]);

iter = iter->next;
}
}

void testRemoveNoValueInList()
{
int arr[] = { 0, 25, 25, -25, 2, 5, 6, 8 };
int sizeArr = sizeof(arr) / sizeof(int);

List list;
initList(&list);

for (int i = 0; i < sizeArr; i++) {
addValue(&list, arr[i]);
}

int oldSize = list.size;

removeValue(&list, -2342);

int newSize = list.size;

assert(oldSize == newSize);
}

void testRemoveValueOneTimeInList()
{
int arr[] = { 0, 25, 25, -25, 2, 5, 6, 8 };
int sizeArr = sizeof(arr) / sizeof(int);

List list;
initList(&list);

for (int i = 0; i < sizeArr; i++) {
addValue(&list, arr[i]);
}

int oldSize = list.size;

removeValue(&list, 0);

int newSize = list.size;

assert(oldSize - 1 == newSize);
}

void testRemoveMoreOneTimeInList()
{
int arr[] = { 0, 25, 25, 25, -25, 2, 5, 6, 8 };
int sizeArr = sizeof(arr) / sizeof(int);

List list;
initList(&list);

for (int i = 0; i < sizeArr; i++) {
addValue(&list, arr[i]);
}

int oldSize = list.size;

removeValue(&list, 25);

int newSize = list.size;

assert(oldSize - 3 == newSize);
}

int testRunAll()
{
printf("---------- TESTS ----------\n");

testInitList();
printf("Test 1 PASSED\n");

testCreateNode();
printf("Test 2 PASSED\n");

testSize();
printf("Test 3 PASSED\n");

testAddValueInSortedOrder();
printf("Test 4 PASSED\n");

testRemoveNoValueInList();
printf("Test 5 PASSED\n");

testRemoveValueOneTimeInList();
printf("Test 6 PASSED\n");

testRemoveMoreOneTimeInList();
printf("Test 7 PASSED\n");

printf("\n\nALL TESTS PASSED\n");

return EXIT_SUCCESS;
}
16 changes: 16 additions & 0 deletions 6/test/test_sorted_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include "../sorted_list.h"

#include <assert.h>

int compareIntAscending(const void* a, const void* b);

void testInitList();
void testCreateNode();
void testSize();
void testAddValueInSortedOrder();
void testRemoveNoValueInList();
void testRemoveValueOneTimeInList();
void testRemoveMoreOneTimeInList();

int testRunAll();