somewhat working stack data struct

This commit is contained in:
hsv2 2022-09-24 19:57:15 +02:00
commit efacd0880b
11 changed files with 300 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.o
*.d
*.a

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "libft"]
path = libft
url = ../libft

6
compile_flags.txt Normal file
View File

@ -0,0 +1,6 @@
-Iinclude
-Ilibft
-std=c17
-Wall
-Wextra
-Wpedantic

43
include/ft_stack.h Normal file
View File

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#ifndef STACK_H
# define STACK_H
# include <stddef.h>
typedef struct s_stack t_stack;
// Allocate a stack of c elements of size s
t_stack *stack_new(size_t c, size_t s);
// Free a stack and return NULL
void *stack_del(t_stack *s);
// Get the top item of the stack or NULL on error
void *stack_peek(t_stack *s);
// Pop from the top of the stack and return popped item or NULL on error
void *stack_pop(t_stack *s);
// Push to the top of the stack and return pushed item or NULL on error
void *stack_push(t_stack *s, void *item);
// Left rotate the stack
t_stack *stack_lrotate(t_stack *s, unsigned int step);
// Right rotate the stack
t_stack *stack_rrotate(t_stack *s, unsigned int step);
// Swap the two topmost items of the stack
t_stack *stack_swap(t_stack *s);
size_t stack_height(t_stack *s);
size_t stack_itemsize(t_stack *s);
size_t stack_maxheight(t_stack *s);
#endif
// vi: noet sw=4 ts=4:

1
libft Submodule

@ -0,0 +1 @@
Subproject commit e60990f2d7796adaf37a1870453312b86a0fec03

32
src/stack_attr.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#include <ft_stack.h>
#include "stack_data.h"
size_t stack_height(t_stack *s)
{
return (s->top / s->itemsize);
}
size_t stack_itemsize(t_stack *s)
{
return (s->itemsize);
}
size_t stack_maxheight(t_stack *s)
{
return (s->maxsize / s->itemsize);
}
// vi: noet sw=4 ts=4:

42
src/stack_common_ops.c Normal file
View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#include <ft_stack.h>
#include <libft.h>
#include "stack_data.h"
void *stack_peek(t_stack *s)
{
if (s->top < 1 || s->top > s->maxsize)
return (NULL);
return (&((char *)(s->items))[s->top - 1]);
}
void *stack_pop(t_stack *s)
{
if (s->top < 1 || s->top > s->maxsize)
return (NULL);
s->top -= s->itemsize;
return (&((char *)(s->items))[s->top]);
}
void *stack_push(t_stack *s, void *item)
{
if (s->top >= s->maxsize)
return (NULL);
ft_memmove(&((char *)(s->items))[s->top], item, s->itemsize);
s->top += s->itemsize;
return (item);
}
// vi: noet sw=4 ts=4:

26
src/stack_data.h Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#ifndef STACK_DATA_H
# define STACK_DATA_H
#include <stdlib.h>
typedef struct s_stack
{
size_t itemsize;
size_t maxsize;
size_t top;
void *items;
} t_stack;
#endif
// vi: noet sw=4 ts=4:

60
src/stack_extra_ops.c Normal file
View File

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#include <ft_stack.h>
#include <stdlib.h>
#include "libft.h"
#include "stack_data.h"
static void *reverse_array(void *start, void *end, size_t itemsize);
t_stack *stack_lrotate(t_stack *s, unsigned int step)
{
return (NULL);
}
t_stack *stack_rrotate(t_stack *s, unsigned int step)
{
step = step % (stack_height(s) - 1);
reverse_array(s->items,
&((char *)(s->items))[(step - 1) * s->itemsize],
s->itemsize);
reverse_array(&((char *)(s->items))[step * s->itemsize],
&((char *)(s->items))[s->top - s->itemsize],
s->itemsize);
reverse_array(s->items,
&((char *)(s->items))[s->top - s->itemsize],
s->itemsize);
return (s);
}
static void *reverse_array(void *start, void *end, size_t itemsize)
{
void *const tmp = malloc(itemsize);
if (!tmp)
return (NULL);
if (start < end)
while ((char *)start + itemsize <= (char *)end)
{
ft_memcpy(tmp, start, itemsize);
ft_memcpy(start, end, itemsize);
ft_memcpy(end, tmp, itemsize);
start = (char *)start + itemsize;
end = (char *)end - itemsize;
}
return (start);
}
// vi: noet sw=4 ts=4:

46
src/stack_life.c Normal file
View File

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#include <ft_stack.h>
#include <stdlib.h>
#include "stack_data.h"
t_stack *stack_new(size_t count, size_t size)
{
t_stack *s;
s = malloc(sizeof (t_stack));
if (s)
{
s->items = malloc(count * size);
if (s->items)
{
s->itemsize = size;
s->maxsize = count * size;
s->top = 0;
}
else
s = stack_del(s);
}
return (s);
}
void *stack_del(t_stack *s)
{
free(s->items);
free(s);
return (NULL);
}
// vi: noet sw=4 ts=4:

38
src/test.c Normal file
View File

@ -0,0 +1,38 @@
#include <stdio.h>
#include <ft_stack.h>
#include "stack_data.h"
static void print_stack(t_stack *s);
int main(void)
{
t_stack *s = stack_new(8, sizeof (int));
for (int item = 0; item < 10; item++)
{
printf("push %d\n", item);
stack_push(s, &item);
print_stack(s);
}
printf("pop %d\n", *(int*)stack_pop(s));
print_stack(s);
printf("right rotate step 1\n");
stack_rrotate(s, 1);
print_stack(s);
printf("right rotate step 3\n");
stack_rrotate(s, 3);
print_stack(s);
return 0;
}
static void print_stack(t_stack *s)
{
for (size_t i = 0; i < stack_height(s); i++)
printf("%d ", ((int*)(s->items))[i]);
putchar('\n');
}