Implement in C please Develop a template class that includes

Implement in C++ please

Develop a template class that includes three functions: push, pop, is Empty.

Solution

// stackADT.h: header file
template< class T, int sizeOfStack >
class Stack {
int empty;
T elements[sizeOfStack];
int top;
public:
Stack();
~Stack();
void push(T);
T pop();
int empty();
int full();
};

// stackImplementation.cpp: function definitions
#include \"stackADT.h\"

template< class T, int sizeOfStack >
Stack< T, sizeOfStack >::Stack() {
empty = -1;
top = empty;
}

template< class T, int sizeOfStack >
Stack< T, sizeOfStack >::~Stack()
{ delete[] elements; }

template< class T, int sizeOfStack >
void Stack< T, sizeOfStack >::push(T c)
{ elements[ ++top ] = c; }

template< class T, int sizeOfStack >
T Stack< T, sizeOfStack >::pop()
{ return elements[ top-- ]; }

template< class T, int sizeOfStack >
int Stack< T, sizeOfStack >::full()
{ return top + 1 == sizeOfStack; }

template< class T, int sizeOfStack >
int Stack< T, sizeOfStack >::empty()
{ return top == empty; }


// stacktest.cpp: use templated stack
#include <iostream.h>
#include \"stackADT.h\"

int main() {
Stack<char, 10> s; // 10 chars
char ch;
while ((ch = cin.get()) != \'\ \')
if (!s.full()) s.push(ch);
while (!s.empty())
cout << s.pop();
cout << endl;
Stack<double, 4> ds; // 4 doubles
double d[] =
{1.0, 3.0, 5.0, 7.0, 9.0, 0.0};
int i = 0;
while (d[i] != 0.0 && !ds.full())
if (!ds.full()) ds.push(d[i++]);
while (!ds.empty())
cout << ds.pop() << \" \";
cout << endl;
return 0;
}

Implement in C++ please Develop a template class that includes three functions: push, pop, is Empty. Solution// stackADT.h: header file template< class T, in
Implement in C++ please Develop a template class that includes three functions: push, pop, is Empty. Solution// stackADT.h: header file template< class T, in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site