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

Fix this so that it follows the directions correclty. C++. Implement a Stack who

ID: 3888329 • Letter: F

Question

Fix this so that it follows the directions correclty. C++. Implement a Stack whose size can grow as elements are inserted into the stack. You will build three different implementations of this Stack. (Using templates for the type of object). Two of these implementations will be array-based. Each of these two should take an initial capacity for the stack in the constructor. The only difference between these implementations will be what happens when the Stack is full. For the first implementation, ArrayStack, you should increase the size of the array by a constant amount. For the second implementation, DoublingArrayStack, you should double the size of the array. Finally, for the third implementation, you should implement a Stack using a Linked List.

For the Linked List, implement another class for the nodes in the Linked List.

#include <iostream>

#include <iomanip>

using namespace std;

template <typename Type> //Declaration of template

class Stck

{

Type *stckP;

int sze;

Type topp;

public:

Stck(int = 10); //constrctor with default argument

~Stck(); //Destrc

bool push(const Type );

bool pop();

void dspStack();

};

int main()

{

Stck <int> stc(5); // calling class with data type int and value

cout << "********Pushing elements to stack********** ";

cout<<"Enter the elements to stack:";

int cnt = 0;

while (cnt++ != 5)

{

int temp;

cin >> temp;

stc.push(temp);

}

  

cout << " *********data in stack before poping********** ";

stc.dspStack();

  

cout << " *********poping two elements from stack********** ";

  

stc.pop();

stc.pop();

stc.dspStack();

  

return 0;

}

//Constcutor allocating the memory

template <typename T>

Stck<T>::Stck(int st)

{

sze = st > 0 ? st: 10;

stckP = new T[sze];

topp = -1;

}

//Deallocating the memory

template <typename T>

Stck<T>::~Stck()

{

delete [] stckP;

}

//Pusing values to stack

template <typename T>

bool Stck<T>::push(const T value)

{

if (topp == sze - 1)

  

{

size_t st = sze + 10;

T *pic = new (nothrow) T[st];

if (pic==NULL)

return false;

for (size_t i=0; i<sze; i++)

pic[i] = stckP[i];

delete[] stckP;

stckP = pic;

sze = st;

}

  

topp++;

stckP[topp] = value;

  

return true;

}

template <typename T>

bool Stck<T>::pop()

{

if (topp == - 1)

return false;

  

stckP[topp] = 0;

topp--;

  

return true;

}

//displaying the data

template <typename T>

void Stck<T>::dspStack()

{

for (int uu = sze -1; uu >= 0; uu--)

cout << setw(4) << stckP[uu] << endl;

}

Explanation / Answer

#include <iostream>

#include <iomanip>

using namespace std;

template <typename Type> //Declaration of template

class Stck

{

Type *stckP;

int sze;

Type topp;

public:

Stck(int = 10); //constrctor with default argument

~Stck(); //Destrc

bool push(const Type );

bool pop();

void dspStack();

};

int main()

{

Stck <int> stc(5); // calling class with data type int and value

cout << "********Pushing elements to stack********** ";

cout<<"Enter the elements to stack:";

int cnt = 0;

while (cnt++ != 5)

{

int temp;

cin >> temp;

stc.push(temp);

}

  

cout << " *********data in stack before poping********** ";

stc.dspStack();

  

cout << " *********poping two elements from stack********** ";

  

stc.pop();

stc.pop();

stc.dspStack();

  

return 0;

}

//Constcutor allocating the memory

template <typename T>

Stck<T>::Stck(int st)

{

sze = st > 0 ? st: 10;

stckP = new T[sze];

topp = -1;

}

//Deallocating the memory

template <typename T>

Stck<T>::~Stck()

{

delete [] stckP;

}

//Pusing values to stack

template <typename T>

bool Stck<T>::push(const T value)

{

if (topp == sze - 1)

  

{

size_t st = sze + 10;

T *pic = new (nothrow) T[st];

if (pic==NULL)

return false;

for (size_t i=0; i<sze; i++)

pic[i] = stckP[i];

delete[] stckP;

stckP = pic;

sze = st;

}

  

topp++;

stckP[topp] = value;

  

return true;

}

template <typename T>

bool Stck<T>::pop()

{

if (topp == - 1)

return false;

  

stckP[topp] = 0;

topp--;

  

return true;

}

//displaying the data

template <typename T>

void Stck<T>::dspStack()

{

for (int uu = sze -1; uu >= 0; uu--)

cout << setw(4) << stckP[uu] << endl;

}