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 << ""; } else{ while(current != NULL){ cout << current->data << " "; current = current->link; } } cout << endl; } void push(char value){ /* CODE HERE */ } char pop(){ /* CODE HERE */ } void reset(){ /* CODE HERE */ } };

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

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!