14. Implement two stacks in an array
My Approach
Time and Auxiliary Space Complexity
Code (C++)
class twoStacks
{
int *arr;
int size;
int top1, top2;
public:
twoStacks(int n = 100)
{
size = n;
arr = new int[n];
top1 = -1;
top2 = size;
}
void push1(int x)
{
arr[++top1] = x;
}
void push2(int x)
{
arr[--top2] = x;
}
int pop1()
{
if (top1 == -1)
return -1;
return arr[top1--];
}
int pop2()
{
if (top2 == size)
return -1;
return arr[top2++];
}
};Contribution and Support
Last updated