From 4bb10a54cc8aac4f8b94dad91e25d9eef6779dd7 Mon Sep 17 00:00:00 2001 From: Murilo Soares Pereira Date: Sat, 3 Apr 2010 21:44:59 -0300 Subject: [PATCH] Added stack files, with push(). --- lib/stack.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ lib/stack.h | 9 +++++++++ 2 files changed, 54 insertions(+) create mode 100644 lib/stack.c create mode 100644 lib/stack.h diff --git a/lib/stack.c b/lib/stack.c new file mode 100644 index 0000000..d57fd96 --- /dev/null +++ b/lib/stack.c @@ -0,0 +1,45 @@ +#include +#include +#include "card.h" +#include "stack.h" + +struct stack *initialize_stack() { + struct stack *stack = NULL; + + stack = malloc(sizeof(stack)); + + stack->card = NULL; + stack->next = NULL; + + return(stack); +} + +bool empty(struct stack *stack) { + return(stack->card == NULL); +} + +int length(struct stack *stack) { + int length = 0; + + if (!empty(stack)) { + length = 1; + while (stack->next != NULL) { + length++; + } + } + + return(length); +} + +void push(struct stack *stack, struct card *card) { + struct stack *new_stack = NULL; + + if (empty(stack)) { + stack->card = card; + } else { + new_stack = malloc(sizeof(new_stack)); + new_stack->card = card; + new_stack->next = stack; + stack = new_stack; + } +} diff --git a/lib/stack.h b/lib/stack.h new file mode 100644 index 0000000..72daa6f --- /dev/null +++ b/lib/stack.h @@ -0,0 +1,9 @@ +#ifndef STACK_H +#define STACK_H + +struct stack { + struct card *card; + struct stack *next; +}; + +#endif