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

Step: 3

blur-text-image

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

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions

Question

Explain the importance of Human Resource Management

Answered: 1 week ago

Question

Discuss the scope of Human Resource Management

Answered: 1 week ago

Question

Discuss the different types of leadership

Answered: 1 week ago

Question

Write a note on Organisation manuals

Answered: 1 week ago

Question

Explain the importance of nonverbal messages.

Answered: 1 week ago

Question

Describe the advantages of effective listening.

Answered: 1 week ago

Question

Prepare an employment application.

Answered: 1 week ago