#include <iostream>
using namespace std;
class kstack{
int *s;
int *index;
int *top_of_stack;
int next_available;
public:
kstack(int capacity,int number_of_stacks){
s = new int[capacity];
index = new int[capacity];
top_of_stack = new int[number_of_stacks];
next_available = 0;
for(int i=0;i<capacity;i++)
index[i] = (i != capacity-1)?i+1 : -1;
for(int i=0;i<number_of_stacks;i++)
top_of_stack[i] = -1;
}
void push(int,int);
void pop(int);
};
void kstack::push(int stack_num,int value){
if(next_available == -1){
cout<<"All Stacks overflowed\n";
return;
}
int free_index = index[next_available];
s[next_available] = value;
index[next_available] = top_of_stack[stack_num];
top_of_stack[stack_num] = next_available;
next_available = free_index;
cout<<"Element pushed successfully\n";
}
void kstack::pop(int stack_num){
int top_index = top_of_stack[stack_num];
if(top_index == -1)
cout<<"Stack "<<stack_num<<" is empty!\n";
int top_element = s[top_of_stack[stack_num]];
cout<<top_element<<" poped\n";
top_of_stack[stack_num] = index[top_index];
index[top_index] = next_available;
next_available = top_index;
}
int main() {
kstack s(6,3);
s.push(0,2);
s.push(0,15);
s.push(2,10);
s.push(1,4);
s.pop(0);
s.push(2,3);
s.pop(2);
s.push(2,5);
s.push(2,6);
s.push(3,7);
s.push(2,8);
return 0;
}