szubert

int_stack.c

Oct 3rd, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. //
  2. // Created by oopc-8 on 26.09.16.
  3. //
  4.  
  5. #include "int_stack.h"
  6.  
  7. void init(tStack *s) {
  8.     tStackElement *newContent;
  9.     newContent = (tStackElement) malloc(sizeof(tStackElement));
  10.  
  11.     if (newContent == NULL) {
  12.         printf("Insufficient memory");
  13.         return -1;
  14.     }
  15.  
  16.     s->content = newContent;
  17.     s->top = -1;
  18. }
  19.  
  20. void destroy(tStack *s) {
  21.     free(s->content);
  22.     s->content = NULL;
  23.     s->top = -1;
  24. }
  25.  
  26. int isEmpty(tStack s) {
  27.     if (s.top < 0) {
  28.         return 1;
  29.     } else {
  30.         return 0;
  31.     }
  32. }
  33.  
  34. int isFull(tStack s) {
  35.     if (s.top >= s.maxSize - 1) {
  36.         return 1;
  37.     } else {
  38.         return 0;
  39.     }
  40. }
  41.  
  42. int push(tStack *s,tStackElement el) {
  43.     s->top++;
  44.     s->content = (tStackElement) realloc(s->content, sizeof(tStackElement) * (top + 1));
  45.  
  46.     s->content[s->top] = el;
  47.     return 1;
  48. }
  49.  
  50. tStackElement pop(tStack *s){
  51.     if (isEmpty(s)) {
  52.         printf("Cannot pop element, cause stack is empty");
  53.         return -1;
  54.     }
  55.  
  56.     return s->content[s->top--];
  57. }
Advertisement
Add Comment
Please, Sign In to add comment