Question
c++ * Implement the push(), pop() and reset() for class linkedStack. Only write in where it say CODE HERE - Note that pop() returns a
c++
* Implement the push(), pop() and reset() for class linkedStack.
Only write in where it say CODE HERE - Note that pop() returns a character. - In case of an error, pop() should throw myException("error message") - reset() must empty the linked list by deleting element by element.
* Compile and run exercise1.cpp
linkedStack.h
#include
using namespace std;
class Node { public: char data; Node *link; };
class myException : public runtime_error { public: myException(string const& msg) : runtime_error(msg){} };
class linkedStack { private: Node *head; //pointer to the stack
public: linkedStack(){ head = NULL; } ~linkedStack(){ reset(); } bool isEmpty(){ return head == NULL; } char top(){ if(isEmpty()){ throw myException("Stack underflow"); } return head->data; } void print(){ Node *current = head; if(isEmpty()){ cout << "
excersise1.cpp //Don't edit the code here
#include "linkedStack.h"
int main(){
linkedStack s;
try{
char v = s.pop();// shouldn't throw an exception
}
catch(myException& e){
cerr << "Error : " << e.what() << endl;
}
catch(exception& e){
cerr << "Exception catched : " << e.what() << endl;
return 1;
}
for(int i = 0; i < 10; i++){
s.push(i + 65);
}
s.pop();
cout << endl << "The stack contains:" << endl;
s.print();
cout << endl << "Resetting the stack" << endl;
s.reset();
cout << endl << "The stack contains:" << endl;
s.print();
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started