Guest User

Untitled

a guest
Aug 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "stack.h"
  4.  
  5. void Stack_push(Stack *stack, int element)
  6. {
  7. stack->ptr++;
  8. if(stack->ptr == (stack->tos + SIZE)) {
  9. printf("Stack Overflow.\n");
  10. exit(1);
  11. }
  12. *stack->ptr = element;
  13. }
  14.  
  15. int Stack_pop(Stack *stack)
  16. {
  17. if(stack->ptr == stack->tos) {
  18. printf("Stack Underflow.\n");
  19. exit(1);
  20. }
  21. stack->ptr--;
  22. return *(stack->ptr + 1);
  23. }
  24.  
  25. void Stack_add(Stack *stack)
  26. {
  27. int a = *stack->ptr;
  28. int b = *(stack->ptr - 1);
  29. int result = a + b;
  30.  
  31. Stack_pop(stack);
  32. Stack_pop(stack);
  33. Stack_push(stack, result);
  34. }
  35.  
  36. void Stack_sub(Stack *stack)
  37. {
  38. int a = *stack->ptr;
  39. int b = *(stack->ptr - 1);
  40. int result = a - b;
  41.  
  42. Stack_pop(stack);
  43. Stack_pop(stack);
  44. Stack_push(stack, result);
  45. }
  46.  
  47. void Stack_reset(Stack *stack)
  48. {
  49. int i = 0;
  50. for (i = 0; i < SIZE; i++) {
  51. stack->body[i] = 0;
  52. }
  53. }
Add Comment
Please, Sign In to add comment