Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN C++... Modify the code below to be a generic linked list. You should use templates to achieve this objective. Include a different data type

IN C++... Modify the code below to be a generic linked list. You should use templates to achieve this objective. Include a different data type in the test program to verify that more than 1 data type is supported.

GameEntryNode.h

#ifndef LINKEDLIST_GAMEENTRYNODE_H

#define LINKEDLIST_GAMEENTRYNODE_H

#include "GameEntry.h"

class GameEntryNode { // a node in a list of strings

private:

GameEntry elem; // element value

GameEntryNode *next; // next item in the list

friend class GameEntryLinkedList; // provide GameEntryLinkedList access

};

#endif

GameEntryLinkedList.h

#ifndef LINKEDLIST_GAMEENTRYLINKEDLIST_H

#define LINKEDLIST_GAMEENTRYLINKEDLIST_H

#include

#include "GameEntry.h"

#include "GameEntryNode.h"

class GameEntryLinkedList { // a linked list of GameEntries

public:

GameEntryLinkedList(int maxEntries = 10); // empty list constructor

~GameEntryLinkedList(); // destructor

bool empty() const; // is list empty?

const GameEntry &front() const; // get top score.

void add(const GameEntry &e); // add to the list.

std::string getList(); // get string representation of all GameEntries.

private:

GameEntryNode *head; // pointer to the head of list

int maxEntries; // maximum number of entries

void removeFront(); // remove front item list

void pruneList(); // remove any extraneous entries

};

#endif

GameEntry.h

#ifndef GAME_ENTRY_H

#define GAME_ENTRY_H

#include

using namespace std;

class GameEntry { // a game score entry

public:

GameEntry(const string& n = "", int s = 0); // constructor

string getName() const; // get player name

int getScore() const; // get score

private:

string name; // player's name

int score; // player's score

};

#endif

GameEntry.cpp

#include "GameEntry.h"

GameEntry::GameEntry(const string& n, int s) // constructor

: name(n), score(s) { }

// accessors

string GameEntry::getName() const { return name; }

int GameEntry::getScore() const { return score; }

GameEntryLinkedList.cpp

#include

#include "GameEntryNode.h"

#include "GameEntryLinkedList.h"

GameEntryLinkedList::GameEntryLinkedList(int maxEnt)

: head(nullptr), maxEntries(maxEnt) {}

GameEntryLinkedList::~GameEntryLinkedList() {

while (!empty()) removeFront();

}

bool GameEntryLinkedList::empty() const {

return head == nullptr;

}

/**

* Get the top score.

* @return The top score.

*/

const GameEntry &GameEntryLinkedList::front() const {

return head->elem;

}

/**

* Add a new GameEntry

* @param e A GameEntry.

*/

void GameEntryLinkedList::add(const GameEntry &e) {

auto *newNode = new GameEntryNode;

newNode->elem = e;

// handle condition where newNode is the first entry or

// the newNode is larger than head.

if (!head || newNode->elem.getScore() > head->elem.getScore()) {

newNode-> = head;

head = newNode;

// else find an insertion point for the newNode...

}

else {

GameEntryNode *current = head;

// advance through the nodes until we encounter a node whose next element's score

// is less than the new Node's. this node will be our insertion point.

while (current->next && newNode->elem.getScore() < current->next->elem.getScore()) {

current = current->next;

}

// insert the newNode between the current node and next node.

auto *next = current->next;

newNode->next = next;

current->next = newNode;

}

pruneList();

}

void GameEntryLinkedList::pruneList() {

// prune any extra nodes...

int nodeCount = 1;

auto *current = head;

while (nodeCount != maxEntries && current->next) {

current = current->next;

nodeCount++;

}

// if we've hit our max number of entries allowed

// and the current node has a next node, then delete it.

if (nodeCount == maxEntries && current->next) {

auto *next = current->next;

current->next = nullptr;

delete next;

}

}

void GameEntryLinkedList::removeFront() { // remove front item

GameEntryNode *old = head; // save current head

head = old->next; // skip over old head

delete old; // delete the old head

}

std::string GameEntryLinkedList::getList() {

GameEntryNode *temp = head;

std::string result;

while (temp != nullptr) {

std::string name = temp->elem.getName();

std::string score = std::to_string(temp->elem.getScore());

result.append(name).append(" : ").append(score);

result.append(" ");

temp = temp->next;

}

return result;

}

GameEntryLinkedListTest.cpp

#include

#include "GameEntryLinkedList.h"

int main() {

GameEntry g1("Dave", 90);

GameEntry g2("Ed", 700);

GameEntry g3("Adam", 590);

GameEntry g4("Joe", 980);

GameEntry g5("Loser", 0);

GameEntry g6("Note quite Loser", 5);

GameEntry g7("Mary", 19000);

GameEntry g8("Juliette", 800);

// limit to 10 entries

GameEntryLinkedList gell(10);

gell.add(g1);

gell.add(g2);

gell.add(g5);

gell.add(g4);

gell.add(g3);

gell.add(g6);

gell.add(g7);

gell.add(g8);

std::cout << gell.getList() << std::endl;

system("PAUSE");

exit(EXIT_SUCCESS);

}

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

Computer Aided Database Design

Authors: Antonio Albano, Valeria De Antonellis, A. Di Leva

1st Edition

0444877355, 978-0444877352

More Books

Students also viewed these Databases questions