Загрузка данных


#ifndef LIST_H
#define LIST_H

#include "door_struct.h"

struct node {
    struct door data;
    struct node *next;
};

struct node *init(struct door door);
struct node *add_door(struct node *elem, struct door door);
struct node *find_door(int door_id, struct node *root);
struct node *remove_door(struct node *elem, struct node *root);
void destroy(struct node *root);

#endif


#include "list.h"

#include <stdlib.h>

struct node *init(struct door door) {
    struct node *root = malloc(sizeof(struct node));

    if (root != NULL) {
        root->data = door;
        root->next = NULL;
    }

    return root;
}

struct node *add_door(struct node *elem, struct door door) {
    struct node *new_node = NULL;

    if (elem != NULL) {
        new_node = malloc(sizeof(struct node));
        if (new_node != NULL) {
            new_node->data = door;
            new_node->next = elem->next;
            elem->next = new_node;
        }
    }

    return new_node;
}

struct node *find_door(int door_id, struct node *root) {
    struct node *result = NULL;

    while (root != NULL && result == NULL) {
        if (root->data.id == door_id) {
            result = root;
        }
        root = root->next;
    }

    return result;
}

struct node *remove_door(struct node *elem, struct node *root) {
    if (elem != NULL && root != NULL) {
        if (elem == root) {
            root = root->next;
            free(elem);
        } else {
            struct node *current = root;

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

            if (current->next == elem) {
                current->next = elem->next;
                free(elem);
            }
        }
    }

    return root;
}

void destroy(struct node *root) {
    while (root != NULL) {
        struct node *next = root->next;
        free(root);
        root = next;
    }
}



#include <stdio.h>

#include "list.h"

#define SUCCESS 1
#define FAIL 0

int add_door_test(void);
int remove_door_middle_test(void);
int remove_door_root_test(void);
void print_test_result(const char *test_name, int result);

int main(void) {
    print_test_result("add_door_test", add_door_test());
    print_test_result("remove_door_middle_test", remove_door_middle_test());
    print_test_result("remove_door_root_test", remove_door_root_test());

    return 0;
}

int add_door_test(void) {
    int result = FAIL;
    struct door door_1 = {1, 0};
    struct door door_2 = {2, 1};

    struct node *root = init(door_1);
    struct node *added = add_door(root, door_2);

    if (root != NULL && added != NULL && root->next == added && added->data.id == 2 &&
        added->data.status == 1 && added->next == NULL) {
        result = SUCCESS;
    }

    destroy(root);

    return result;
}

int remove_door_middle_test(void) {
    int result = FAIL;
    struct door door_1 = {1, 0};
    struct door door_2 = {2, 1};
    struct door door_3 = {3, 0};

    struct node *root = init(door_1);
    struct node *second = add_door(root, door_2);
    struct node *third = add_door(second, door_3);

    root = remove_door(second, root);

    if (root != NULL && root->next == third && find_door(2, root) == NULL) {
        result = SUCCESS;
    }

    destroy(root);

    return result;
}

int remove_door_root_test(void) {
    int result = FAIL;
    struct door door_1 = {1, 0};
    struct door door_2 = {2, 1};

    struct node *root = init(door_1);
    struct node *second = add_door(root, door_2);

    root = remove_door(root, root);

    if (root == second && root != NULL && root->data.id == 2) {
        result = SUCCESS;
    }

    destroy(root);

    return result;
}

void print_test_result(const char *test_name, int result) {
    printf("%s: ", test_name);
    if (result == SUCCESS) {
        printf("SUCCESS");
    } else {
        printf("FAIL");
    }
    printf("\n");
}



CC = gcc
CFLAGS = -Wall -Wextra -Werror -std=c11
BUILD_DIR = ../build

.PHONY: all clean rebuild door_struct list_test

all: door_struct list_test

$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)

door_struct: $(BUILD_DIR)
	$(CC) $(CFLAGS) dmanager_module.c -o $(BUILD_DIR)/Quest_1

list_test: $(BUILD_DIR)
	$(CC) $(CFLAGS) list.c list_test.c -o $(BUILD_DIR)/Quest_2

clean:
	rm -rf $(BUILD_DIR)

rebuild: clean all