46 lines
993 B
C
46 lines
993 B
C
|
#include <stdio.h>
|
||
|
#define MAX_SIZE 100
|
||
|
#define NUM_STACKS 3
|
||
|
|
||
|
int tops[NUM_STACKS] = {-1, -1, -1};
|
||
|
int stack[MAX_SIZE];
|
||
|
int stack_size = MAX_SIZE / NUM_STACKS;
|
||
|
|
||
|
void push(int stack_num, int value) {
|
||
|
int index = stack_num * stack_size + tops[stack_num] + 1;
|
||
|
if (tops[stack_num] < stack_size - 1) {
|
||
|
tops[stack_num]++;
|
||
|
stack[index] = value;
|
||
|
printf("Pushed %d to stack %d\n", value, stack_num);
|
||
|
} else {
|
||
|
printf("Stack %d is full\n", stack_num);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int pop(int stack_num) {
|
||
|
int index = stack_num * stack_size + tops[stack_num];
|
||
|
if (tops[stack_num] >= 0) {
|
||
|
int value = stack[index];
|
||
|
tops[stack_num]--;
|
||
|
printf("Popped %d from stack %d\n", value, stack_num);
|
||
|
return value;
|
||
|
} else {
|
||
|
printf("Stack %d is empty\n", stack_num);
|
||
|
return -1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
push(0, 10);
|
||
|
push(0, 20);
|
||
|
push(1, 30);
|
||
|
push(2, 40);
|
||
|
|
||
|
pop(0);
|
||
|
pop(1);
|
||
|
pop(2);
|
||
|
pop(2);
|
||
|
|
||
|
return 0;
|
||
|
}
|