Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Program: Below are the instructions, followed by all necessary code/data. Stack.h: #ifndef NODEB_STACK_H #define NODEB_STACK_H #include Node.h #include using namespace std; template typename Object>

C++ Program: Below are the instructions, followed by all necessary code/data.

image text in transcribed

Stack.h:

#ifndef NODEB_STACK_H #define NODEB_STACK_H #include "Node.h" #include  using namespace std; templatetypename Object> class Stack { private: // Store the address of the top Node in the Stack  Node* top; public: // Constructor  Stack() { // Stack is empty. Top doesn't point at anything.  top = nullptr; } // Deconstructor / Destructor  ~Stack() { // Pop all the Nodes left in the Stack  while (!isEmpty()) { pop(); } } bool isEmpty() const { return (top == nullptr); } void push(Object item) { if (isEmpty()) { // This is the first Node in the Stack  // Allocate memory in the heap to store the new Node  Node* newNode = new Node(item); // Update top  top = newNode; } else { // There is already at least one Node in the Stack  Node* newNode = new Node(item, top); // Update top  top = newNode; } } Object pop() { if (isEmpty()) { // There is no Node to pop. Return the default Object.  // Note: This assumes the Object has a default constructor.  return Object(); } else { // There is at least one Node in the Stack.  // Store a copy of the Object to be returned  Object copy = top->getItem(); // Store a copy of top  Node* topCopy = top; // Update top to point at whatever the Node we are about to delete is pointing at  top = top->getNext(); // Delete the Node from heap memory  // Use the delete keyword with a pointer to the memory you want to deallocate from the heap  delete topCopy; // Return the Object  return copy; } } bool exists(Object item) const { Node* curr = top; while (curr != nullptr) { // Note: This assumes the Object can use the == operator  if (curr->getItem() == item) { // Found the item in the Stack  return true; } curr = curr->getNext(); } // Did not find the item in the Stack  return false; } void printStack() const { Node* curr = top; while (curr != nullptr) { // Note: This assumes that the Object can use the  cout getItem() getNext(); } } }; #endif //NODEB_STACK_H 

Node.h:

#ifndef NODEB_NODE_H #define NODEB_NODE_H  templatetypename Object> class Node { private: Object item; // Create a pointer to the next Node  Node* next; public: /** Create new node with specified data  Have it reference null  @param newItem is data to added to new node  */  Node(Object newItem) { item = newItem; // explicitly state that the pointer isn't pointing to anything  next = nullptr; } /** Create new node with specified data  Have it reference nextNode  @param newItem is data to added to new node  @param nextNode is reference that new Node's next will hold   */  Node(Object newItem, Node* nextNode) { item = newItem; next = nextNode; } /** set data field (item) of Node  @param newItem, to set item field to  */  void setItem(Object newItem) { item = newItem; } // end setItem   /** get data field (item) of Node  @return item field  */  Object getItem() const { return item; } /** set next field of Node  @param nextNode, to set next field to  */  void setNext(Node* nextNode) { next = nextNode; } /** get next field Node  @return next field  */  Node* getNext() const { return next; } }; #endif //NODEB_NODE_H 

Now, the following code is what I've personally written. 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:

#ifndef COMBINE_H_ #define 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: //CONSTRUCTORS  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); } //GETTERS  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; } //SETTERS  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 //Default value  this->weight = 0; } else  { this->weight = weight; } } void setDash(float dash) { if (dash //Default value  this->dash = 0; } else  { this->dash = dash; } } void setBench(float bench) { if (bench //Default value  this->bench = 0; } else  { this->bench = bench; } } // Overloaded operators  friend ostream& operatorconst Combine &com) { outs return outs; } }; /* getPlayersFromFile reads the file containing the dataset in and assigns each entry its attributes.  * The function is similar to what we looked at in class; however, I made one major change which was including stringstream  * simply for better handling of values  */ void getPlayersFromFile(string filename, vector &players) { ifstream inFile; // Use ../ to get out of the cmake-build-debug folder to find the file  inFile.open("../" + filename); // inFile.open(filename);  string name = "", college = "", pos = "", header = ""; string temp; int height = 0, weight = 0; float dash = 0, bench = 0; char comma = ','; // check that the file is in a good state  if (inFile) { // read in the header line  getline(inFile, header); // loop through all the data in the file  while (inFile && inFile.peek() != EOF) { // read line  getline(inFile,temp); // pass it to string stream  stringstream ss(temp); // get name  getline(ss,name,','); // get college  getline(ss,college,','); // get POS  getline(ss,pos,','); // get height  getline(ss,temp,','); height = atoi(temp.c_str()); // get weight  getline(ss,temp,','); weight = atoi(temp.c_str()); if(ss.good()) { // get 40 yard  getline(ss,temp,','); dash = atof(temp.c_str()); } else{ dash=0; } if(ss.good()) { // get bench press  getline(ss,temp,','); bench = atof(temp.c_str()); } else{ bench=0; } // create a Combine object and put it on the back of the vector  players.push_back(Combine(name, college, pos, height, weight, dash, bench)); } } else  { cerr "File not found" /*  * avgWeight simply accepts the vector of the Combine class from main and iterates through each object, adding the each entry's (player's) weight to the get the sum before  * dividing by the size of the vector to calculate the average.  */ float avgWeight(vector &players) { float total = 0; getPlayersFromFile("CombinePlayers.txt", players); for (int i = 0; i //cout  } 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 //This displays the dataset  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
For this project, you will write a C++ program that uses Stack and Queue data structures to store objects of varying data types. Requirements It is recommended that you use the data and program from Project 1 as a starting point. Stack Class Start with the Stack class from lecture. What is the Big-Oh complexity of the methods? Queue Class Create a Queue class that uses the Node class from lecture to create a functioning queue data structure. Your Queue should be able to push and pop Objects and determine if an Object is in the Queue. What is the complexity of each method? Your Queue must be able to be used with any data type. . Your Nodes must be stored in heap memory. Your program must not have any memory leaks. 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? For this project, you will write a C++ program that uses Stack and Queue data structures to store objects of varying data types. Requirements It is recommended that you use the data and program from Project 1 as a starting point. Stack Class Start with the Stack class from lecture. What is the Big-Oh complexity of the methods? Queue Class Create a Queue class that uses the Node class from lecture to create a functioning queue data structure. Your Queue should be able to push and pop Objects and determine if an Object is in the Queue. What is the complexity of each method? Your Queue must be able to be used with any data type. . Your Nodes must be stored in heap memory. Your program must not have any memory leaks. 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

Question

Could you show me how to get the correct answer of 43,888?

Answered: 1 week ago