Instructions Download and study the files OpStack.h, and opStack.cpp In this pro
ID: 3705843 • Letter: I
Question
Instructions Download and study the files OpStack.h, and opStack.cpp In this program we are using a modified version of the Stack data structure we have seen in class. The difference is that in this version, we are not just storing a long in each stack location, we are also storing a char Your Link struct should be modified to reflect this change. Your Stack struct should be a stack of Link objects, and your push function should accept two arguments, a long and a char. Fianlly, your pop function should return a pointer to Link, and not just a long Long story short, make it so that opStack.cpp compiles and runs, and that it produces the expected output. Upload your updated OpStack.h file here.Explanation / Answer
//Stack.h
#ifndef OpStack_h
#define OpStack_h
struct Link
{
int data;
char op;
Link *next;
};
struct Stack
{
Link *head;
Stack();
void push(int v, char c);
Link* pop();
bool isEmpty();
};
#endif
----------------------------------------------------
//Stack.cpp
#include"Stack.h"
#include<iostream>
Stack::Stack()
{
head = NULL;
}
void Stack::push(int v, char ch)
{
Link *newNode, *cur = head;
newNode = new Link;
newNode->data = v;
newNode->op = ch;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
}
else
{
newNode->next = head;
head = newNode;
}
}
Link *Stack::pop()
{
Link *cur = head;
head = head->next;
return cur;
}
bool Stack::isEmpty()
{
if (head == NULL)
return true;
else
return false;
}
----------------------------------------------
//main.cpp
#include<iostream>
#include"Stack.h"
using namespace std;
int main()
{
Stack* myStack = new Stack();
myStack->push(5, 'a');
myStack->push(2, 's');
myStack->push(7, 'a');
//myStack->push(10, 'p'); you can push as many items as you want
while (!myStack->isEmpty())
{
Link* temp = myStack->pop();
cout << temp->data << " - " << temp->op << endl;
delete temp;
}
return 0;
}
/*
7 - a
2 - s
5 - a
*/