In: Computer Science
Using C++. Implement l_list.cpp and l_list.h
l_list and encounterNode. The former is a richly featured linked list with a wide variety of methods and the latter is a node meant to encapsulate a game encounter. In the context of this task, the encounterNode represents a battle encounter. Each encounter will describe what a player has to watch in the course of the level in terms of enemies, rewards and such. Therefore a combination of encounterNodes contained within a linked list would represent a single level in a game. The classes and behaviors of their methods are detailed below:
l_list
-head: encounterNode*
---------------------------
+l_list()
+~l_list()
+addToFront(a:string*, b:int, c:string):int
+addToBack(a:string*, b:int, c:string):int
+addAtIndex(a:string*, b:int, c:string, index:int):int
+getListSize():int
+removeFromFront():encounterNode*
+removeFromBack():encounterNode*
+removeFromIndex(index:int):encounterNode*
+printSummaryOfList():void
+printList():void
The class has the following variables:
•head: The head pointer which demarcates the start
of the list.
The class has the following methods:
•l_list: The list constructor. It will start by
initialising a blank list with no elements.
•∼l_list: The list destructor. It should delete all of the remaining nodes in the list and deallocate all of the memory contained within. After deleting the list, it should print out the following message: ”Number of nodes deleted: X” without quotation marks where X indicates the number of nodes deleted in the process. Be sure to add a newline at the end of this output.
•addToFront: This method receives the variables to construct a new node and then allocates memory for it before adding it to the list at the front. It returns the new size of the list, that is the size of the list with the added node.
•addToBack: This method receives the variables to construct a new node and then allocates memory for it before adding it to the list at the back of the list. It returns the new size of the list, that is the size of the list with the added node.
•addAtIndex: This method receives the variables required to instantiate a new node as well as an index. This method should insert this node into the list at the given index. If the given index is not contained within the list, such as inserting at 5 when the list is only 2 nodes big, instead you must add it to the front. Note that the list is 0-indexed. It returns the new size of the list, that is the size of the list with the added node.
•getListSize: This method determines the number of nodes in the list and returns this as an integer.
•removeFromFront: This remove method removes a node from the list from the front and returns it. Note that the node is returned and not deleted; it must be properly unlinked from the list without being destroyed. If the list is empty, return NULL.
•removeFromBack: This remove method removes a node from the list from the back and returns it. Note that the node is returned and not deleted; it must be properly unlinked from the list without being destroyed. If the list is empty, return NULL.
•removeFromIndex: This method receives an index of a node to remove from the list. This node must be removed and returned without being destroyed; that is, unlinked from the list without being deleted. Note that if the index is not valid or the list is empty then the method should return NULL. The list is 0-indexed.
•printList: This method prints out the entire
list, starting from the front. If the list is empty, print ”EMPTY
LIST” without the quotation marks and a newline at the end. The
example output is given below:
Node 0
Number of Enemies: 2
Reward: Boots of Healing
Enemy 1: Imp
Enemy 2: Imp
Node 1
Number of Enemies: 2
Reward: Sword of Revealing
Light
Enemy 1: Mancubus
Enemy 2: Hell Knight
•printSummaryOfList: This method is a more
sophisticated type of print. Instead of merely printing out of the
full information of the list, it aggregates and collates the
information from the list into a more easily readable report. If
the list is empty,
print ”EMPTY LIST” without the quotation marks and a newline at the
end. The report determines the following pieces of
information:
1.The number of nodes in the
list.
2.The number of enemies, in total
in the list.
3.The number of rewards which are
healing to the player, that is if the reward has Healing or Health
in its name. You can assume that the case will match the specific
form of Healing or Health.
Therefore the output of this print operation takes the form
(considering the prior example as the list):
Number of Nodes: 2
Number of Enemies: 4
Number of Healing Rewards: 1
You are only allowed the use of the following libraries: string and iostream.
===================encounterNode.h=====================
#ifndef ENCOUNTER_NODE_H
#define ENCOUNTER_NODE_H
#include<iostream>
#include<string>
using namespace std;
class encounterNode
{
private:
string* enemies;
int numEnemies;
string reward;
public:
encounterNode* next;
encounterNode(string* list, int
numE, string re);
~encounterNode();
string* getEnemies() const;
void setEnemies(string* a, int
b);
string getEnemyAtIndex(int
a);
void setEnemyAtIndex(int a, string
b);
string getReward() const;
void setReward(string a);
int getNumEnemies() const;
void setNumEnemies(int a);
void print();
};
#endif
===================encounterNode.cpp=====================
#include "encounterNode.h"
#include<iostream>
#include<string>
using namespace std;
encounterNode::encounterNode(string* list, int numE, string
re)
{
enemies = list;
numEnemies = numE;
reward = re;
}
encounterNode::~encounterNode()
{
delete [] enemies;
}
string* encounterNode::getEnemies() const
{
return enemies;
}
void encounterNode::setEnemies(string* a, int b)
{
if (enemies != NULL)
delete[]enemies;
enemies = new string[b];
for (int i = 0; i < b; i++)
enemies[i] = a[i];
numEnemies = b;
}
string encounterNode::getEnemyAtIndex(int a)
{
if (a >= 0 && a < numEnemies)
return enemies[a];
else
return "";
}
void encounterNode::setEnemyAtIndex(int a, string b)
{
if (a >= 0 && a < numEnemies)
enemies[a] = b;
}
string encounterNode::getReward() const
{
return reward;
}
void encounterNode::setReward(string a)
{
reward = a;
}
int encounterNode::getNumEnemies() const
{
return numEnemies;
}
void encounterNode::setNumEnemies(int a)
{
numEnemies = a;
}
void encounterNode::print()
{
cout << "Number of Enemies: " <<
numEnemies << endl;
cout << "Reward: " << reward <<
endl;
for (int i = 0; i < numEnemies; i++)
{
cout << "Enemy " << (i
+ 1) << ": " << enemies[i] << endl;
}
cout << endl;
}
// encounterNode.h
#ifndef ENCOUNTER_NODE_H_
#define ENCOUNTER_NODE_H_
#include<iostream>
#include<string>
using namespace std;
class encounterNode
{
private:
string* enemies;
int numEnemies;
string reward;
public:
encounterNode* next;
encounterNode(string* list, int numE, string re);
~encounterNode();
string* getEnemies() const;
void setEnemies(string* a, int b);
string getEnemyAtIndex(int a);
void setEnemyAtIndex(int a, string b);
string getReward() const;
void setReward(string a);
int getNumEnemies() const;
void setNumEnemies(int a);
void print();
};
#endif
//end of encounterNode.h
// encounterNode.cpp
#include "encounterNode.h"
#include<iostream>
#include<string>
using namespace std;
encounterNode::encounterNode(string* list, int numE, string
re)
{
// perform a deep copy of the enemies
enemies = new string[numE];
for(int i=0;i<numE;i++)
enemies[i] = list[i];
numEnemies = numE;
reward = re;
next = NULL;
}
encounterNode::~encounterNode()
{
delete [] enemies;
}
string* encounterNode::getEnemies() const
{
return enemies;
}
void encounterNode::setEnemies(string* a, int b)
{
if (enemies != NULL)
delete[]enemies;
enemies = new string[b];
for (int i = 0; i < b; i++)
enemies[i] = a[i];
numEnemies = b;
}
string encounterNode::getEnemyAtIndex(int a)
{
if (a >= 0 && a < numEnemies)
return enemies[a];
else
return "";
}
void encounterNode::setEnemyAtIndex(int a, string b)
{
if (a >= 0 && a < numEnemies)
enemies[a] = b;
}
string encounterNode::getReward() const
{
return reward;
}
void encounterNode::setReward(string a)
{
reward = a;
}
int encounterNode::getNumEnemies() const
{
return numEnemies;
}
void encounterNode::setNumEnemies(int a)
{
numEnemies = a;
}
void encounterNode::print()
{
cout << "Number of Enemies: " << numEnemies <<
endl;
cout << "Reward: " << reward << endl;
for (int i = 0; i < numEnemies; i++)
{
cout << "Enemy " << (i + 1) << ": " <<
enemies[i] << endl;
}
cout << endl;
}
//end of encounterNode.cpp
// l_list.h
#ifndef L_LIST_H_
#define L_LIST_H_
#include "encounterNode.h"
class l_list
{
private:
encounterNode *head;
public:
l_list();
~l_list();
int addToFront(string *a , int b , string c);
int addToBack(string *a , int b , string c);
int addAtIndex(string *a , int b , string c, int
index);
int getListSize();
encounterNode* removeFromFront();
encounterNode* removeFromBack();
encounterNode* removeFromIndex(int index);
void printSummaryOfList();
void printList();
};
#endif
//end of l_list.h
// l_list.cpp
#include "l_list.h"
l_list::l_list()
{
head = NULL;
}
l_list::~l_list()
{
int count = 0;
while(head != NULL)
{
count++;
encounterNode *node = head;
head = head->next;
delete(node);
}
cout<<"Number of nodes deleted: "<<count<<endl;
}
int l_list::addToFront(string *a, int b, string c)
{
encounterNode *node = new encounterNode(a,b,c);
node->next = head;
head = node;
return getListSize();
}
int l_list::addToBack(string *a, int b, string c)
{
encounterNode *node = new encounterNode(a,b,c);
if(head == NULL)
head = node;
else
{
encounterNode *curr = head;
while(curr->next != NULL)
curr =
curr->next;
curr->next = node;
}
return getListSize();
}
int l_list::addAtIndex(string *a, int b, string c, int
index)
{
encounterNode *node = new encounterNode(a,b,c);
if(index >=0 && index <
getListSize())
{
int idx = 0;
encounterNode *curr = head;
encounterNode *prev = NULL;
while(idx < index)
{
prev =
curr;
curr =
curr->next;
idx++;
}
if(prev == NULL)
{
node->next =
head;
head =
node;
}else
{
prev->next =
node;
node->next =
curr;
}
}else
{
node->next = head;
head = node;
}
return getListSize();
}
int l_list::getListSize()
{
int count = 0;
encounterNode *node = head;
while(node != NULL)
{
count++;
node = node->next;
}
return count;
}
encounterNode* l_list::removeFromFront()
{
if(head == NULL)
return NULL;
else
{
encounterNode *node = head;
head = head->next;
return node;
}
}
encounterNode* l_list::removeFromBack()
{
if(head == NULL)
return NULL;
else
{
encounterNode *node = head;
encounterNode *prev = NULL;
while(node->next !=
NULL)
{
prev =
node;
node =
node->next;
}
if(prev == NULL)
head =
NULL;
else
prev->next =
NULL;
return node;
}
}
encounterNode* l_list::removeFromIndex(int index)
{
if(head == NULL || index < 0 || index >=
getListSize())
return NULL;
else
{
encounterNode *node = head;
encounterNode *prev = NULL;
int idx = 0;
while(idx < index)
{
prev =
node;
node =
node->next;
idx++;
}
if(prev == NULL)
head =
head->next;
else
prev->next =
node->next;
return node;
}
}
void l_list::printList()
{
if(head == NULL)
cout<<"EMPTY
LIST"<<endl;
else
{
encounterNode *node = head;
int index = 0;
while(node != NULL)
{
cout<<"Node "<<index<<endl;
node->print();
node =
node->next;
index++;
}
cout<<endl;
}
}
void l_list::printSummaryOfList()
{
if(head == NULL)
cout<<"EMPTY
LIST"<<endl;
else
{
int count = 0;
int enemies = 0;
int healing_rewards = 0;
encounterNode *node = head;
while(node != NULL)
{
count++;
enemies +=
node->getNumEnemies();
if((node->getReward().find("Healing") != string::npos) ||
(node->getReward().find("Health") != string::npos))
healing_rewards++;
node =
node->next;
}
cout<<"Number of Nodes :
"<<count<<endl;
cout<<"Number of Enemies:
"<<enemies<<endl;
cout<<"Number of Healing
Rewards: "<<healing_rewards<<endl;
}
}
//end of l_list.cpp
// main.cpp : C++ program to test the l_list class
#include "l_list.h"
#include <iostream>
using namespace std;
int main()
{
l_list list;
string enemies[] = {"Imp","Imp"};
string reward = "Boots of Healing";
list.addToFront(enemies,2,reward);
string enemies1[] = {"Mancubus","Hell Knight"};
reward = "Sword of Revealing Light";
list.addToBack(enemies1,2,reward);
list.printList();
list.printSummaryOfList();
return 0;
}
//end of program
Output: