#include "stack.h" void stack_push(double* stack, int* top, int size, double item) { // TODO: Check whether the stack is full before adding new item // TODO: Check if all inputs are valid // (e.g. 'stack' and 'top' must not be a null pointer; // 'size' is positive integer; 'top' < 'size') if(stack == 0 || top == 0 || size <= 0) { printf("ERROR: Invalid arguments.\n"); return; } if(*top >= size) { printf("ERROR: The stack is full.\n"); return; } *(stack + (*top)++) = item; } double stack_pop(double* stack, int* top) { // TODO: Check if all inputs are valid // (e.g. 'stack' and 'top' must not be a null pointer; // 'top' must be greater than 0) if(stack == 0 || top == 0 || *top <= 0) { printf("ERROR: Invalid arguments.\n"); return; } return *(stack + --(*top)); } void stack_clear(double* stack, int* top, int size) { int i; // TODO: Check if all inputs are valid // (e.g. 'stack' and 'top' must not be a null pointer) if(stack == 0 || top == 0 || size <= 0) { printf("ERROR: Invalid arguments.\n"); return; } // TODO: Reset all elements in 'stack' to zero. for(i = 0; i < size; i++) *(stack + i) = 0.0; *top = 0; } void stack_print(double* stack, int* top) { int i; if(stack == 0 || top == 0) { printf("ERROR: Invalid arguments.\n"); return; } for(i = 0; i < *top; i++) printf("%d: %8.2f\n", i, *(stack + i)); printf("\n"); }