Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Stack {
  5.     int *startaddress;
  6.     int currentsize;
  7.     int capacity;
  8.     int initialcap;
  9. };
  10.  
  11. void stack_destroy(struct Stack stack_dest){
  12.     free(stack_dest.startaddress);
  13. }
  14.  
  15. struct Stack stack_create(int size){
  16.     int* array;
  17.     array = malloc (size * sizeof(int));   
  18.     struct Stack new_stack;
  19.     new_stack.startaddress = &array[0];
  20.     new_stack.currentsize = 0;
  21.     new_stack.capacity = size;
  22.     new_stack.initialcap = size;
  23.     printf("Stack successfully created...\n");
  24.     return new_stack;
  25. }
  26.  
  27. /*Need to pass pointer in order to change variables outside the function, also in pop*/
  28. void stack_push (struct Stack* ptr, int elem){
  29.     if (ptr->currentsize >= ptr->capacity){
  30.         printf("The stack was full, reallocating 2 times the size...\n");
  31.         ptr->startaddress = realloc(ptr->startaddress, 2* (sizeof(int) * ptr->capacity));
  32.         ptr->capacity *=2;
  33.     }
  34.     ptr->startaddress[ptr->currentsize] = elem;
  35.     ptr->currentsize+=1;
  36.     printf("Element added...\n");
  37. }
  38.  
  39. int stack_pop (struct Stack* ptr){
  40.     if(ptr->capacity > ptr->currentsize * 2 && ptr->capacity != ptr->initialcap){
  41.         ptr->startaddress = realloc(ptr->startaddress,  sizeof(int)/ ptr->capacity / 2);       
  42.         printf("Reduced stacksize, because it was bigger than needed.\n");
  43.     }
  44.     ptr->currentsize--;
  45.     return ptr->startaddress[ptr->currentsize];
  46. }
  47.  
  48. int stack_top (struct Stack stack){
  49.     return stack.startaddress[stack.currentsize-1];
  50. }
  51.  
  52. void stack_print(struct Stack stack){
  53.     if(stack.currentsize == 0){
  54.         printf("Stack is empty!\n");
  55.     }
  56.     else{      
  57.     printf("Printing all elements on commandline: \n");
  58.         for(int temp = 1; temp < stack.currentsize; temp ++){
  59.             printf("%d ",stack.startaddress[stack.currentsize-temp]);
  60.         }
  61.         printf("\n");
  62.     }
  63. }
  64.  
  65. int main(void){
  66.     int size = 2;
  67.     struct Stack stack1 = stack_create(size);
  68.     struct Stack* ptr;
  69.     ptr = &stack1;
  70.    
  71.     stack_push(ptr, 1);
  72.     stack_push(ptr, 2);
  73.     stack_push(ptr, 3);
  74.     stack_push(ptr, 4);
  75.     stack_push(ptr, 5);
  76.     stack_push(ptr, 6);
  77.     stack_push(ptr, 7);
  78.    
  79.     printf("Expecting 1 through 7:\n");
  80.     stack_print(stack1);
  81.     printf("\n");
  82.     printf("Expecting 7:\n");
  83.     printf("%d\n",stack_top(stack1));
  84.     printf("\n");
  85.     printf("Expecting 7:\n");
  86.     printf("%d\n",stack_pop(ptr));
  87.     printf("\n");
  88.     printf("Expecting 6:\n");
  89.     printf("%d\n",stack_top(stack1));
  90.     printf("\n");
  91.     printf("popping elements to reduce size...\n");
  92.     printf("%d\n",stack_pop(ptr));
  93.     printf("%d\n",stack_pop(ptr));
  94.     printf("%d\n",stack_pop(ptr));
  95.     printf("%d\n",stack_pop(ptr));
  96.     printf("%d\n",stack_pop(ptr));
  97.    
  98.     stack_destroy(stack1);
  99. return EXIT_SUCCESS;   
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement