Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/stack.c b/stack.c
- index b77fda0..ec5d1d0 100644
- --- a/stack.c
- +++ b/stack.c
- @@ -1,13 +1,35 @@
- +/*
- + * "Stack" is very simple implementation of stack data structure
- + * Copyright (C) 2015 Robin Obůrka
- + *
- + * This program is free software: you can redistribute it and/or modify
- + * it under the terms of the GNU General Public License as published by
- + * the Free Software Foundation, either version 3 of the License, or
- + * (at your option) any later version.
- + *
- + * This program is distributed in the hope that it will be useful,
- + * but WITHOUT ANY WARRANTY; without even the implied warranty of
- + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- + * GNU General Public License for more details.
- + *
- + * You should have received a copy of the GNU General Public License
- + * along with this program. If not, see <http://www.gnu.org/licenses/>.
- + */
- +
- #include "stack.h"
- #include <stdlib.h>
- #include <stdbool.h>
- +// This is helper function.
- +// It is eventually used by stack_push() for resize inner data structures.
- static void stack_realloc(struct stack_ctx *ctx)
- {
- ctx->stack_size = 2 * ctx->stack_size;
- ctx->data = (int *) realloc(ctx->data, ctx->stack_size * sizeof(int));
- }
- +// Initialize empty stack of size initial_stack_size.
- +// Returns: complete stack's context.
- struct stack_ctx *stack_init(int initial_stack_size)
- {
- struct stack_ctx *ctx = (struct stack_ctx *) malloc(sizeof(struct stack_ctx));
- @@ -19,12 +41,14 @@ struct stack_ctx *stack_init(int initial_stack_size)
- return ctx;
- }
- +// Free inner data structures and stack's context.
- void stack_destroy(struct stack_ctx *ctx)
- {
- free(ctx->data);
- free(ctx);
- }
- +// Push one number to stack
- void stack_push(struct stack_ctx *ctx, int num)
- {
- if (ctx->pointer == ctx->stack_size) {
- @@ -34,16 +58,23 @@ void stack_push(struct stack_ctx *ctx, int num)
- ctx->data[ctx->pointer++] = num;
- }
- +// Pop top-most number from stack
- +// Returns: stored number
- int stack_pop(struct stack_ctx *ctx)
- {
- return ctx->data[--ctx->pointer];
- }
- +// Check stack status
- +// Returns: true if stack is empty; false otherwise
- bool stack_is_empty(struct stack_ctx *ctx)
- {
- - if (ctx->pointer == 0) {
- - return true;
- - }
- + return (ctx->pointer == 0);
- +}
- - return false;
- +// Check how much items are stored
- +// Returns: Count of stored numbers
- +bool stack_has_items(struct stack_ctx *ctx)
- +{
- + return (ctx->pointer != 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement