two stacks implementations

class twoStacks
{
public:
twoStacks(int arraySize);
bool isEmptyStack1();
bool isEmptyStack2();
bool isFullStack1();
bool isFullStack2();
bool popStack1(int &item);
bool popStack2(int &item);
bool pushStack1(int item);
bool pushStack2(int item);
~twoStacks();
private:
// declare appropriate private //data members
int *array;
int size;
int top1;
int top2;

};
twoStacks::twoStacks(int arraySize)
{
array = new int [size];
size = arraySize;
top1 = 0;
top2 = size-1;

}
bool twoStacks::isEmptyStack1()
{
if (top1 == 0)
return true;
else
return false;
}
bool twoStacks::isEmptyStack2()
{
if (top2 == size-1)
return true;
else
return false;
}
bool twoStacks::isFullStack1()
{
if (top1 == size-1 || top1 == top2)
return true;
else
return false;
}
bool twoStacks::isFullStack2()
{
if (top2 == 0 || top1 == top2)
return true;
else
return false;
}
bool twoStacks::popStack1(int &item)
{
if (!isEmptyStack1())
{
item = array [--top1];
return true;
}
else
return false;
}
//reference varibale can be used to store return values
bool twoStacks::popStack2(int &item)
{
if (!isEmptyStack2())
{
item = array [++top2];
return true;
}
else
return false;
}
bool twoStacks::pushStack1(int item)
{
if (!isFullStack1())
{
array [top1++] = item;
return true;
}
else
return false;
}
bool twoStacks::pushStack2(int item)
{
if (!isFullStack2())
{
array [top2--] = item;
return true;
}
else
return false;
}
twoStacks::~twoStacks()
{
delete []array;
}

Comments

Popular Posts