Question

In: Computer Science

Implement a class named stack pair that provides a pair of stacks. Make the class a...

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.

Solutions

Expert Solution

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.


Related Solutions

Make one program in c++ Implement the Point class composed of an ordered pair (?, ?)....
Make one program in c++ Implement the Point class composed of an ordered pair (?, ?). Redefine the operators in your Point class Calculate the coefficients of a Linear Regression ? = ?? + ? with next data for x and y. x y 46 4.7 72 12.3 59 6.5 56 5.9 54 6.6 70 10.3 68 8.4 48 4
Implement our own stack class patterned after Java's Stack class. Start with a generic class that...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as the...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as...
Make a public class Creater that provides a single class (static) method named subtractor. subtractor takes...
Make a public class Creater that provides a single class (static) method named subtractor. subtractor takes a single int argument and returns a method that implements the following Create interface: public interface Create { int change(int something); --------------------------------------------------------------------- The returned “function” should implement Create so that it subtracts the value passed to subtractor. For example Create one = Creater.substractor(5); Create two = Creater.subtractor(-3); System.out.println(one.change(5)); // should print 0 System.out.println(second.change(3); // should print -6 The answer should be is a single...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For simplicity the data type to be stored in the stack is String but you can also use generic type. 2. Test your class implementation by calling push() and pop(). If the stack is empty (size equals zero) pop() should return null and print that “stack is empty”. If stack is full (size equals max) push() returns false and prints that “stack is full”. This...
You are provided with a StackInterface to implement your stack, make sure to implement the data...
You are provided with a StackInterface to implement your stack, make sure to implement the data structure using java.util.ArrayList and naming your implementation ArrayListStack (make sure to use generics like you have been for the data structures so far). The provided StackTester assumes you name your implementation that way. Note that you have also been provided a PalindromeTester with the specific isPalindrome() method you are required to implement, following the approach above. The method should take whitespace, case-sensitivity, digits, and...
Implement a stack in C++ using an array, not an array list. Make your stack size...
Implement a stack in C++ using an array, not an array list. Make your stack size 5 when you test it, but do not hardcode this! You should be able to change the size for testing purposes with the change of one variable. DO NOT use Stack class defined in C++ Implement the following methods in your stack class. stack() creates an empty stacks, stack s is new and empty. push(item) adds a new item to the stack s, stacks...
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement the Stack.java in a class called ArrayStack.java that uses Generics. Write a main function that creates two stacks, one containing 10 Integers and a different one containing 10 Strings, and reverses there contents using a method called reverse that takes as a paramater a Generic array. You will need to implement all the methods of Stack.java as well as create a toString method in...
b. Implement StackFromList, a templated stack class backed by the above singlylinked list. The stack should...
b. Implement StackFromList, a templated stack class backed by the above singlylinked list. The stack should have a private linked list member, and utilize the linked list methods to implement its functionality. The stack should include a constructor, a destructor, a push, a pop, and an isEmpty method (which returns a bool). c. Implement, QueueFromList, a templated queue class backed by the above singlylinked list. The queue should have a private linked list member, and utilize the linked list methods...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT