Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

All the following code is below. Stack.h was pre-written. The Queue.h class (which uses the pre-written Node.h ) class was also written. Following those three

image text in transcribed

All the following code is below. Stack.h was pre-written. The Queue.h class (which uses the pre-written Node.h) class was also written. Following those three classes is the code from "Project 1" which the directions reference

Stack.h:

#ifndef NODEB_STACK_H #define NODEB_STACK_H #include "Node.h" #include  using namespace std; templatetypename Object> class Stack { private:  Node* top; public:  Stack() {  top = nullptr; }  ~Stack() {  while (!isEmpty()) { pop(); } } bool isEmpty() const { return (top == nullptr); } void push(Object item) { if (isEmpty()) {  Node* newNode = new Node(item);  top = newNode; } else {  Node* newNode = new Node(item, top);  top = newNode; } } Object pop() { if (isEmpty()) {  return Object(); } else {  Object copy = top->getItem();  Node* topCopy = top;  top = top->getNext();  delete topCopy; // Return the Object  return copy; } } bool exists(Object item) const { Node* curr = top; while (curr != nullptr) {  if (curr->getItem() == item) {  return true; } curr = curr->getNext(); }  return false; } void printStack() const { Node* curr = top; while (curr != nullptr) {  cout getItem() getNext(); } } };  

Node.h:

templatetypename Object> class Node { private: Object item; Node* next; public: Node(Object newItem) { item = newItem; next = nullptr; } Node(Object newItem, Node* nextNode) { item = newItem; next = nextNode; } void setItem(Object newItem) { item = newItem; } Object getItem() const { return item; } void setNext(Node* nextNode) { next = nextNode; } Node* getNext() const { return next; } };

Queue.h:

#include using namespace std; templateclass t> struct node { t info; node* next; }; templateclass t> class queuetype { node* f; node *r; public: queuetype() { f = NULL; r = NULL; } void insertion(node*); node* deletion(); int isempty(); void display(); node* createnewnode(t); };

Now, the following code is from the "Project 1" that the directions reference. Basically, I wrote code that takes a text file containing information on different football players and their combine results, stores those players as a vector of objects of the class, and prints out those objects. Finally, I wrote a function that calculates the average weight of the players.

Combine.h:

#include  #include  #include  #include  #include  #include   using namespace std; class Combine { private: string name, college, pos; int height, weight; float dash, bench; public:  Combine() { name = "John Smith"; college = "University"; pos = "player"; height = 0; weight = 0; dash = 0; bench = 0; } Combine(string name, string college, string pos, int height, int weight, float dash, float bench) { this->name = name; this->college = college; this->pos = pos; setHeight(height); setWeight(weight); setDash(dash); setBench(bench); }   string getName() const  { return name; } string getCollege() const  { return college; } string getPos() const  { return pos; } int getHeight() const  { return height; } int getWeight() const  { return weight; } float getDash() const  { return dash; } float getBench() const  { return bench; }   void setName(string name) { this->name = name; } void setCollege(string college) { this->college = college; } void setPos(string pos) { this->pos = pos; } void setHeight(int height) { if (height //Default value  this->height = 0; } else  { this->height = height; } } void setWeight(int weight) { if (weight   this->weight = 0; } else  { this->weight = weight; } } void setDash(float dash) { if (dash   this->dash = 0; } else  { this->dash = dash; } } void setBench(float bench) { if (bench //Default value  this->bench = 0; } else  { this->bench = bench; } }   friend ostream& operatorconst Combine &com) { outs return outs; } };  void getPlayersFromFile(string filename, vector &players) { ifstream inFile;  inFile.open("../" + filename);   string name = "", college = "", pos = "", header = ""; string temp; int height = 0, weight = 0; float dash = 0, bench = 0; char comma = ',';   if (inFile) {  getline(inFile, header);  while (inFile && inFile.peek() != EOF) {  getline(inFile,temp);  stringstream ss(temp);  getline(ss,name,',');  getline(ss,college,',');  getline(ss,pos,',');  getline(ss,temp,','); height = atoi(temp.c_str());  getline(ss,temp,','); weight = atoi(temp.c_str()); if(ss.good()) {  getline(ss,temp,','); dash = atof(temp.c_str()); } else{ dash=0; } if(ss.good()) {  getline(ss,temp,','); bench = atof(temp.c_str()); } else{ bench=0; }  players.push_back(Combine(name, college, pos, height, weight, dash, bench)); } } else  { cerr "File not found"  float avgWeight(vector &players) { float total = 0; getPlayersFromFile("CombinePlayers.txt", players); for (int i = 0; i   } cout "The average weight of a player is: " "lbs"  

Main.cpp:

#include "Combine.h" #include  using namespace std; int main() { vector players; getPlayersFromFile("CombinePlayers.txt", players); for (int i = 0; i   cout return 0; }

CombinePlayers.txt:

Name, College, POS, Height (in), Weight (lbs), 40 Yard, Bench Press Johnathan Abram, Mississippi State, S, 71, 205, 4.45, 0 Paul Adams, Missouri, OT, 78, 317, 5.18, 16 Nasir Adderley, Delaware, S, 72, 206, , 0 Azeez Al-Shaair, Florida Atlantic, LB, 73, 234, , 16 Otaro Alaka, Texas A&M, LB, 75, 239, 4.82, 20 Dakota Allen, Texas Tech, LB, 73, 232, 4.77, 23 Josh Allen, Kentucky, EDG, 77, 262, 4.63, 28 Zach Allen, Boston College, DE, 76, 281, 5, 24
Main function Create three Queue objects: one of integers, one of strings, and one of a custom data type (ideally the type you created in Project 1). Demonstrate that the Queue methods work correctly by calling methods on the objects and printing out to the console when appropriate. Perform the following operations: Create a Queue object and a Stack object, both of the type you created in Project 1. Print and push the first 10 objects from your vector (from Project 1) onto the Queue. Pop the 10 objects off the Queue and push them onto the Stack. Pop and print the 10 objects off the Stack. What is the order of the objects before and after adding them to the Queue and Stack? When and why did it change? Design Consider the following questions: - Using the Node class, will the links point from the front to the back of the Queue or from the back to the front? - How will you make sure there are no memory leaks? - Which Nodes in the Queue do you need to keep track of? Is there still a top Node pointer? - How will you print the objects in the main function? Should you implement an operator? Main function Create three Queue objects: one of integers, one of strings, and one of a custom data type (ideally the type you created in Project 1). Demonstrate that the Queue methods work correctly by calling methods on the objects and printing out to the console when appropriate. Perform the following operations: Create a Queue object and a Stack object, both of the type you created in Project 1. Print and push the first 10 objects from your vector (from Project 1) onto the Queue. Pop the 10 objects off the Queue and push them onto the Stack. Pop and print the 10 objects off the Stack. What is the order of the objects before and after adding them to the Queue and Stack? When and why did it change? Design Consider the following questions: - Using the Node class, will the links point from the front to the back of the Queue or from the back to the front? - How will you make sure there are no memory leaks? - Which Nodes in the Queue do you need to keep track of? Is there still a top Node pointer? - How will you print the objects in the main function? Should you implement an operator

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

More Books

Students also viewed these Databases questions