Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #define MAX_STACK_SIZE 100
  2. #define I_AM_NO_A_VALID_RESULT 9999
  3. typedef int StackElement;
  4.  
  5.  
  6. struct Stack {
  7.     StackElement arr[MAX_STACK_SIZE];
  8.     int top;
  9. };
  10.  
  11. void stack_init(struct Stack* stack) {
  12.     if (!stack)
  13.         exit(-1);
  14.     stack->top = 0;
  15. }
  16.  
  17. size_t stack_size(struct Stack* stack) {
  18.     if (!stack)
  19.         exit(-1);
  20.     return stack->top;
  21. }
  22.  
  23. StackElement stack_peek(struct Stack* stack) {
  24.     if (!stack)
  25.         exit(-1);
  26.     if (stack_size(stack) != 0) {
  27.         return stack->arr[stack->top - 1];
  28.     }
  29. }
  30.  
  31. void stack_push(struct Stack* stack, StackElement element) {
  32.     if (!stack)
  33.         exit(-1);
  34.     if (stack->top < MAX_STACK_SIZE) {
  35.         stack->arr[stack->top] = element;
  36.         stack->top++;
  37.     }
  38. }
  39.  
  40. StackElement stack_pop(struct Stack* stack) {
  41.     if (!stack)
  42.         exit(-1);
  43.     if (stack_size(stack) > 0) {
  44.         stack->top--;
  45.         return stack->arr[stack->top];
  46.     }
  47. }
  48.  
  49. //********* Тесты *******************************************
  50. #define check_expr(expr, expected) {\
  51.     int result = (expr); \
  52.     if (result != (expected)) {\
  53.         printf("Ожидал:\t '" #expr "'\t to == '%d', получил '%d' \t(функция %s в строке %d).\n", \
  54.                (expected), result, __FUNCTION__, __LINE__);\
  55.         error_count++;\
  56.     }\
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement