Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions

Question

Brief the importance of span of control and its concepts.

Answered: 1 week ago

Question

What is meant by decentralisation?

Answered: 1 week ago

Question

Write down the Limitation of Beer - Lamberts law?

Answered: 1 week ago

Question

Discuss the Hawthorne experiments in detail

Answered: 1 week ago

Question

Explain the characteristics of a good system of control

Answered: 1 week ago

Question

6. Be able to choose and prepare a training site.

Answered: 1 week ago