Advertisement
Mary_99

task1a not working but in progress

Oct 11th, 2019 (edited)
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. #define SIZE 5
  6.  
  7. typedef struct{
  8.  
  9.     int* data;//hold the adress of the dynbamin array
  10.     int top;
  11.     int size;
  12. } Stack;
  13.  
  14. //void init(Stack* s);// return the base address of the stack after allocating the stack and also goinng to initilize the top and the size accordingly
  15.  
  16. Stack* init(int size);
  17. void destroy(Stack* s);
  18. void push(Stack *s, int element);
  19. int pop(Stack* s);
  20. bool isEmpty(Stack* s);
  21.  
  22.  
  23. int main( int argc, char const *argv[])
  24. {
  25.     Stack*s1 = init(SIZE);
  26.     push(s1, 10);
  27.     push(s1, 20);
  28.     push(s1, 30);
  29.     push(s1, 40);
  30.     push(s1, 50);
  31.     printf("Size of the stack: %d\n",s1->size);
  32.     push(s1, 60);
  33.     printf("Size of the stack: %d\n",s1->size);
  34.    
  35.     while(s1->top != -1)
  36.     {
  37.         printf("%d\n", pop(s1));
  38.     }
  39.    
  40.     destory (s1);
  41.  
  42.     return 0;
  43.    
  44. }
  45.  
  46.  
  47. Stack* init(int size)// when we want to construct stack
  48. {
  49.     Stack*s = (Stack*)malloc(sizeof(Stack));//constucting the stack
  50.     if(s == '\0')// if malloc fails
  51.     {
  52.         printf(" Error -- Unalble to allocate memory , terminating the program\n");
  53.         abort();
  54.         //exit(1)
  55.     }
  56.     s->data = (int*)malloc(sizeof(int) * size);// initlize elements integer pointer data i base adress //allocation of the array for the stack
  57.     if(s->data =='/0')
  58.     {
  59.         printf(" Error -- Unalble to allocate memory , terminating the program\n");
  60.         abort();
  61.         //exit(1)
  62.     }
  63.     s->top = -1;
  64.     s->size = size;
  65.     return s;
  66.    
  67. }
  68.  
  69.  
  70. void push(Stack *s, int element)
  71. {
  72.     //cheak if array is overflowed or not
  73.     //if aary is overflowed we need to double the size of the array
  74.     if(s->top == s->size -1)// is overflowed
  75.     {
  76.         //double the size of the array
  77.         int *temp = (int*)malloc(sizeof(int)* s->size * 2);
  78.         if (temp == '\0') // we can not alocate no more space there
  79.         {
  80.             printf("Error -- Unalble to allocate memory... ");
  81.             return;
  82.         }
  83.         //memory allocated succesfully
  84.         int i = 0;
  85.         for(i = 0; i <= s->top; ++i)// copying existing data to the data array on the new allocated array
  86.         {
  87.             temp[i] = s->data[i];
  88.         }
  89.         free(s->data);
  90.         s->data = temp;
  91.         s->size *= 2;
  92.     }
  93.     // if not ovcerflowed
  94.     //top willl be increment by 1 fist and than that top will be index
  95.     s->data[++s->top] = element; //this is the push operation incres the top by 1 and than put the value at the top index of the array    
  96. }
  97.  
  98.  
  99.  
  100. int pop(Stack* s)
  101. {
  102.     if(isEmpty(s))
  103.     {
  104.         printf("Stack is empty");
  105.         abort();
  106.     }
  107.     //get the value from the top and return this value/ decrement the top by 1
  108.     else
  109.     {
  110.         return s->data[s->top--];
  111.     }
  112. }
  113. bool isEmpty(Stack *s)
  114. {
  115.     (s->top == -1);
  116. }
  117.  
  118.  
  119. void destory (Stack *s)
  120. {
  121.     free(s->data);
  122.     s->top=-1;
  123.     s->size = 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement