Note: I am coding in C++ Here is list.cpp: #define _LIST_CPP #include \"list.h\"
ID: 3824901 • Letter: N
Question
Note: I am coding in C++
Here is list.cpp:
#define _LIST_CPP
#include "list.h"
#include "animal.h"
#include <cstddef>
//
// In the `List` class, complete the `adopt` member function in `list.cpp`.
// The `adopt` function must:
//
// 1. find the animal closest to the head of the list that meets the given
// preferences (if a suitable animal exists),
// 2. remove that node from the list, and
// 3. return a pointer to the `Animal` (if a suitable animal was found) **or**
// `NULL` if no animal was found
//
template <class T>
const Animal *List<T>::adopt(bool willAdoptCats, bool willAdoptDogs) {
//your code here
}
while(temp->next != NULL) {
if (willAdoptCats) {
temp2->next = temp->next;
temp->next = NULL;
//return Animal;
} else {
temp = temp->next;
temp2 = temp2->next;
}
}
return NULL;
}
template <class T>
List<T>::List() {
this->head = NULL;
}
template <class T>
void List<T>::insertFront(const T& ndata) {
ListNode* node = new ListNode(ndata);
node->next = head;
this->head = node;
}
Consider an animal shelter that contains two types of animals: dogs and cats. A dog is represented by the Dog class, a cat is represented by a Cat class, and both classes inherit the Animal class. The animal shelter maintains a singly list of all the animals (nodes with an Animal * pointer that points to the Animal). When you want to adopt an animal from this shelter, you let them know your preference for a dog, cat, or either. Based on your preference, you adopt the animal closest to the head of the list that matches your preference. In the List class, complete the adopt member function in list.cpp The adopt function must: 1. find the animal closest to the head of the list that meets the given preferences (if a suitable animal exists), 2. remove that node from the list, and 3. return a pointer to the Animal (if a suitable animal was found) or NULL if no animal was found The List class (list.cpp, list.h) stores a singly linked list of List Node nodes, using the same structure and variable names as MP3. The Animal class has a member function getType() that will return "Dog" or "Cat" based on the type of animal. A complete Makefile and tester code is provided for you. To compile and test, run: make ./animal-testExplanation / Answer
The logic for the adopt function is as follows:
Following is the code for the required adop member function:
template <class T>
const Animal *List<T>::adopt(bool willAdoptCats, bool willAdoptDogs) {
Animal *current = head;
Animal *previous = head;
Animal *preferredAnimal = NULL;
// iterate over the animal list and check if the current animal
// is the preffered animal to adapt.
while (current != NULL) {
if (("Dog" == current->getType() && willAdoptDogs) ||
("Cat" == current->getType() && willAdoptCats)) {
preferredAnimal = current;
break;
} else {
previous = current;
current = current->next;
}
// Check if preferred animal was found, if not return NULL
if (preferredAnimal == NULL) {
return NULL;
}
// Remove the preferred animal from the list if its not NUll and return it.
if (preferredAnimal == head) {
this->head = this->head->next;
} else {
previous->next = preferredAnimal->next;
}
prefferedAnimal->next = NULL;
return prefferedAnimal;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.