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

2. Given the algorithm for the functions of Push() and Pop() in a Stack class in

ID: 3595207 • Letter: 2

Question

2. Given the algorithm for the functions of Push() and Pop() in a Stack class in real code, be able to fill in the blanks /1 Define a structure to be used as the stack item struct StackItem char StackItem *next; ch; li // this stack pushes and pops characters bool MyStack: :Push(char ch) I Create a new node and insert the data StackItem *newNode - new StackItem(): newNode->ch - ch; newNode->next = NULL ; 11 Check to see if the stack is empty if( // Push newNode as first in the stack else // Push newNode on top of the staclk return true; // Signal successful push

Explanation / Answer

struct StackItem
{
char ch;
StackItem *next;
};
bool MyStack::Push(char ch)
{
//create new node and insert the data
StackItem *newNode=new StackItem();
newNode->ch=ch;
newNode->next=NULL;
if(isEmpty()) // or can use (top==NULL)
{
top=newNode;//make newnode as top of stack
}
else
{
newNode->next=top;
top=newNode;
}
return true;
}
char MyStack::Pop()
{
char ch;
struct StackItem *temp;
//check for empty stack
if(isEmpty()) return '';
// remove top item from stack
temp=top;
top=top->next;
ch=temp->ch;
delete temp;//delete memory
return ch;
}

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