Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "stack.h"
- void Stack_push(Stack *stack, int element)
- {
- stack->ptr++;
- if(stack->ptr == (stack->tos + SIZE)) {
- printf("Stack Overflow.\n");
- exit(1);
- }
- *stack->ptr = element;
- }
- int Stack_pop(Stack *stack)
- {
- if(stack->ptr == stack->tos) {
- printf("Stack Underflow.\n");
- exit(1);
- }
- stack->ptr--;
- return *(stack->ptr + 1);
- }
- void Stack_add(Stack *stack)
- {
- int a = *stack->ptr;
- int b = *(stack->ptr - 1);
- int result = a + b;
- Stack_pop(stack);
- Stack_pop(stack);
- Stack_push(stack, result);
- }
- void Stack_sub(Stack *stack)
- {
- int a = *stack->ptr;
- int b = *(stack->ptr - 1);
- int result = a - b;
- Stack_pop(stack);
- Stack_pop(stack);
- Stack_push(stack, result);
- }
- void Stack_reset(Stack *stack)
- {
- int i = 0;
- for (i = 0; i < SIZE; i++) {
- stack->body[i] = 0;
- }
- }
Add Comment
Please, Sign In to add comment