fork(1) download
  1. #include<stdio.h>
  2. #include<limits.h>
  3.  
  4. typedef struct int_stack
  5. {
  6. size_t position;
  7. size_t length;
  8. int* container;
  9. } int_stack;
  10.  
  11. void initialize(int_stack* stack, int container[], size_t container_size)
  12. {
  13. stack->length = container_size;
  14. stack->position = 0;
  15. stack->container = container;
  16. }
  17.  
  18.  
  19. void push(int_stack* stack, int value)
  20. {
  21. if(stack->position < stack->length)
  22. {
  23. stack->container[stack->position++] = value;
  24. }
  25. }
  26.  
  27. int pop(int_stack* stack)
  28. {
  29. int value = INT_MIN;
  30. if(stack->position > 0)
  31. {
  32. value = stack->container[--stack->position];
  33. }
  34. return value;
  35. }
  36.  
  37. int main()
  38. {
  39. int_stack stack;
  40. int array[5];
  41.  
  42. initialize(&stack, array, sizeof(array)/sizeof(array[0]));
  43. push(&stack, 10);
  44. push(&stack, 2);
  45. push(&stack, 6);
  46. printf("%d\n", pop(&stack));
  47. printf("%d\n", pop(&stack));
  48. push(&stack, 3);
  49. push(&stack,-2);
  50. printf("%d\n", pop(&stack));
  51. printf("%d\n", pop(&stack));
  52. printf("%d\n", pop(&stack));
  53. printf("%d\n", pop(&stack));
  54. printf("%d\n", pop(&stack));
  55.  
  56. return 0;
  57. }
  58.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
6
2
-2
3
10
-2147483648
-2147483648