Advertisement
Hikigaya8man

git_stack.c

May 24th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. diff --git a/stack.c b/stack.c
  2. index b77fda0..ec5d1d0 100644
  3. --- a/stack.c
  4. +++ b/stack.c
  5. @@ -1,13 +1,35 @@
  6. +/*
  7. + * "Stack" is very simple implementation of stack data structure
  8. + * Copyright (C) 2015 Robin Obůrka
  9. + *
  10. + * This program is free software: you can redistribute it and/or modify
  11. + * it under the terms of the GNU General Public License as published by
  12. + * the Free Software Foundation, either version 3 of the License, or
  13. + * (at your option) any later version.
  14. + *
  15. + * This program is distributed in the hope that it will be useful,
  16. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. + * GNU General Public License for more details.
  19. + *
  20. + * You should have received a copy of the GNU General Public License
  21. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. + */
  23. +
  24. #include "stack.h"
  25. #include <stdlib.h>
  26. #include <stdbool.h>
  27.  
  28. +// This is helper function.
  29. +// It is eventually used by stack_push() for resize inner data structures.
  30. static void stack_realloc(struct stack_ctx *ctx)
  31. {
  32. ctx->stack_size = 2 * ctx->stack_size;
  33. ctx->data = (int *) realloc(ctx->data, ctx->stack_size * sizeof(int));
  34. }
  35.  
  36. +// Initialize empty stack of size initial_stack_size.
  37. +// Returns: complete stack's context.
  38. struct stack_ctx *stack_init(int initial_stack_size)
  39. {
  40. struct stack_ctx *ctx = (struct stack_ctx *) malloc(sizeof(struct stack_ctx));
  41. @@ -19,12 +41,14 @@ struct stack_ctx *stack_init(int initial_stack_size)
  42. return ctx;
  43. }
  44.  
  45. +// Free inner data structures and stack's context.
  46. void stack_destroy(struct stack_ctx *ctx)
  47. {
  48. free(ctx->data);
  49. free(ctx);
  50. }
  51.  
  52. +// Push one number to stack
  53. void stack_push(struct stack_ctx *ctx, int num)
  54. {
  55. if (ctx->pointer == ctx->stack_size) {
  56. @@ -34,16 +58,23 @@ void stack_push(struct stack_ctx *ctx, int num)
  57. ctx->data[ctx->pointer++] = num;
  58. }
  59.  
  60. +// Pop top-most number from stack
  61. +// Returns: stored number
  62. int stack_pop(struct stack_ctx *ctx)
  63. {
  64. return ctx->data[--ctx->pointer];
  65. }
  66.  
  67. +// Check stack status
  68. +// Returns: true if stack is empty; false otherwise
  69. bool stack_is_empty(struct stack_ctx *ctx)
  70. {
  71. - if (ctx->pointer == 0) {
  72. - return true;
  73. - }
  74. + return (ctx->pointer == 0);
  75. +}
  76.  
  77. - return false;
  78. +// Check how much items are stored
  79. +// Returns: Count of stored numbers
  80. +bool stack_has_items(struct stack_ctx *ctx)
  81. +{
  82. + return (ctx->pointer != 0);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement