Question

In: Computer Science

Using C++. Implement l_list.cpp and l_list.h l_list and encounterNode. The former is a richly featured linked...

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;
}

Solutions

Expert Solution

// 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:


Related Solutions

Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked...
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked List as a backing data structure Requirements Write the following structs struct Location { string name; string address; }; struct VisitNode { Location loc; VisitNode * next; }; Create a class called Stack. In this class, create a private variable VisitNode * head. This will keep track of the location of the head node. Add the following private function. This function will be used...
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class...
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class to use is probably your first step.(Please do not use collections like .push() . pop(), etc.) and instead create the implementation A templated stack class that stores type T with the following public functions: - void Push(T t) - adds t to the top of the stack - T Pop() - asserts that the stack is not empty then removes and returns the item...
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
Write C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only),...
(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only), the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined. p1=23x 9 + 18x 7+3 1. Class Node ● Private member variables: coefficient (double), exponents (integer), and next pointer. ● Setter and getter functions to set and get all member variables ● constructor 2. Class PolynomialLinkedList ● Private member variable to represent linked list (head)...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID,...
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID, name, designation and department using linked list and perform the following operations on the list. Add employee details based on department Remove employee details based on ID if found, otherwise display appropriate message Display employee details Count the number of employees in each department
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
The goal of this assignment is to implement a set container using linked lists. Use the...
The goal of this assignment is to implement a set container using linked lists. Use the authors bag3.h and bag3.cpp as a basis for implementing your set container using linked lists. The authors bag3.h and bag3.cpp can be found here https://www.cs.colorado.edu/~main/chapter5/ Since you are using the authors bag3.h and bag3.cpp for your Set container implementation, make sure that you change the name of the class and constructors to reflect the set class. Additionally you will need to implement the follow...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT