penguinh class penguin public penguin void addpnumint num i

penguin.h:

class penguin {

public:
penguin();
void addp_num(int num);
int getp_num();
void set_link(penguin * next_ptr) {link = next_ptr;}
penguin * get_link() {return link;}
  
private:
int p_num;
penguin * link;

};


penguin::penguin() {
p_num = 0;
}

void penguin::addp_num(int num)
{

p_num = num;
}

int penguin::getp_num()
{
return p_num;
}

class linked_list {
public:
linked_list() { head_ptr = NULL;}
void add_penguin(int newData);
void remove_headpenquin();
void print_penquins();
void add_sorted(int newData);
void clear();
private:
penguin * head_ptr;
};

void linked_list::add_penguin(int newData){
penguin * newOne = new penguin;
  
newOne->addp_num(newData);
newOne->set_link(head_ptr);
head_ptr = newOne;
std::cout << \"head_ptr is now: \" << head_ptr->getp_num() << std::endl;
  
}
void linked_list::remove_headpenquin()
{
if (head_ptr == NULL) std::cout << \"Empty List\" << std::endl;

penguin * doomed = head_ptr;
head_ptr = head_ptr->get_link();
delete doomed;
}

void linked_list::clear()
{
penguin* doomed = head_ptr;
while (head_ptr != NULL)
{
remove_headpenquin();
doomed = head_ptr;
}

}

void linked_list::print_penquins()
{
if (head_ptr == NULL) std::cout << \"Empty List\" << std::endl;
else std::cout << \"Printing penguins...\" << std::endl;
for(penguin *cur = head_ptr; cur!=NULL; cur = cur->get_link())
std::cout << cur->getp_num() << std::endl;
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

You work as a programmer for a startup company and your project manager has given you this legacy code (above) that was written by a programmer who left the company. The project manager needs you to expand the linked_list class to add a new function called found that will accept a value from the client and return true if the value exists in the list. Note the return type must be bool.

Your project manager also needs you to convert the penguin class and linked_list class to templates so the class can be used by multiple client programs, each possibly using different data types.

Additionally, you need to write a client .cpp file for testing that creates two objects of the templated linked_list class, each with different data types. You do not have to create a template for the clients.

Your program should meet the functional requirements and include a minimum of the following:

> Add a new function called found that will accept a value from the client and return true if the value exists in the list. (10 points)

> The penguin class and linked_list class converted to templates (5 points)

> A client .cpp file that has a main function create an object of the templated linked_list class with one data type and calls all of the functions of that class. Then, the main function should create a second object of the templated linked_list class with a different data type and use it to call the functions of that class. Either client should successfully run with the templated classes.  (5 points)

Hint: Remember to use the Tutorial: Template Checklist

Solution

template void TLinkedList :: remove(T value){ if(!head){ //if there is no head std::cerr << \"Error: List is Empty\" << std::endl; // The list is empty return; } if(head->data == value){ listSize--; //decremenets listSize since a node is removed node * temp = head; head = head->next; delete head; return; } else{ if(head->remove(value)){ //If something was removed as a result of the node\'s remove() listSize--; return; //decrement listSize since something was removed } else{ //else if nothing removed std::cerr << \"Error : Value not in list\" << std::endl; return; } } } template bool TLinkedList :: node :: remove(T value ){ //returns a bool. If something was removed, returns true. If nothing was removed, returns false if(next-> data == value){ node * temp = next; next = next->next; delete temp; return true; //something removed } if(!next){ return false; } else{ return next -> remove(value); } } //==============SEARCH=================== template T TLinkedList :: search(T value){ if(!head){ std::cerr << \"Error: List is empty\" << std::endl; return 0; } if(head->data == value){ //If head\'s data is the value searched for return head->data; //return head\'s data } else{ return head->search(value); } } template T TLinkedList :: node :: search(T value){ if(data == value){ return data; } if(!next){ std::cerr << \"Error: Value not in list\" << std::endl; return 0; } else{ return next->search(value); } }
penguin.h: class penguin { public: penguin(); void addp_num(int num); int getp_num(); void set_link(penguin * next_ptr) {link = next_ptr;} penguin * get_link()
penguin.h: class penguin { public: penguin(); void addp_num(int num); int getp_num(); void set_link(penguin * next_ptr) {link = next_ptr;} penguin * get_link()

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site