Question: 2.1 Task 1: Linked List In c++ you will be implementing two classes for this task: l list and encounterNode. The former is a richly
2.1 Task 1: Linked List
In c++ you will be implementing two classes for this task: 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 behaviours of their methods are detailed below:
encounterNode
-enemies: string*
-numEnemies: int
-reward: string
+next: encounterNode*
---------------------------
+encounterNode(list: string *, numE:int, re: string)
+~encounterNode()
+getEnemies() const:string *
+setEnemies(a:string *, b:int):void
+getEnemyAtIndex(a:int):string
+setEnemyAtIndex(a:int, b:string):void
+getReward() const:string
+setReward(a:string):void
+getNumEnemies() const:int
+setNumEnemies(a:int):void
+print():void
The class has the following variables:
enemies: A string array that quanties the enemies represented in the encounter.
numEnemies: The number of enemies in that encounter. It is the size of the pre-
ceding array.
reward: The reward for completing the encounter.
next: A pointer to the next encounter node in the linked list.
The class has the following methods:
encounterNode: The node constructor. It will take 3 arguments: a list of enemies,
the size of the prior list and the reward for that encounter.
encounterNode: The class destructor.
getters and setters: Each of the class variables except for next has a getter and a
setter. This is standard practice but there is one specic getter and setter which
sets individual elements in the enemy array that is non-standard as well.
print: A print function for each of the nodes. When called it should print out all of
the node information. The format of which is given below:
Number of Enemies: 4
Reward: Boots of Healing
Enemy 1: Imp
Enemy 2: Cyber Demon
Enemy 3: Lost Soul
Enemy 4: Imp
Remember that each line should have a newline but watch that you do not add
excessive amounts of space.
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 specic 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
For this task, you are only allowed the use of the following libraries: string and iostream.
Also remember that for each slot you must provide a main.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
