USING C++ For this project, download this file: penguin.h You work as a programm
ID: 3860235 • Letter: U
Question
USING C++
For this project, download this file:
penguin.h
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 Template Checklist
(this is penguin.h)
Explanation / Answer
Given below are the modified files and new test file for testing the implementations. Please do rate the answer if it helped. Thank you.
penguin.h
#include <iostream>
template <typename T>
class penguin {
public:
penguin();
void addp_data(T d);
T getp_data();
void set_link(penguin * next_ptr) {link = next_ptr;}
penguin * get_link() {return link;}
private:
T data;
penguin * link;
};
template <typename T>
penguin<T>::penguin() {
link = NULL;
}
template <typename T>
void penguin<T>::addp_data(T val)
{
data = val;
}
template <typename T>
T penguin<T>::getp_data()
{
return data;
}
linked_list.h
#include "penguin.h"
template <typename T>
class linked_list {
public:
linked_list() { head_ptr = NULL;}
void add_penguin(T newData);
void remove_headpenquin();
void print_penquins();
void add_sorted(T newData);
void clear();
bool found(T val);
private:
penguin<T> * head_ptr;
};
template <typename T>
void linked_list<T>::add_penguin(T newData){
penguin<T> * newOne = new penguin<T>;
newOne->addp_data(newData);
newOne->set_link(head_ptr);
head_ptr = newOne;
std::cout << "head_ptr is now: " << head_ptr->getp_data() << std::endl;
}
template <typename T>
void linked_list<T>::remove_headpenquin()
{
if (head_ptr == NULL) std::cout << "Empty List" << std::endl;
penguin<T> * doomed = head_ptr;
head_ptr = head_ptr->get_link();
delete doomed;
}
template <typename T>
void linked_list<T>::clear()
{
penguin<T>* doomed = head_ptr;
while (head_ptr != NULL)
{
remove_headpenquin();
doomed = head_ptr;
}
}
template <typename T>
void linked_list<T>::print_penquins()
{
if (head_ptr == NULL) std::cout << "Empty List" << std::endl;
else std::cout << "Printing penguins..." << std::endl;
for(penguin<T> *cur = head_ptr; cur!=NULL; cur = cur->get_link())
std::cout << cur->getp_data() << std::endl;
}
template <typename T>
bool linked_list<T>::found(T val)
{
if (head_ptr == NULL) return false;
for(penguin<T>* cur = head_ptr; cur != NULL; cur = cur->get_link())
{
if(cur->getp_data() == val) //found the value
return true;
}
return false;
}
test_penguin_list.cpp
#include "linked_list.h"
#include <iostream>
using namespace std;
int main()
{
//create a int linked list
linked_list<int> numbers;
numbers.add_penguin(2);
numbers.add_penguin(4);
numbers.add_penguin(6);
numbers.add_penguin(8);
numbers.add_penguin(10);
cout << "the numbers list is " << endl;
numbers.print_penquins();
int n[] = {4, 15, 12, 8};
for(int i = 0; i < 4; i++)
{
if(numbers.found(n[i]))
cout << "found " << n[i] << " in numbers list" << endl;
else
cout << "not found " << n[i] << " in numbers list" << endl;
}
//now createing string linked list
linked_list<string> names;
names.add_penguin("bob");
names.add_penguin("alice");
names.add_penguin("peter");
names.add_penguin("henry");
names.add_penguin("john");
cout << "the names list is " << endl;
names.print_penquins();
string ns[] = {"alice", "michael", "john", "george"};
for(int i = 0; i < 4; i++)
{
if(names.found(ns[i]))
cout << "found " << ns[i] << " in names list" << endl;
else
cout << "not found " << ns[i] << " in names list" << endl;
}
}
output
head_ptr is now: 2
head_ptr is now: 4
head_ptr is now: 6
head_ptr is now: 8
head_ptr is now: 10
the numbers list is
Printing penguins...
10
8
6
4
2
found 4 in numbers list
not found 15 in numbers list
not found 12 in numbers list
found 8 in numbers list
head_ptr is now: bob
head_ptr is now: alice
head_ptr is now: peter
head_ptr is now: henry
head_ptr is now: john
the names list is
Printing penguins...
john
henry
peter
alice
bob
found alice in names list
not found michael in names list
found john in names list
not found george in names list
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.