Question: C++ TEMPLATES MAKE, MAIN.cpp, LLIST.H AND LLIST.CPP ALL GIVEN BELOW Please do the following: Code provided below. Template the LList class. Include template < >

C++ TEMPLATES

MAKE, MAIN.cpp, LLIST.H AND LLIST.CPP ALL GIVEN BELOW

Please do the following: Code provided below.

Template the LList class. Include template < > tags wherever the class is mentioned. Since there is only one generic type - convention the name is T (instead of FirstType, SecondType).

Fix the inner classes Item. Item is setup to store an int variable.

Change approriate mentions of int to T. References to inner classes need to be changed as well - remember that they are now templated.

Copy the contents from llist.cpp into the bottom of llist.h, and fix these functions.

Make and run the program using make. It should produce the following output without valgrind errors:

HEADER FILE:

class LList{ private: struct Item { Item(const int& v, Item* p, Item* n); int val; Item *prev; Item *next; };

public: LList(); LList(const LList& other); LList& operator=(const LList& other); ~LList();

int size() const; bool empty() const; void push_back(const int& val); int& get(int pos); void clear();

private: Item* getNodeAt(int pos) const;

Item* mHead; Item* mTail; int mSize; };

CPP FILE:

#include "llist.h" LList::Item::Item(const int& v, Item* p, Item* n) : val(v), prev(p), next(n) { }

LList::LList() { mHead = new Item(int(), nullptr, nullptr); mTail = mHead; mSize = 0; }

LList::LList(const LList& other) { mHead = new Item(int(), nullptr, nullptr); mTail = mHead; mSize = 0; Item* temp = other.mHead; while(temp != other.mTail){ push_back(temp->val); temp = temp->next; } }

LList& LList::operator=(const LList& other) { if(this == &other){ return *this; } clear(); Item* temp = other.mHead; while(temp != other.mTail){ push_back(temp->val); temp = temp->next; } return *this; }

LList::~LList() { clear(); delete mHead; }

int LList::size() const { return mSize; }

bool LList::empty() const { return mSize == 0; }

void LList::push_back(const int& val) { Item* n = new Item (val, mTail->prev, mTail); if(mHead == mTail){ mHead = n; } else { mTail->prev->next = n; } mTail->prev = n; ++mSize; }

int& LList::get(int loc) { Item *temp = getNodeAt(loc); return temp->val; }

void LList::clear() { while(mHead != mTail) { Item *temp = mHead->next; delete mHead; mHead = temp; } mSize = 0; }

typename LList::Item* LList::getNodeAt(int loc) const { if(loc >= 0 && loc < mSize){ Item *temp = mHead; while(loc > 0){ temp = temp->next; loc--; } return temp; } else { return nullptr; } }

MAIN FILE:

#include #include "llist.h"

struct Pokemon { int id; std::string name;

Pokemon() : id(0), name("") { }

Pokemon(int i, std::string n) : id(i), name(n) { } };

int main() { LList pokedex;

pokedex.push_back(Pokemon(1, "Bulbasaur")); pokedex.push_back(Pokemon(4, "Charmander")); pokedex.push_back(Pokemon(7, "Squirtle"));

for (int i = 0; i < pokedex.size(); i++) { std::cout << pokedex.get(i).id << " " << pokedex.get(i).name << std::endl; }

pokedex.clear();

pokedex.push_back(Pokemon(144, "Articuno")); pokedex.push_back(Pokemon(145, "Zapdos")); pokedex.push_back(Pokemon(146, "Moltres"));

for (int i = 0; i < pokedex.size(); i++) { std::cout << pokedex.get(i).id << " " << pokedex.get(i).name << std::endl; } return 0; }

MAKEFILE:

all: pokedex

pokedex: main.cpp llist.h

g++ -g -Wall -std=c++11 main.cpp -o pokedex

valgrind --leak-check=yes ./pokedex

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!