Advertisement
Monika__

Stack wesrja 2a

Oct 28th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.20 KB | None | 0 0
  1. //#pragma once
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5.  
  6.  
  7. typedef struct{
  8.  
  9.     int* data;
  10.     int top;
  11.     int size;
  12. } Stack;
  13.  
  14.  
  15. void init(Stack* s);
  16. void destroy(Stack* s);
  17. void push(Stack *s, int element);
  18. int pop(Stack* s);
  19. bool isEmpty(Stack* s);
  20. void initAndCopy(Stack* s, Stack* source);
  21. void assign(Stack* s, Stack* source);
  22. void copy(Stack* s, Stack* source);
  23.  
  24.  #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdbool.h>
  27. #include "stack1a.h"
  28.  
  29. void init(Stack* s)
  30. {
  31.     s->top = -1;
  32.     s->size = 5;
  33.     s->data = (int*)malloc(s->size * sizeof(int));
  34.     if(s->data == NULL)
  35.     {
  36.         printf("Memory not allocated");
  37.         free(s->data);
  38.         exit(1);
  39.     }
  40. }
  41.  
  42. void initAndCopy(Stack* s, Stack* source)
  43. {
  44.     init(s);
  45.     copy(s, source);
  46.  
  47. }
  48.  
  49. void assign(Stack* s, Stack* source)
  50. {
  51.  
  52.     if(s->size == source->size)
  53.     {
  54.         s->top = source->top;
  55.         copy(s, source);
  56.     } else {
  57.        
  58.         s->size = source->size;
  59.         int *temp = realloc(s->data, s->size);
  60.         if(temp == NULL)
  61.         {
  62.             printf("Reallocation problem. Aborting\n");
  63.             abort();  
  64.         }
  65.         s->data = temp;
  66.         s->top = source->top;
  67.         printf("size of new stack is: %d\n", s->size);
  68.         copy(s, source);
  69.     }
  70.    
  71. }
  72. void copy(Stack* s, Stack* source)
  73. {
  74.     int counter = 0;
  75.     while(counter <= source->top)
  76.     {
  77.         s->data[counter] = source->data[counter];
  78.         //push(s, source->data[counter]);
  79.         counter++;
  80.     }
  81. }
  82.  
  83.  
  84. void push(Stack *s, int element)
  85. {
  86.  
  87.    if(s->top == s->size -1)
  88.     {
  89.         int *temp = realloc(s->data, s->size*2*sizeof(int));
  90.         if(temp == NULL)
  91.         {
  92.             printf("Reallocation problem. Aborting\n");
  93.             abort();  
  94.         } else
  95.         {
  96.             s->data = temp;
  97.         }
  98.         s->size *= 2;
  99.     }
  100.    
  101.     s->data[++s->top] = element;
  102. }
  103.  
  104.  
  105. int pop(Stack* s)
  106. {
  107.     if(isEmpty(s))
  108.     {
  109.         printf("Stack is empty\n");
  110.         //destroy(s); pozorne rozwiazanie, tylko na potrzeby valgrind
  111.         exit(1);
  112.     }
  113.     else
  114.     {
  115.         return s->data[s->top--];
  116.     }
  117. }
  118.  
  119. bool isEmpty(Stack *s)
  120. {
  121.     if (s->top == -1)
  122.     {
  123.         return true;
  124.        
  125.     }
  126.     else
  127.     {
  128.         return false;
  129.        
  130.     }
  131. }
  132.  
  133.  
  134. void destroy (Stack *s)
  135. {
  136.     free(s->data);
  137. }
  138. #include <stdio.h>
  139. #include <stdlib.h>
  140. #include <stdbool.h>
  141. #include <assert.h>
  142. #include "stack1a.h"
  143. //#include "stack1a.c"
  144.  
  145. void testIsEmpty(Stack* s)
  146. {
  147.     if(isEmpty(s))
  148.     {
  149.         printf("\nStack is empty\n");
  150.     } else {
  151.         printf("\nStack is not empty\n");
  152.     }
  153. }
  154.  
  155. int main( int argc, char const *argv[])
  156. {
  157.    
  158. //TESTING initandcopy function
  159.     Stack s1;
  160.     init(&s1);
  161.     int counter;
  162.     for(counter = 0; counter < 25; counter++)
  163.     {
  164.         push(&s1, counter);
  165.     }
  166.     Stack s2;
  167.     initAndCopy(&s2, &s1);
  168.     while(s2.top > -1)
  169.     {
  170.         assert(pop(&s2) == pop(&s1));
  171.     }
  172.     destroy(&s2);
  173.     destroy(&s1);
  174.    
  175. //TESTING THE ASSIGN FUNCTION
  176. /*
  177.     Stack s1;
  178.     init(&s1);
  179.     testIsEmpty(&s1);
  180.     Stack s2;
  181.     init(&s2);
  182.     int counter;
  183.     for(counter = 0; counter < 25; counter++)
  184.     {
  185.         push(&s1, counter);
  186.         push(&s2, counter%3);
  187.     }
  188.     testIsEmpty(&s1);
  189.  
  190.     assign(&s2, &s1);
  191.    
  192.     while(s1.top > -1)
  193.     {
  194.         assert(pop(&s2) == pop(&s1));
  195.         //printf("Value from stack\ns1: %d\n", pop(&s1));
  196.     }
  197.     testIsEmpty(&s1);
  198.  
  199.     destroy(&s2);
  200.     destroy(&s1);
  201. */    
  202.     return 0;
  203.    
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement