In: Computer Science
Implement a class named stack pair that provides a pair of stacks. Make the class a template class. So, you will have two files: stack pair.h and stack pair.template, following the style of the text. The basic idea is that two stacks can share a single static array. This may be advantageous if only one of the stacks will be in heavy use at any one time.
• The class should have various methods to manipulate the stack:
T pop a()
T pop b()
These methods pop the top of the respective stack and return the value. Use assert to cause the program to fail if an attempt is made to pop from an empty list.
• void push a(T item)
void push b(T item)
These methods push the value onto the respective stack. Use assert to cause the program to fail if an attempt is made to push to a full stack.
• size t size a()
size t size b()
These methods return the size of the respective stack. 1
• bool is empty a()
bool is empty b()
These methods return true if the respective stack is empty and return false if the stack is not empty.
• is full a()
is full b()
These methods return true if the respective stack has no more space for new data to be pushed.
The methods return false if the respective stack has space available. Because of the implementation, these two methods will always return the same thing. The implementation of the stack should be done with a single static array. Define the CAPACITY of that array as a static constant of size 30, to start with. Variables will be needed to keep track of the top of each stack, we’ll call them top a and top b. For illustrations, we’ll use the following diagram. The CAPACITY is only 16. Notice that top a and top b indicate where the next element will go when pushing.
Test your work by writing four programs. Two of these will be copies of others. When you are instructed to check something, use an if-statement and, if the expected behavior fails, print a message and terminate the program using the exit function. Write a program that will push a few values onto the a stack, then push a few values onto the b stack. The program should pop the values on the a stack and check that they are correct as they are popped. The program should pop the values on the b stack and check that they are correct as they are popped. Copy the previous program and increase the number of values pushed on a and or b to get a total number of CAPACITY values pushed between the two stacks. The program should not fail and both stack-full functions should return true. Copy the previous program and push one more value onto one of the stacks. The program should fail. Write a program that will check that popping from an empty stack terminates the program.
stackpair.h
#ifndef STACKPAIR_H
#define STACKPAIR_H
#include<iostream>
using namespace std;
template<class T>
class stackpair
{
private:
int top_a, top_b;
public:
//static variable declaration
static const int CAPACITY;
static T *arr;
//constructor
stackpair()
{
top_a = -1;
top_b = CAPACITY;
}
//pop from stack a
T pop_a()
{
if(isempty_a())
{
throw "Stack Underflow!";
}
T item = arr[top_a];
top_a--;
return item;
}
//pop from stack b
T pop_b()
{
if(isempty_b())
{
throw "Stack Underflow!";
}
T item = arr[top_b];
top_b++;
return item;
}
//push to stack a
void push_a(T item)
{
if(isfull_a())
{
throw "Stack overflow!";
}
top_a++;
arr[top_a] = item;
}
//push to stack a
void push_b(T item)
{
if(isfull_b())
{
throw "Stack overflow!";
}
top_b--;
arr[top_b] = item;
}
//size of stack a
size_t size_a()
{
return top_a+1;
}
//size of stack b
size_t size_b()
{
return CAPACITY - top_b;
}
//check if stack a is empty
bool isempty_a()
{
return top_a<0;
}
//check if stack b is empty
bool isempty_b()
{
return top_b>=CAPACITY;
}
//check if stack a is full
bool isfull_a()
{
return top_a >= top_b-1;
}
bool isfull_b()
{
return top_a >= top_b-1;
}
};
template<class T>
const int stackpair<T>::CAPACITY = 16;
template<class T>
T* stackpair<T>:: arr = new T[stackpair::CAPACITY];
#endif // STACKPAIR_H
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* main.cpp */
#include <iostream>
#include"stackpair.h"
using namespace std;
//main function
int main()
{
//create stackpair object
stackpair<int> stk;
try
{
for(int i=0; i<8; i++)
{
stk.push_a(i+1);
stk.push_b(30-i);
}
}catch(char const *error){
cout<<error<<endl;
return 0;
}
try
{
for(int i=0; i<9; i++)
{
cout<<stk.pop_a()<<endl;
cout<<stk.pop_b()<<endl;;
}
}catch(char const *error){
cout<<error<<endl;
return 0;
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Output:
8
23
7
24
6
25
5
26
4
27
3
28
2
29
1
30
Stack Underflow!
N.B. Whether you face any problem then share with me in the comment section, I'll happy to help you.