Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 10g Insider Solutions

Authors: Arun R. Kumar, John Kanagaraj, Richard Stroupe

1st Edition

0672327910, 978-0672327919

More Books

Students also viewed these Databases questions

Question

Brief the importance of span of control and its concepts.

Answered: 1 week ago

Question

What is meant by decentralisation?

Answered: 1 week ago

Question

At what level(s) was this OD intervention scoped?

Answered: 1 week ago