Introduce old redir program
[lcapit-junk-code.git] / static-data-structures / stack.c
blob396a9f43939c621f907a9fa3b851ef33d75fb7ed
1 #include <stdio.h>
3 #define MAX_STACK_SIZE 16
5 struct data {
6 int code;
7 };
9 struct stack {
10 struct data data[MAX_STACK_SIZE];
11 int top;
14 static void stack_init(struct stack *stack)
16 stack->top = 0;
19 static int stack_emty(const struct stack *stack)
21 return stack->top == 0;
24 static int stack_size(const struct stack *stack)
26 return stack->top;
29 static int stack_push(struct stack *stack, struct data *data)
31 if (stack_size(stack) >= MAX_STACK_SIZE)
32 return -1;
34 stack->data[stack->top] = *data;
35 stack->top++;
36 return 0;
39 static int stack_pop(struct stack *stack, struct data *data)
41 if (stack_emty(stack))
42 return -1;
44 stack->top--;
46 if (data)
47 *data = stack->data[stack->top];
48 return 0;
51 static void fill_data(struct data *data, int code)
53 data->code = code;
56 int main(void)
58 struct stack stack;
59 struct data data, data1, data2, data3, data4;
61 stack_init(&stack);
63 if (stack_emty(&stack))
64 printf("Stack is empty\n");
65 else
66 printf("Stack is not empty\n");
68 fill_data(&data1, 1);
69 fill_data(&data2, 2);
70 fill_data(&data3, 3);
71 fill_data(&data4, 4);
73 stack_push(&stack, &data1);
74 stack_push(&stack, &data2);
75 stack_push(&stack, &data3);
76 stack_push(&stack, &data4);
78 if (stack_emty(&stack))
79 printf("Stack is empty\n");
80 else
81 printf("Stack is not empty\n");
83 printf("Stack's size: %d\n", stack_size(&stack));
85 while (stack_pop(&stack, &data) == 0)
86 printf("%d\n", data.code);
88 if (stack_emty(&stack))
89 printf("Stack is empty\n");
90 else
91 printf("Stack is not empty\n");
93 return 0;