Stacks Design an ADT for a twocolor Tstack that consists of
Stacks
Design an ADT for a two-color, T-stack that consists of two stacks - one \"red\" (R) and one \"blue\"
(B), where T is a template. This ADT uses a single array whose capacity is set to some value N,
where N equals the summation of the size of the red stack and the size of the blue stack. In speci c,
you are asked to design a class called RBStack. It consists of a set of variables and operations as
follows:
Member Variables
T * stackRedBlue ;
int sizeR ;
int sizeB ;
int topR ;
int topB ;
Meber Functions
constructor 1
constructor 2 // copy constructor
destructor
isFull() // is the stack full?
isFullR() // is the red stack full?
isFullB() // is the blue stack full?
isEmpty() // is the stack empty?
isEmptyR() // is the red stack empty?
isEmptyB() // is the blue stack empty?
popR( T & ) // pop an element from the red stack
popB( T & ) // pop an element from the blue stack
popR B(T&, T&) //pop an element from the red stack and an element from the blue stack
pushR( T ) // push a double into the red stack
pushB( T ) // push a double into the blue stack
pushR B( T, T ) // push a double into the rad stack and a double into the blue
stack
printR() // print the red stack
printB() // print the blue stack
- Test your ADT by a complete C++ program.
Solution
#include<iostream>
using namespace std;
template <class T>
class ADT
{
T * stackRedBlue ;
int sizeR ;
int sizeB ;
int topR ;
int topB ;
public:
//Meber Functions
//constructor 1
ADT(int s1, int s2)
{
int size = s1+s2;
stackRedBlue =new T[size];
//cout<<\"sizeof = \"<<sizeof(stackRedBlue)<<endl;
sizeR= s1;
sizeB= s2;
topR= 0;
topB= 0;
}
//constructor 2 // copy constructor
ADT(ADT &obj)
{
sizeR= obj.sizeR;
sizeB= obj.sizeB;
topR= obj.topR;
topB= obj.topB;
//stackRedBlue =new ADT(sizeR,sizeB]);
stackRedBlue = obj.stackRedBlue;
}
//destructor
~ADT()
{
delete stackRedBlue;
}
// is the stack full?
bool isFull()
{
if( (topR == sizeR) && (topB == sizeB))
return true;
else
return false;
}
// is the red stack full?
bool isFullR()
{
if( topR == sizeR)
return true;
else
return false;
}
// is the blue stack full?
bool isFullB()
{
if( topB == sizeB)
return true;
else
return false;
}
// is the stack empty?
bool isEmpty()
{
if( (topB == 0) && (topR == 0))
return true;
else
return false;
}
// is the red stack empty?
bool isEmptyR()
{
if( (topR == 0))
return true;
else
return false;
}
bool isEmptyB() // is the blue stack empty?
{
if( (topB == 0))
return true;
else
return false;
}
void popR( T &R ) // pop an element from the red stack
{
R = stackRedBlue[--topR];
delete[topR] stackRedBlue;
}
void popB( T &B ) // pop an element from the blue stack
{
--topB;
int s = sizeR+topB;
printB();
B = stackRedBlue[s];
delete[sizeR+topB] stackRedBlue;
}
void popRB(T &R, T&B) //pop an element from the red stack and an element from the blue stack
{
--topB;
int s = topR+topB;
R = stackRedBlue[--topR];
delete[topR] stackRedBlue;
B = stackRedBlue[s];
delete[sizeR+topB] stackRedBlue;
}
void pushR( T R) // push a double into the red stack
{
stackRedBlue[topR] = R;
topR++;
}
void pushB( T B ) // push a double into the blue stack
{
stackRedBlue[sizeR+topB] = B;
topB++;
}
void pushRB( T R, T B) // push a double into the rad stack and a double into the blue
{
stackRedBlue[topR] = R;
topR++;
stackRedBlue[sizeR+topB] = B;
topB++;
}
//stack
void printR() // print the red stack
{
for(int i= 0 ; i< topR ; i++)
{
cout<<stackRedBlue[i]<<endl;
}
}
void printB() // print the blue stack
{
for(int i= sizeR ; i<sizeR+topB ; i++)
{
cout<<stackRedBlue[i]<<endl;
}
}
};
int main()
{
ADT<double> stack1(2,3),stack2(stack1);
double num;
stack2.pushR(1);
stack2.pushR(9);
stack2.pushB(3);
stack2.pushB(5);
stack2.printR();
stack2.printB();
stack2.popR(num);
cout<<\"popR= \"<<num<<endl;
stack2.popB(num);
cout<<\"popB= \"<<num<<endl;
}
yet to test as this program wasquite big,,



