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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ dkms.conf

# debug information files
*.dwo

*build/
8 changes: 8 additions & 0 deletions src/hello.c
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вообще этого здесь быть не должно

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>
int main()
{
printf("Hello, world!\n");


return 0;
}
92 changes: 92 additions & 0 deletions src/list/TestList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "list.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

bool testInsertIntoEmpty(void)
{
Node* head = NULL;
insertSorted(&head, 10);
bool result = (head != NULL && head->value == 10 && head->next == NULL);
freeList(head);
return result;
}

bool testInsertCorrectOrder(void)
{
Node* head = NULL;
insertSorted(&head, 5);
insertSorted(&head, 3);
insertSorted(&head, 7);
insertSorted(&head, 5);

bool result = head != NULL && head->value == 3 && head->next != NULL && head->next->value == 5 && head->next->next != NULL && head->next->next->value == 5 && head->next->next->next != NULL && head->next->next->next->value == 7 && head->next->next->next->next == NULL;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ой нет. Напишите функцию, которая сравнивает список с массивом. Так будет сиииильно лучше.


freeList(head);
return result;
}

bool testDeleteExisting(void)
{
Node* head = NULL;
insertSorted(&head, 1);
insertSorted(&head, 3);
insertSorted(&head, 5);

bool result = deleteValue(&head, 3) == 1 && head != NULL && head->value == 1 && head->next != NULL && head->next->value == 5 && head->next->next == NULL;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Аналогично

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

И далее тоже


freeList(head);
return result;
}

bool testDeleteNonExisting(void)
{
Node* head = NULL;
insertSorted(&head, 4);
insertSorted(&head, 6);

bool result = deleteValue(&head, 100) == 0;
freeList(head);
return result;
}

bool testDeleteFromEmpty(void)
{
Node* head = NULL;
return deleteValue(&head, 5) == 0;
}

bool testDeleteFirstLast(void)
{
Node* head = NULL;
insertSorted(&head, 2);
insertSorted(&head, 4);
insertSorted(&head, 6);

bool result = deleteValue(&head, 2) == 1 && deleteValue(&head, 6) == 1 && head != NULL && head->value == 4 && head->next == NULL;

freeList(head);
return result;
}

int main(int argc, char* argv[])
{
if (argc >1 && strcmp(argv[1], "--test")==0){
if (!testInsertIntoEmpty() ||
!testInsertCorrectOrder() ||
!testDeleteExisting() ||
!testDeleteNonExisting() ||
!testDeleteFromEmpty() ||
!testDeleteFirstLast()) {
printf("Tests failed\n");
return 1;
}

printf("All tests passed\n");
return 0;

}

return 0;
}
66 changes: 66 additions & 0 deletions src/list/list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <stdio.h>
#include <stdlib.h>
#include "list.h"


void insertSorted(Node** head, int value) {
Node* newNode = malloc(sizeof(Node));
newNode->value = value;
newNode->next = NULL;

if (*head == NULL || value < (*head)->value) {
newNode->next = *head;
*head = newNode;
return;
}


Node* current = *head;
while (current->next != NULL && current->next->value < value) {
current = current->next;
}

newNode->next = current->next;
current->next = newNode;
}

int deleteValue(Node** head, int value) {
Node* current = *head;
Node* prev = NULL;

while (current != NULL && current->value != value) {
prev = current;
current = current->next;
}

if (current == NULL) return 0;

if (prev == NULL) *head = current->next;
else prev->next = current->next;

free(current);
return 1;
}

void printList(Node* head) {
if (head == NULL) {
printf("Список пуст.\n");
return;
}

printf("Список: ");
while (head != NULL) {
printf("%d ", head->value);
head = head->next;
}
printf("\n");
}


void freeList(Node* head) {
while (head != NULL) {
Node* temp = head;
head = head->next;
free(temp);
}
}
13 changes: 13 additions & 0 deletions src/list/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

typedef struct Node {
int value;
struct Node* next;
} Node;

void insertSorted(Node** head, int value);
int deleteValue(Node** head, int value);
void printList(Node* head);
void freeList(Node* head);