Question: Please help with the following question it is needed. You may use and copy the skeleton code provided instead of typing it all out. Question:

Please help with the following question it is needed.

You may use and copy the skeleton code provided instead of typing it all out.

Question:

Rewrite the StringNode and StringLinkedList classes using the template design pattern, your new implementation must follow the following class interface:

#include #include using namespace std;

template class Node{ public: E elem; Node* next; };

template class LinkedList { public: LinkedList (); ~ LinkedList (); bool isEmpty () const; const E& front () const; const E& back () const; void addFront (const E& e); void removeFront (); void addBack(const E& s); void removeBack (); friend ostream& operator << (ostream& out , const LinkedList & obj ){ Node * temp = obj.head; 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 * head; };

Input Main:

int main(void){ LinkedList * myList = new 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; 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; return 0; }

Program Output: [] 1 [Gandalf] [Aragorn] --> [Gandalf] [Legolas] --> [Aragorn] --> [Gandalf] Front element: Legolas Back element: Gandalf [Aragorn] --> [Gandalf] [Gandalf] [] [Gollum] [Gollum] --> [Bilbo Baggins] [Gollum] --> [Bilbo Baggins] --> [Saruman] Front element: Gollum Back element: Saruman [Gollum] --> [Bilbo Baggins] [Gollum] []

Skeleton code:

#include  #include  using namespace std; 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) { } }; class QueueEmpty : public RuntimeException { public: QueueEmpty(const string& err) : RuntimeException(err) { } }; class StackEmpty : public RuntimeException { public: StackEmpty(const string& err) : RuntimeException(err) { } }; template  class Node{ public: E elem; Node* next; }; template  class LinkedList{ public: LinkedList(); LinkedList(const LinkedList& obj); LinkedList& operator= (const LinkedList& obj); ~LinkedList(); Node* getNode(const int n) const; int size() const; 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& obj){ Node* temp = obj.head; 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: int numberOfElements; Node* head; }; template  class Queue{ public: Queue(); Queue(const Queue& obj); Queue& operator= (const Queue& obj); ~Queue(); const int size() const; bool empty() const; const E& front() const throw(QueueEmpty); void enqueue(const E& e); void dequeue() throw(QueueEmpty); friend ostream& operator << (ostream& out, const Queue& obj){ if(((obj.myQueue)->size()) > 0){ Node* temp = (obj.myQueue)->getNode(0); if(temp == NULL){out << "[]"; return out;} out << "["; while(temp != NULL){ out << temp->elem; if(temp->next != NULL){ out << "] --> [";} temp = temp->next; } out << "]"; return out; } else{ out << "[]"; return out; } } private: LinkedList* myQueue; int sizeOfQueue; }; template  class Stack{ public: Stack(); Stack(const Stack& obj); Stack& operator= (const Stack& obj); ~Stack(); const int size() const; bool empty() const; const E& top() const throw(StackEmpty); void push(const E& e); void pop() throw(StackEmpty); friend ostream& operator << (ostream& out, const Stack& obj){ if(((obj.myStack)->size()) > 0){ Node* temp = (obj.myStack)->getNode(0); if(temp == NULL){out << "[]"; return out;} out << "["; while(temp != NULL){ out << temp->elem; if(temp->next != NULL){ out << "] --> [";} temp = temp->next; } out << "]"; return out; } else{ out << "[]"; return out; } } private: LinkedList* myStack; int sizeOfStack; }; template  Stack::Stack(){ //incomplete } template  Stack::Stack(const Stack& obj){ //incomplete } template  Stack& Stack::operator= (const Stack& obj){ //incomplete } template Stack::~Stack(){ //incomplete } template  const int Stack::size() const{ //incomplete } template  bool Stack::empty() const{ //incomplete } template  const E& Stack::top() const throw(StackEmpty){ //incomplete } template  void Stack::pop() throw(StackEmpty){ //incomplete } template  void Stack::push(const E& e){ //incomplete } template  Queue::Queue(){ //incomplete } template  Queue::Queue(const Queue& obj){ //incomplete } template  Queue& Queue::operator= (const Queue& obj){ //incomplete } template Queue::~Queue(){ //incomplete } template  const int Queue::size() const{ //incomplete } template  bool Queue::empty() const{ //incomplete } template  const E& Queue::front() const throw(QueueEmpty){ //incomplete } template  void Queue::dequeue() throw(QueueEmpty){ //incomplete } template  void Queue::enqueue(const E& e){ //incomplete } template  LinkedList::LinkedList(): head(NULL), numberOfElements(0) { //incomplete } template  LinkedList::LinkedList(const LinkedList& obj){ //incomplete } template  LinkedList& LinkedList::operator= (const LinkedList& obj){ //incomplete } template  LinkedList::~LinkedList(){ //incomplete } template int LinkedList::size() const { //incomplete } template  bool LinkedList::isEmpty() const{ //incomplete } template  const E& LinkedList::front() const throw(LinkedListEmpty){ //incomplete } template  const E& LinkedList::back() const throw(LinkedListEmpty){ //incomplete } template  void LinkedList::addFront(const E& e){ //incomplete } template  void LinkedList::removeFront() throw(LinkedListEmpty){ //incomplete } template  void LinkedList::addBack(const E& s){ //incomplete } template  void LinkedList::removeBack() throw(LinkedListEmpty){ //incomplete } template  Node* LinkedList::getNode(const int n) const{ Node* temp = head; if(n <= numberOfElements-1){ for(int i =0; i < n; i++ ){ temp = temp->next; } return temp; } } int main(void){ LinkedList* myList = new LinkedList(); cout << myList->size() << endl; cout<< *myList << endl; //Adding to the front cout << myList->isEmpty() << endl; myList->addFront("Gandalf"); cout<< *myList << endl; myList->addFront("Aragorn"); cout<< *myList << endl; cout << myList->size() << endl; myList->addFront("Legolas"); cout<< *myList << endl; Node* myNode = myList->getNode(0); cout << myNode->elem << endl; cout << myList->size() << endl; cout << "Front element: \t"<< myList->front() << endl; cout << "Back element: \t"<< myList->back() << endl; //Removing from the front myList->removeFront(); cout<< *myList << endl; cout << myList->size() << endl; myList->removeFront(); cout<< *myList << endl; cout << myList->size() << endl; myList->removeFront(); cout<< *myList << endl; cout << myList->size() << endl; //Should be able to handle this myList->removeFront(); //Adding from the back myList->addBack("Gollum"); cout<< *myList << endl; cout << myList->size() << endl; myList->addBack("Bilbo Baggins"); cout<< *myList << endl; cout << myList->size() << endl; myList->addBack("Saruman"); cout << " 1: "<< *myList << endl; LinkedList* myList2 = new LinkedList(*myList); cout << " 2: "<< *myList2 << endl; LinkedList* myList3 = new LinkedList(); *myList3 = *myList; cout << " 3: "<< *myList3 << endl; cout << "Front element: \t"<< myList->front() << endl; cout << "Back element: \t"<< myList->back() << endl; //Removing from the back myList->removeBack(); cout<< *myList << endl; cout << myList->size() << endl; myList->removeBack(); cout<< *myList << endl; cout << myList->size() << endl; myList->removeBack(); cout<< *myList << endl; cout << myList->size() << endl; //Should be able to handle this myList->removeBack(); cout<< *myList << endl; cout << myList->size() << endl; Queue* myQueue = new Queue(); myQueue->enqueue("Ritso"); myQueue->enqueue("Ritesh"); myQueue->enqueue("Ajoodha"); cout << *myQueue << endl; myQueue->dequeue(); cout << *myQueue << endl; myQueue->dequeue(); cout << *myQueue << endl; myQueue->dequeue(); cout << *myQueue << endl; myQueue->dequeue(); cout << *myQueue << endl; myQueue->dequeue(); Stack* myStack = new Stack(); myStack->push("Ritso"); myStack->push("Ritesh"); myStack->push("Ajoodha"); cout << *myStack << endl; myStack->pop(); cout << *myStack << endl; myStack->pop(); cout << *myStack << endl; myStack->pop(); cout << *myStack << endl; myStack->pop(); cout << *myStack << endl; myStack->pop(); return 0; } 

Skeleton Code:

#include  #include  using namespace std; 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) { } }; class QueueEmpty : public RuntimeException { public: QueueEmpty(const string& err) : RuntimeException(err) { } }; class StackEmpty : public RuntimeException { public: StackEmpty(const string& err) : RuntimeException(err) { } }; template  class Node{ public: E elem; Node* next; }; template  class LinkedList{ public: LinkedList(); LinkedList(const LinkedList& obj); LinkedList& operator= (const LinkedList& obj); ~LinkedList(); Node* getNode(const int n) const; int size() const; 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& obj){ Node* temp = obj.head; 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: int numberOfElements; Node* head; }; template  class Queue{ public: Queue(); Queue(const Queue& obj); Queue& operator= (const Queue& obj); ~Queue(); const int size() const; bool empty() const; const E& front() const throw(QueueEmpty); void enqueue(const E& e); void dequeue() throw(QueueEmpty); friend ostream& operator << (ostream& out, const Queue& obj){ if(((obj.myQueue)->size()) > 0){ Node* temp = (obj.myQueue)->getNode(0); if(temp == NULL){out << "[]"; return out;} out << "["; while(temp != NULL){ out << temp->elem; if(temp->next != NULL){ out << "] --> [";} temp = temp->next; } out << "]"; return out; } else{ out << "[]"; return out; } } private: LinkedList* myQueue; int sizeOfQueue; }; template  class Stack{ public: Stack(); Stack(const Stack& obj); Stack& operator= (const Stack& obj); ~Stack(); const int size() const; bool empty() const; const E& top() const throw(StackEmpty); void push(const E& e); void pop() throw(StackEmpty); friend ostream& operator << (ostream& out, const Stack& obj){ if(((obj.myStack)->size()) > 0){ Node* temp = (obj.myStack)->getNode(0); if(temp == NULL){out << "[]"; return out;} out << "["; while(temp != NULL){ out << temp->elem; if(temp->next != NULL){ out << "] --> [";} temp = temp->next; } out << "]"; return out; } else{ out << "[]"; return out; } } private: LinkedList* myStack; int sizeOfStack; }; template  Stack::Stack(){ //incomplete } template  Stack::Stack(const Stack& obj){ //incomplete } template  Stack& Stack::operator= (const Stack& obj){ //incomplete } template Stack::~Stack(){ //incomplete } template  const int Stack::size() const{ //incomplete } template  bool Stack::empty() const{ //incomplete } template  const E& Stack::top() const throw(StackEmpty){ //incomplete } template  void Stack::pop() throw(StackEmpty){ //incomplete } template  void Stack::push(const E& e){ //incomplete } template  Queue::Queue(){ //incomplete } template  Queue::Queue(const Queue& obj){ //incomplete } template  Queue& Queue::operator= (const Queue& obj){ //incomplete } template Queue::~Queue(){ //incomplete } template  const int Queue::size() const{ //incomplete } template  bool Queue::empty() const{ //incomplete } template  const E& Queue::front() const throw(QueueEmpty){ //incomplete } template  void Queue::dequeue() throw(QueueEmpty){ //incomplete } template  void Queue::enqueue(const E& e){ //incomplete } template  LinkedList::LinkedList(): head(NULL), numberOfElements(0) { //incomplete } template  LinkedList::LinkedList(const LinkedList& obj){ //incomplete } template  LinkedList& LinkedList::operator= (const LinkedList& obj){ //incomplete } template  LinkedList::~LinkedList(){ //incomplete } template int LinkedList::size() const { //incomplete } template  bool LinkedList::isEmpty() const{ //incomplete } template  const E& LinkedList::front() const throw(LinkedListEmpty){ //incomplete } template  const E& LinkedList::back() const throw(LinkedListEmpty){ //incomplete } template  void LinkedList::addFront(const E& e){ //incomplete } template  void LinkedList::removeFront() throw(LinkedListEmpty){ //incomplete } template  void LinkedList::addBack(const E& s){ //incomplete } template  void LinkedList::removeBack() throw(LinkedListEmpty){ //incomplete } template  Node* LinkedList::getNode(const int n) const{ Node* temp = head; if(n <= numberOfElements-1){ for(int i =0; i < n; i++ ){ temp = temp->next; } return temp; } } int main(void){ LinkedList* myList = new LinkedList(); cout << myList->size() << endl; cout<< *myList << endl; //Adding to the front cout << myList->isEmpty() << endl; myList->addFront("Gandalf"); cout<< *myList << endl; myList->addFront("Aragorn"); cout<< *myList << endl; cout << myList->size() << endl; myList->addFront("Legolas"); cout<< *myList << endl; Node* myNode = myList->getNode(0); cout << myNode->elem << endl; cout << myList->size() << endl; cout << "Front element: \t"<< myList->front() << endl; cout << "Back element: \t"<< myList->back() << endl; //Removing from the front myList->removeFront(); cout<< *myList << endl; cout << myList->size() << endl; myList->removeFront(); cout<< *myList << endl; cout << myList->size() << endl; myList->removeFront(); cout<< *myList << endl; cout << myList->size() << endl; //Should be able to handle this myList->removeFront(); //Adding from the back myList->addBack("Gollum"); cout<< *myList << endl; cout << myList->size() << endl; myList->addBack("Bilbo Baggins"); cout<< *myList << endl; cout << myList->size() << endl; myList->addBack("Saruman"); cout << " 1: "<< *myList << endl; LinkedList* myList2 = new LinkedList(*myList); cout << " 2: "<< *myList2 << endl; LinkedList* myList3 = new LinkedList(); *myList3 = *myList; cout << " 3: "<< *myList3 << endl; cout << "Front element: \t"<< myList->front() << endl; cout << "Back element: \t"<< myList->back() << endl; //Removing from the back myList->removeBack(); cout<< *myList << endl; cout << myList->size() << endl; myList->removeBack(); cout<< *myList << endl; cout << myList->size() << endl; myList->removeBack(); cout<< *myList << endl; cout << myList->size() << endl; //Should be able to handle this myList->removeBack(); cout<< *myList << endl; cout << myList->size() << endl; Queue* myQueue = new Queue(); myQueue->enqueue("Ritso"); myQueue->enqueue("Ritesh"); myQueue->enqueue("Ajoodha"); cout << *myQueue << endl; myQueue->dequeue(); cout << *myQueue << endl; myQueue->dequeue(); cout << *myQueue << endl; myQueue->dequeue(); cout << *myQueue << endl; myQueue->dequeue(); cout << *myQueue << endl; myQueue->dequeue(); Stack* myStack = new Stack(); myStack->push("Ritso"); myStack->push("Ritesh"); myStack->push("Ajoodha"); cout << *myStack << endl; myStack->pop(); cout << *myStack << endl; myStack->pop(); cout << *myStack << endl; myStack->pop(); cout << *myStack << endl; myStack->pop(); cout << *myStack << endl; myStack->pop(); 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!