Advertisement
Monika__

stack1a.c

Oct 14th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include "stack1a.h"
  5.  
  6.  /*
  7. typedef struct{
  8.  
  9.     int* data;
  10.     int top;
  11.     int size;
  12. } Stack;
  13.  
  14.  
  15. Stack* init(int size);
  16. void destroy(Stack* s);
  17. void push(Stack *s, int element);
  18. int pop(Stack* s);
  19. bool isEmpty(Stack* s);
  20.  
  21.  
  22. int main( int argc, char const *argv[])
  23. {
  24.     Stack*s1 = init(5);
  25.     push(s1, 10);
  26.     push(s1, 20);
  27.     push(s1, 30);
  28.     push(s1, 40);
  29.     push(s1, 50);
  30.     printf("Size of the stack: %d\n",s1->size);
  31.     push(s1, 60);
  32.     printf("Size of the stack: %d\n",s1->size);
  33.    
  34.     while(s1->top != -1)
  35.     {
  36.         printf("%d\n", pop(s1));
  37.     }
  38.    
  39.     destroy (s1);
  40.     return 0;
  41.    
  42. }
  43.  */
  44.  
  45. Stack* init(int size)
  46. {
  47.     Stack*s = (Stack*)malloc(sizeof(Stack));
  48.     if(!s)// if malloc fails
  49.     {
  50.         printf("Memory not allocated\n");
  51.         abort();
  52.     }
  53.    
  54.     s->data = (int*)malloc(sizeof(int) * size); //allocation of the array for the stack
  55.    
  56.     if(!s->data)
  57.     {
  58.         printf("Memory not allocated\n");
  59.         abort();    
  60.     }
  61.    
  62.     s->top = -1;
  63.     s->size = size;
  64.     return s;
  65.    
  66. }
  67.  
  68.  
  69. void push(Stack *s, int element)
  70. {
  71.  
  72.    if(s->top == s->size -1)
  73.     {
  74.        // s->data
  75.         int *temp = realloc(s->data, s->size*2*sizeof(int));
  76.         if(temp == NULL)
  77.         {
  78.             printf("Reallocation problem. Aborting\n");
  79.             abort();  
  80.         } else
  81.         {
  82.             s->data = temp;
  83.         }
  84.       /*  if (!s->data)
  85.         {
  86.             free(s->data);
  87.             perror("realloc");
  88.         }*/
  89.         s->size *= 2;
  90.     }
  91.    
  92.     s->data[++s->top] = element;
  93. }
  94.  
  95.  
  96. int pop(Stack* s)
  97. {
  98.     if(isEmpty(s))
  99.     {
  100.         printf("Stack is empty\n");
  101.         exit(1);
  102.         //abort();
  103.     }
  104.     else
  105.     {
  106.         return s->data[s->top--];
  107.     }
  108. }
  109.  
  110. bool isEmpty(Stack *s)
  111. {
  112.     if (s->top == -1)
  113.     {
  114.         return true;
  115.        
  116.     }
  117.     else
  118.     {
  119.         return false;
  120.        
  121.     }
  122. }
  123.  
  124.  
  125. void destroy (Stack *s)
  126. {
  127.     free(s->data);
  128.     free(s);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement