Commit 309ee93a authored by jonas.stirnema's avatar jonas.stirnema
Browse files

Modified growable stack

parent 279abae6
CC := gcc
CFLAGS := -Wall -Wextra -g -fsanitize=address -fsanitize=leak
BIN := bin
SRC := src
INCLUDE := include
LIBRARIES := -lm
EXECUTABLE1 := main
# ADD ALL THE SOURCES FOR EXECUTABLE1
SOURCES1 += $(SRC)/$(EXECUTABLE1).c
.PHONY: all run clean # Specify these are not files but internal terms
all: $(BIN)/$(EXECUTABLE1)
run: clean all
clear
@echo "RUNNING..."
./$(BIN)/$(EXECUTABLE1)
# # $^ -> dep
# # $@ -> target
# # $< -> premiere dep
# # Compiles directly the c files
# # Not optimal but succint and simple
# COMPILE FIRST EXECUTABLE
$(BIN)/$(EXECUTABLE1): $(SOURCES1)
@echo "COMPILING..."
$(CC) $(CFLAGS) -I $(INCLUDE) $^ -o $@ $(LIBRARIES)
clean:
@echo "Cleaning..."
-rm $(BIN)/*
/*
* HEADER QUEEN
* Author : Jonas S.
* Date : 17/11/2021
! QUEEN BACKTRACKING
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#ifndef _QUEEN_H_
#define _QUEEN_H_
// DEFINES
// PROTOTYPE FUNCTIONS
#endif
\ No newline at end of file
/*
* CODE MAIN
* Author : Jonas S.
* Date : 17/11/2021
! DESCRIPTION
*/
#include <stdio.h>
int main()
{
printf("ISSOU\n");
return 0;
}
\ No newline at end of file
/*
* CODE QUEEN
* Author : Jonas S.
* Date : 17/11/2021
! DESCRIPTION
*/
#include "../include/queen.h"
void nbr_solutions(int n, bool board_in[][], int col, int* ptr_cnt)
{
for (int li = 0; li < n; i++)
{
if (board_in[li][col])
{
if (col < n - 1)
{
bool board[n][n];
copy(n, board, board);
prises_devant(n, baord_in[][], board[][], )
}
}
}
}
......@@ -14,22 +14,30 @@
#define _STACK_H_
// DEFINES
#define INCREMENT_STACK 100
#define INCREMENT_STACK 5
// TERMINAL COLOR DEFINES
#define TERM_GREEN "\x1B[32m"
#define TERM_RED "\x1B[31m"
#define TERM_WHITE "\x1B[37m"
typedef struct
{
uint64_t max;
{
int64_t max;
int64_t top;
int32_t *data;
}stack_t;
// PROTOTYPE FUNCTIONS
int32_t* create_stack(uint64_t capa);
stack_t* create_stack(uint64_t capa);
void stack_push(stack_t* st, int32_t val);
int32_t stack_pop(stack_t* st);
int32_t stack_peek(stack_t st);
void stack_pop(stack_t* st, int32_t* val);
void stack_peek(stack_t st, int32_t* val);
bool stack_is_full(stack_t st);
bool stack_is_empty(stack_t st);
void stack_destroy(stack_t* st);
void stack_show(stack_t st);
#endif
\ No newline at end of file
......@@ -7,47 +7,129 @@
#include "../include/stack.h"
int32_t* create_stack(uint64_t capa)
/**
* @brief Return pointer to stack with given size
*
* @param capa
* @return stack_t*
*/
stack_t* create_stack(uint64_t capa)
{
stack_t* st = malloc(sizeof(stack_t));
st->max = capa;
st->data = malloc(st->max * sizeof(int32_t));
st->top = -1;
return st;
}
void stack_push(stack_t* st, int32_t val)
/**
* @brief Free a stack
*
* @param st
*/
void stack_destroy(stack_t* st)
{
// UP CAPACITY WHEN MAX IS REACHED
if(st->top == st->max)
{
realloc(st->data, st->max + INCREMENT_STACK);
}
st->data[++st->top] = val;
free(st->data);
free(st);
}
int32_t stack_pop(stack_t* st)
/**
* @brief Check is a stack is empty
*
* @param st
* @return true
* @return false
*/
bool stack_is_empty(stack_t st)
{
return st->data[st->top--];
return st.top == -1;
}
int32_t stack_peek(stack_t st)
/**
* @brief Check if a stack is full
*
* @param st
* @return true
* @return false
*/
bool stack_is_full(stack_t st)
{
return st.data[st.top];
return st.top == st.max - 1;
}
bool stack_is_full(stack_t st)
/**
* @brief Push an given element to a stack
*
* @param st
* @param val
*/
void stack_push(stack_t* st, int32_t val)
{
return st.top == st.max;
// UP CAPACITY WHEN MAX IS REACHED
if(stack_is_full(*st))
{
st->max += INCREMENT_STACK;
st->data = realloc(st->data, st->max * sizeof(int32_t));
fprintf(stderr, "%sPUSH: MAX CAPAXITY INCREMENTED%s\n", TERM_RED, TERM_WHITE);
}
st->data[++st->top] = val;
}
bool stack_is_empty(stack_t st)
/**
* @brief Pop the last element on the stack
*
* @param st
* @param val
*/
void stack_pop(stack_t* st, int32_t* val)
{
return st.top == -1;
if( stack_is_empty(*st) )
{
fprintf(stderr, "%sCANNOT POP: STACK IS EMPTY%s\n", TERM_RED, TERM_WHITE);
return;
}
*val = st->data[st->top--];
}
void stack_destroy(stack_t* st)
/**
* @brief Get the last element on the stack
*
* @param st
* @param val
*/
void stack_peek(stack_t st, int32_t* val)
{
free(st->data);
free(st);
if( stack_is_empty(st) )
{
fprintf(stderr, "%sCANNOT PEEK: STACK IS EMPTY%s\n", TERM_RED, TERM_WHITE);
return;
}
*val = st.data[st.top];
}
/**
* @brief Show a stack
*
* @param st
*/
void stack_show(stack_t st)
{
if( stack_is_empty(st) )
{
fprintf(stderr, "%sCANNOT SHOW: STACK IS EMPTY%s\n", TERM_RED, TERM_WHITE);
return;
}
printf("%s", TERM_GREEN);
printf("------------------------------\n");
printf("----------- STACK -----------\n");
printf("------------------------------\n");
printf("MAX SIZE : %ld\n", st.max);
printf("TOP INDEX : %ld\n", st.top);
for (int32_t i = 0; i < st.top + 1; i++)
{
printf("%d - %d\n", i, st.data[i]);
}
printf("%s\n", TERM_WHITE);
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment