Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define MAX_STACK_SIZE 100
- #define I_AM_NO_A_VALID_RESULT 9999
- typedef int StackElement;
- struct Stack {
- StackElement arr[MAX_STACK_SIZE];
- int top;
- };
- void stack_init(struct Stack* stack) {
- if (!stack)
- exit(-1);
- stack->top = 0;
- }
- size_t stack_size(struct Stack* stack) {
- if (!stack)
- exit(-1);
- return stack->top;
- }
- StackElement stack_peek(struct Stack* stack) {
- if (!stack)
- exit(-1);
- if (stack_size(stack) != 0) {
- return stack->arr[stack->top - 1];
- }
- }
- void stack_push(struct Stack* stack, StackElement element) {
- if (!stack)
- exit(-1);
- if (stack->top < MAX_STACK_SIZE) {
- stack->arr[stack->top] = element;
- stack->top++;
- }
- }
- StackElement stack_pop(struct Stack* stack) {
- if (!stack)
- exit(-1);
- if (stack_size(stack) > 0) {
- stack->top--;
- return stack->arr[stack->top];
- }
- }
- //********* Тесты *******************************************
- #define check_expr(expr, expected) {\
- int result = (expr); \
- if (result != (expected)) {\
- printf("Ожидал:\t '" #expr "'\t to == '%d', получил '%d' \t(функция %s в строке %d).\n", \
- (expected), result, __FUNCTION__, __LINE__);\
- error_count++;\
- }\
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement