Advertisement
Mary_99

TASK 1a wesja alpha

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