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


#ifndef STACK_H
#define STACK_H

struct stack {
    int data;
    struct stack *next;
};

struct stack *init(int value);
struct stack *push(struct stack *top, int value);
struct stack *pop(struct stack *top, int *value);
void destroy(struct stack *top);

#endif


#include "stack.h"

#include <stdlib.h>

struct stack *init(int value) {
    struct stack *top = malloc(sizeof(struct stack));

    if (top != NULL) {
        top->data = value;
        top->next = NULL;
    }

    return top;
}

struct stack *push(struct stack *top, int value) {
    struct stack *new_top = malloc(sizeof(struct stack));

    if (new_top != NULL) {
        new_top->data = value;
        new_top->next = top;
    } else {
        new_top = top;
    }

    return new_top;
}

struct stack *pop(struct stack *top, int *value) {
    struct stack *next = NULL;

    if (top != NULL) {
        if (value != NULL) {
            *value = top->data;
        }
        next = top->next;
        free(top);
    }

    return next;
}

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


#include <stdio.h>

#include "stack.h"

#define SUCCESS 1
#define FAIL 0

int push_test(void);
int pop_test(void);
int pop_empty_test(void);
void print_test_result(const char *test_name, int result);

int main(void) {
    print_test_result("push_test", push_test());
    print_test_result("pop_test", pop_test());
    print_test_result("pop_empty_test", pop_empty_test());

    return 0;
}

int push_test(void) {
    int result = FAIL;

    struct stack *top = init(1);
    top = push(top, 2);

    if (top != NULL && top->data == 2 && top->next != NULL && top->next->data == 1) {
        result = SUCCESS;
    }

    destroy(top);

    return result;
}

int pop_test(void) {
    int result = FAIL;
    int value = 0;

    struct stack *top = init(1);
    top = push(top, 2);
    top = push(top, 3);

    top = pop(top, &value);

    if (value == 3 && top != NULL && top->data == 2) {
        result = SUCCESS;
    }

    destroy(top);

    return result;
}

int pop_empty_test(void) {
    int result = FAIL;
    int value = 10;

    struct stack *top = NULL;
    top = pop(top, &value);

    if (top == NULL && value == 10) {
        result = SUCCESS;
    }

    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 stack_test

all: door_struct list_test stack_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

stack_test: $(BUILD_DIR)
	$(CC) $(CFLAGS) stack.c stack_test.c -o $(BUILD_DIR)/Quest_3

clean:
	rm -rf $(BUILD_DIR)

rebuild: clean all