Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a template class called stack with the following: Methods: · Constructor w

ID: 3548494 • Letter: W

Question

Write a template class called stack with the following:

Methods:

· Constructor which assigns size of the array

· A method called push to push an element to the stack

· A method called pop to pop an element from the stack

· A method called isempty that returns true/false depending on the status of the stack

· A method called isfull that returns true/false depending on the status of the stack

Data Members:

· Name of the stack

· Value of the stack

· Size of the stack


Implement all the methods and write a test driver that accepts the following:

· A stack of ten integer values, push all values and pop them out

· A stack of 15 real values, push all values and pop them out

· A stack of ten character values, push all values and pop them out

A stack of five names, push all values and pop them out

Write a template class called stack with the following: Methods: Constructor which assigns size of the array A method called push to push an element to the stack A method called pop to pop an element from the stack ·A method called is empty that returns true/false depending on the status of the stack A method called is full that returns true/false depending on the status of the stack Data Members: Name of the stack Value of the stack Size of the stack Implement all the methods and write a test driver that accepts the following: A stack of ten integer values, push all values and pop them out A stack of 15 real values, push all values and pop them out A stack of ten character values, push all values and pop them out A stack of five names, push all values and pop them out

Explanation / Answer

#include <iostream>

#include <string>

#include <cstdlib>

using namespace std;


template <class T>

class Stack {

private:

string name;

T *value;

int size;

int max;


public:


Stack(string, int );

~Stack();


void push(T &); // push element

T pop(); // pop element

bool isempty(); // return true if empty.

bool isfull(); // return true if full.

};


template <class T>

Stack<T>::Stack(string name, int n) {

name = name;

size = n;

max = n;

value = (T *) malloc(sizeof(T) * size);

}


template <class T>

Stack<T>::~Stack() {

free(value);

}


template <class T>

void Stack<T>::push(T &element) {

if(isfull()) {

cerr << "Stack is full" << endl;

exit(1);

}

value[--size] = element;

}


template <class T>

T Stack<T>::pop() {

if(isempty()) {

cerr << "Stack is Empty" << endl;

exit(1);

}

return value[size++];

}


template <class T>

bool Stack<T>::isempty() {

return size == max;

}


template <class T>

bool Stack<T>::isfull() {

return size == 0;

}


int main()

{

Stack<int> intStack("int", 10);

for(int i=1; i <=10; i++) {

intStack.push(i);

}


while(!intStack.isempty()) {

cout << intStack.pop() << " ";

}

cout << endl;


Stack<float> floatStack("float", 15);

for(int i=1; i <=15; i++) {

float f = (float) i / 15;

floatStack.push(f);

}


while(!floatStack.isempty()) {

cout << floatStack.pop() << " ";

}

cout << endl;


Stack<char> charStack("char", 10);

for(int i=1; i <=10; i++) {

char c = i + 'a';

charStack.push(c);

}


while(!charStack.isempty()) {

cout << charStack.pop() << " ";

}

cout << endl;


Stack<string> stringStack("string", 5);

for(int i=1; i <= 5; i++) {

string s = "main";

stringStack.push(s);

}


while(!stringStack.isempty()) {

cout << stringStack.pop() << endl;

}

cout << endl;


return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote