Question
Need help with this question in c++. The skeleton is shown below which you can copy and paste and the input is shown along with
Need help with this question in c++.
The skeleton is shown below which you can copy and paste and the input is shown along with the desired output.
Implement the RuntimeException Super-class to provide error messages for possible exceptions that could be given by any of the function names marked by throw" in the linked list class interface below:
class RuntimeException {
private:
string errorMsg;
public:
RuntimeException (const string& err ){ errorMsg = err ;}
string getMessage () const {return errorMsg ;}
};
class LinkedListEmpty : public RuntimeException {
public:
LinkedListEmpty (const string& err) : RuntimeException (err) { }
};
template
class Node{
public:
E elem;
Node* next;
};
template
class LinkedList {
public:
LinkedList ();
~ LinkedList ();
bool isEmpty () const;
const E& front () const throw( LinkedListEmpty );
const E& back () const throw( LinkedListEmpty );
void addFront (const E& e);
void removeFront () throw( LinkedListEmpty );
void addBack(const E& s);
void removeBack () throw( LinkedListEmpty );
friend ostream& operator << (ostream& out , const LinkedList
Node
if(temp == NULL ){ out << "[]"; return out ;}
out << "[";
while(temp != NULL ){
out << temp ->elem;
if(temp ->next != NULL ){ out << "] --> [";}
temp = temp ->next;
}
out << "]";
return out;
}
private:
Node
};
Input Main:
int main(void){
LinkedList
cout << *myList << endl;
//Adding to the front
cout << myList ->isEmpty () << endl;
myList ->addFront("Gandalf");
cout << *myList << endl;
myList ->addFront("Aragorn");
cout << *myList << endl;
myList ->addFront("Legolas");
cout << *myList << endl;
cout << "Front element: \t"<< myList ->front () << endl;
cout << "Back element: \t"<< myList ->back () << endl;
//Removing from the front
myList -> removeFront ();
cout << *myList << endl;
myList -> removeFront ();
cout << *myList << endl;
myList -> removeFront ();
cout << *myList << endl;
//Should be able to handle this
myList -> removeFront ();
//Adding to the back
myList ->addBack("Gollum");
cout << *myList << endl;
myList ->addBack("Bilbo Baggins");
cout << *myList << endl;
myList ->addBack("Saruman");
cout << *myList << endl;
cout << "Front element: \t"<< myList ->front () << endl;
cout << "Back element: \t"<< myList ->back () << endl;
//Removing from the back
myList -> removeBack ();
cout << *myList << endl;
myList -> removeBack ();
cout << *myList << endl;
myList -> removeBack ();
cout << *myList << endl;
//Should be able to handle this
myList -> removeBack ();
cout << *myList << endl;
return 0;
}
Program Output:
1
[Gandalf]
[Aragorn] --> [Gandalf]
[Legolas] --> [Aragorn] --> [Gandalf]
Front element: Legolas
Back element: Gandalf
[Aragorn] --> [Gandalf]
[Gandalf]
[]
Removing the front of an empty linked list
[Gollum]
[Gollum] --> [Bilbo Baggins]
[Gollum] --> [Bilbo Baggins] --> [Saruman]
Front element: Gollum
Back element: Saruman
[Gollum] --> [Bilbo Baggins]
[Gollum]
[]
Removing the back of an empty linked list
[]
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