libds

a collection of simple data structures
Log | Files | Refs | LICENSE

commit cc8d46aeba01c317411ed206cbc0de20db684789
parent a7662dfad06c9cc6d930d627829d301d85fefa43
Author: sin <sin@2f30.org>
Date:   Tue,  6 May 2014 14:16:53 +0100

Add stack_size()

Diffstat:
Mds.h | 1+
Mstack.c | 10++++++++++
2 files changed, 11 insertions(+), 0 deletions(-)

diff --git a/ds.h b/ds.h @@ -15,6 +15,7 @@ void stack_free(struct stack *); void *stack_push(struct stack *, void *); void *stack_pop(struct stack *); void *stack_peek(struct stack *); +size_t stack_size(struct stack *); /* str.c */ char *replace_str(const char *, const char *, const char *); diff --git a/stack.c b/stack.c @@ -7,6 +7,7 @@ struct stacknode { struct stack { struct stacknode *top; + size_t sz; }; struct stack * @@ -18,6 +19,7 @@ stack_init(void) if (!s) return NULL; s->top = NULL; + s->sz = 0; return s; } @@ -46,6 +48,7 @@ stack_push(struct stack *s, void *data) n->data = data; n->next = s->top; s->top = n; + s->sz++; return data; } @@ -58,6 +61,7 @@ stack_pop(struct stack *s) return NULL; data = s->top->data; s->top = s->top->next; + s->sz--; return data; } @@ -68,3 +72,9 @@ stack_peek(struct stack *s) return NULL; return s->top->data; } + +size_t +stack_size(struct stack *s) +{ + return s->sz; +}