Home / Expert Answers / Computer Science / nbsp-the-test-driver-this-was-the-slinkedlist-hpp-ifndef-slinkedlist-hpp-define-slinkedli-pa195

(Solved):   ^^The test driver This was the slinkedlist.hpp #ifndef SLINKEDLIST_HPP #define SLINKEDLI ...



An animal shelter, which holds only dogs and cats, operates on a strictly FIFO (first-in, first-out) basis. People must adopt 

^^The test driver

This was the slinkedlist.hpp

#ifndef SLINKEDLIST_HPP
#define SLINKEDLIST_HPP
#include <iostream>
template<typename T> class SLinkedList;
template<typename T> std::ostream& operator<<(std::ostream&, const
SLinkedList<T>&);
template<typename T>
class SLinkedList {
private:
class SNode {
public:
T elem;
SNode* next;
};
private:
SNode* head;
public:
SLinkedList();
~SLinkedList();
bool empty() const;
int len() const;
const T& front() const;
T greatestValue() const;
void addFront(const T& e);
void addBack(const T& e);
void addSorted(const T& e);
T removeFront();
bool remove(T v);
void reverse();
void clear();
friend std::ostream& operator<< <T>(std::ostream& out, const SLinkedList<T>& sl);
};
template<typename T>
SLinkedList<T>::SLinkedList() : head(NULL) {}
template<typename T>
SLinkedList<T>::~SLinkedList() {
clear();
}
template<typename T>
bool SLinkedList<T>::empty() const {
return head == NULL;
}
template<typename T>
int SLinkedList<T>::len() const {

 

SLinkedList<T>::SNode* p = head;
int i = 0;
while(p != NULL) {
i++;
p = p->next;
}
return i;
}
template<typename T>
const T& SLinkedList<T>::front() const {
return head->elem;
}
template<typename T>
T SLinkedList<T>::greatestValue() const {
SLinkedList<T>::SNode* p = head;
T v = head->elem;
while(p != NULL) {
if(p->elem > v)
v = p->elem;
p = p->next;
}
return v;
}
template<typename T>
void SLinkedList<T>::addFront(const T& e) {
SLinkedList<T>::SNode* v = new SNode;
v->elem = e;
v->next = head;
head = v;
}
template<typename T>
void SLinkedList<T>::addBack(const T& e) {
SLinkedList<T>::SNode* v = new SNode;
v->elem = e;
v->next = NULL;
if(empty())
head = v;
else {
SNode* p = head;
while(p->next != NULL)
p = p->next;
p->next = v;
}
}
//insertion sort
template<typename T>
void SLinkedList<T>::addSorted(const T& e) {
SLinkedList<T>::SNode* v = new SNode;
v->elem = e;

 

v->next = NULL;
if(empty())
head = v;
else if(v->elem <= head->elem) {
v->next = head;
head = v;
}
else {
SLinkedList<T>::SNode* p1 = head;
SLinkedList<T>::SNode* p2 = p1;
while(p1 != NULL && v->elem > p1->elem) {
p2 = p1;
p1 = p1->next;
}
p2->next = v;
v->next = p1;
}
}
template<typename T>
T SLinkedList<T>::removeFront() {
SNode* p = head;
T v = head->elem;
head = p->next;
delete p;
return v;
}
template<typename T>
bool SLinkedList<T>::remove(T v) {
SLinkedList<T>::SNode* p1 = head;
SLinkedList<T>::SNode* p2 = p1;
if(head->elem == v)
return removeFront();
while(p1 != NULL) {
if(p1->elem == v) {
p2->next = p1->next;
delete p1;
return true;
}
p2 = p1;
p1 = p1->next;
}
return false;
}
template<typename T>
void SLinkedList<T>::reverse() {
SLinkedList<T>::SNode* p1 = head;
SLinkedList<T>::SNode* p2 = NULL;
while(head != NULL) {
head = p1->next;
p1->next = p2;
p2 = p1;
p1 = head;
}

 

head = p2;
}
template<typename T>
void SLinkedList<T>::clear() {
while(!empty())
removeFront();
}
//global
template<typename T>
std::ostream& operator<<(std::ostream& out, const SLinkedList<T>& sl) {
typename SLinkedList<T>::SNode* p = sl.head;
out << "HEAD" << "->";
while(p != NULL) {
out << p->elem << "->";
p = p->next;
}
out << "NULL";
return out;
}
#endif

An animal shelter, which holds only dogs and cats, operates on a strictly FIFO (first-in, first-out) basis. People must adopt either the oldest (based on arrival time) of all animals at the shelter, or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of that type). i.e. They cannot select which specific animal they would like. Implement an Animalqueue class capable of maintaining this system structure. In particular, your Animalqueue class must implement the following member functions: enqueue, dequeueAny, dequeueDog, and dequeueCat. You must use the singly linked list (SLinkedList) implementation seen in class without modifying it, so you need to think carefully how to handle the order of arrival and how to use it to determine which animal is selected (Hint: use a counter instead of an actual timestamp). You can use the following code as a test driver for your implementation (see below). Please note that you must provide a simple Cat and Dog class definition to properly use the test driver, and that the Animalqueue is a simple wrapper around the Slinkedlist class. Finally, you are free to choose to implement only one queue or use two separate queues.


We have an Answer from Expert

View Expert Answer

Expert Answer


To implement an AnimalQueue class that maintains the system structure described, you can create a class that wraps around the provided SLinkedList cla
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe