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

class Stack private: int size; // size of stackArray int top; I/ top of stackArr

ID: 3731928 • Letter: C

Question

class Stack private: int size; // size of stackArray int top; I/ top of stackArray int *stackArray; public: Stack(int newSize) size newSize; // set array size stackArray-new int [size]; // make a new dynamic array top-1; // no items yet // put item on top of stack void push (int newValue){ stackArray[++top] = newvalue; /* increment top, insert item */ } // take item from top of stack int pop() { return stackArray[top-]; /* access item, decrement top */ } // look at item on top of stack int peek ) bool isEmpty() boolisFull() return stackArray [top]; / access item return (top =-1); return (top= size-1); { { b; int main() Stack aStack(10); aStack.push (20); aStack.push (40); aStack.push (60); aStack. push (80); cout

Explanation / Answer

/*

Stack: Stack is linear data structure that follow a particular for it operations called Last In First Out(LIFO).

Basic two operation of stack:

Push(x)=> Add element x to the top of stack
Pop() => remove top element from the stack


To solve this problem I used your given class by modifying two things

1.Change the name of class from Stack to CharStack
2.change the data type from int to char everywhere

Sample Output:
dlrow olleh

*/

//Please copy a .cpp file and copy code from below line to the end of answer

#include<iostream>

using namespace std;
class CharStack{
private:
int size; //size of stackArray
int top; //top of stackArray
char *stackArray; //pointer to char stack

public:
//constructor to initialize all the values
CharStack(int newSize)
{
size=newSize; //Assigning size
stackArray=new char[size]; //Creating dynamic array of char type
top =-1; //no items yet
}

//method to push/insert char to the stack
void push(char newValue)
{
//Checking for stack overflow
if(isFull()==true)
{
cout<<" Stack Overflow ";
}
else {
//first increment top and them add value at top in the stack
stackArray[++top]=newValue;
}
}

//Method top pop/remove top char from stack
char pop()
{
//getting top element from array and removing by decresing top
char topChar=stackArray[top--];
return topChar;
}

//method to check if stack is empty
bool isEmpty()
{
//if top==-1 means no element in the stack, return true
if(top==-1)
{
return true;
}
else{
return false;
}
}

//method to check if stack is full
bool isFull()
{
if(top==size-1)
{
return true;
}
else{
return false;
}
}
};

int main()
{
//Declaring array a and assigning value hello world to it
char a[]={'h','e','l','l','o',' ','w','o','r','l','d','!'};

//Calculating size of array a
int size=sizeof(a)/sizeof(a[0]);

//Instantiating stack
CharStack chStack(size);

//Now adding all the element using loop,below loop is not adding last excalimation mark(!)
//you can also print ! by removing last -1 from i<size-1
for(int i=0;i<size-1;i++)
{
//Calling push function to add new char
chStack.push(a[i]);
}

//popping all the element from the stack and displaying
while(chStack.isEmpty()==false)
{
//pop char and display
cout<<chStack.pop();
}

cout<<endl;
}