Question: Hello I am trying to insert a char using stack in c++ and am having trouble running my code. I am not sure what the

Hello I am trying to insert a char using stack in c++ and am having trouble running my code. I am not sure what the issue is so please check and correct my code.

#include

#include

using namespace std;

template

class Stack

{

private:

T *st;

int size;

int top;

public:

Stack(){size=10;top=-1;st=new T[size];}

Stack(int size){this->size=size;top=1;st=new T[this->size];

void push(T x);

T pop();

T peek(int index);

int stacktop();

int isEmpty();

int isFull();

void Display();

};

template

void Stack::push(T x)

{

if(isFull()){

cout<<"Stack Overflow"<

}

else{

top++;

st[top]=x;

}

}

template

T Stack::pop(){

if(isEmpty()){

cout<<"Stack Underflow"<

}

else{

x=st[top];

top--;

}

return x;

}

template

T Stack::peek(int index)

{

T x=-1;

if(top-index+1<0){

cout<<"Invalid Index"<

}

else{

x=st[top-index+1];

}

return x;

}

template

int Stack::Stacktop(){

if(isEmpty()){

return -1;

}

return st[top];

}

template

int Stack::isFull(){

if(top==size-1){return true;}

else{return false;}

}

template

int Stack::isEmpty(){

if(top==-1){return true;}

else{return false;}

}

template

void Stack::Display(){

for(int i=top;i>=0;i--){

cout<

}

cout<

}

int main(){

Stackstk(5);

stk.push('a');

stk.push('b');

stk.push('c');

stk.push('d');

stk.push('e');

stk.push('f');

stk.display();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!