Mary_99

task2a bez macro

Oct 24th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.96 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include "task2a.h"
  6. #include "task2a.c"
  7.  
  8.  
  9. int main( int argc, char const *argv[])
  10. {
  11.    
  12.     Stack stack;
  13.     init(&stack);
  14.     push(&stack, 12);
  15.     push(&stack, 14);
  16.     push(&stack, 16);
  17.     display(&stack);
  18.     printf("popped value is:%d\n", pop(&stack));
  19.  
  20.     Stack newStack;
  21.     initAndCopy(&newStack, &stack);
  22.     pop(&newStack);
  23.     push(&newStack, 5);
  24.     push(&newStack, 11);
  25.     push(&newStack, 10);
  26.     push(&newStack, 60);
  27.     display(&newStack);
  28.     printf("\n");
  29.    
  30.     assign(&newStack, &stack);
  31.     display(&newStack);
  32.     printf("\n");
  33.     display(&stack);
  34.     printf("\n");
  35.     destroy(&newStack);
  36.     destroy(&stack);
  37.        
  38.     return 0;
  39.    
  40. }
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <stdbool.h>
  45. #include "task2a.h"
  46.  
  47. void init(Stack* s)
  48. {
  49.     s->top = -1;
  50.     s->size = SIZE;
  51.     s->data = (int*)malloc(SIZE * sizeof(int));
  52.         if(s->data == NULL)
  53.         {
  54.             printf("error");
  55.             free(s->data);
  56.             abort();
  57.         }
  58. }
  59.  
  60.  
  61. void push(Stack *s, int element)
  62. {
  63.  
  64.    if(s->top == s->size -1)
  65.     {
  66.         s->data = realloc(s->data, s->size*2*sizeof(int));
  67.         if (!s->data)
  68.         {
  69.             free(s->data);
  70.             perror("realloc");
  71.         }
  72.         s->size *= 2;
  73.     }
  74.    
  75.     s->data[++s->top] = element;
  76. }
  77.  
  78.  
  79. int pop(Stack* s)
  80. {
  81.     if(isEmpty(s))
  82.     {
  83.         printf("Stack is empty");
  84.         abort();
  85.     }
  86.     else
  87.     {
  88.         return s->data[s->top--];
  89.     }
  90. }
  91.  
  92. bool isEmpty(Stack *s)
  93. {
  94.     if (s->top == -1)
  95.     {
  96.         return true;
  97.        
  98.     }
  99.     else
  100.     {
  101.         return false;
  102.        
  103.     }
  104. }
  105.  
  106.  
  107. void destroy (Stack *s)
  108. {
  109.     free(s->data);
  110. }
  111.  
  112.  
  113. void initAndCopy(Stack* s, Stack* source)
  114. {
  115.     s->data = (int*)malloc(sizeof(int) * source->size);
  116.     if(s->data == NULL)
  117.     {
  118.         printf("unable to allocate memory");
  119.         abort();
  120.     }
  121.     s->top = source->top;
  122.     s->size = source->size;
  123.     for(int i = s->top; i>=0;i--)
  124.     {
  125.         s->data[i] = source-> data[i];
  126.     }
  127.    
  128.    
  129. }
  130.  
  131. void assign (Stack *s, Stack *source)
  132. {
  133.     if(s->size != source->size)
  134.     {
  135.         s->data = realloc(s->data, sizeof(int) * (source->size));
  136.     }
  137.    
  138.     s->top=source->top;
  139.     s->size=source->size;
  140.     for(int i = source->top; i>=0;i--)
  141.     {
  142.         s->data[i] = source-> data[i];
  143.     }
  144. }
  145.  
  146. void display(Stack *s)
  147. {
  148.     int i;
  149.     for(i = 0; i <= s->top; i++) printf("%d ", s->data[i]);
  150.    
  151. }
  152.  
  153.  
  154.  
  155.  
  156. #pragma once
  157. #include <stdio.h>
  158. #include <stdlib.h>
  159. #include <stdbool.h>
  160.  
  161.  
  162. #define SIZE 5
  163.  
  164. typedef struct{
  165.  
  166.     int* data;
  167.     int top;
  168.     int size;
  169. } Stack;
  170.  
  171. void init(Stack* s);
  172. void destroy(Stack* s);
  173. void push(Stack *s, int element);
  174. int pop(Stack* s);
  175. bool isEmpty(Stack* s);
  176. void initAndCopy(Stack* s, Stack* source);
  177. void assign (Stack *s, Stack *source);
  178. void display(Stack *s);
Advertisement
Add Comment
Please, Sign In to add comment