Question

In: Computer Science

8.16 LAB: Mileage tracker for a runner Given the MileageTrackerNode class, complete main() to insert nodes...

8.16 LAB: Mileage tracker for a runner

Given the MileageTrackerNode class, complete main() to insert nodes into a linked list (using the InsertAfter() function). The first user-input value is the number of nodes in the linked list. Use the PrintNodeData() function to print the entire linked list. DO NOT print the dummy head node.

Ex. If the input is:

3
2.2
7/2/18
3.2
7/7/18
4.5
7/16/18

the output is:

2.2, 7/2/18
3.2, 7/7/18
4.5, 7/16/18

_____________________________

The given code that i need to use is:

______________________________

Main.cpp

#include "MileageTrackerNode.h"
#include <string>
#include <iostream>
using namespace std;

int main (int argc, char* argv[]) {
// References for MileageTrackerNode objects
MileageTrackerNode* headNode;
MileageTrackerNode* currNode;
MileageTrackerNode* lastNode;

double miles;
string date;
int i;

// Front of nodes list
headNode = new MileageTrackerNode();
lastNode = headNode;

// TODO: Read in the number of nodes

// TODO: For the read in number of nodes, read
// in data and insert into the linked list

// TODO: Call the PrintNodeData() method
// to print the entire linked list

// MileageTrackerNode Destructor deletes all
// following nodes
delete headNode;
}

___________________________________________________

MileageTrackerNode.h

#ifndef MILEAGETRACKERNODEH
#define MILEAGETRACKERNODEH

#include <string>
using namespace std;

class MileageTrackerNode {
public:
// Constructor
MileageTrackerNode();

// Destructor
~MileageTrackerNode();

// Constructor
MileageTrackerNode(double milesInit, string dateInit);

// Constructor
MileageTrackerNode(double milesInit, string dateInit, MileageTrackerNode* nextLoc);

/* Insert node after this node.
Before: this -- next
After: this -- node -- next
*/
void InsertAfter(MileageTrackerNode* nodeLoc);

// Get location pointed by nextNodeRef
MileageTrackerNode* GetNext();

void PrintNodeData();

private:
double miles; // Node data
string date; // Node data
MileageTrackerNode* nextNodeRef; // Reference to the next node
};

#endif

______________________________________________

MileageTrackerNode.cpp

#include "MileageTrackerNode.h"
#include <iostream>

// Constructor
MileageTrackerNode::MileageTrackerNode() {
miles = 0.0;
date = "";
nextNodeRef = nullptr;
}

// Destructor
MileageTrackerNode::~MileageTrackerNode() {
if(nextNodeRef != nullptr) {
delete nextNodeRef;
}
}

// Constructor
MileageTrackerNode::MileageTrackerNode(double milesInit, string dateInit) {
miles = milesInit;
date = dateInit;
nextNodeRef = nullptr;
}

// Constructor
MileageTrackerNode::MileageTrackerNode(double milesInit, string dateInit, MileageTrackerNode* nextLoc) {
miles = milesInit;
date = dateInit;
nextNodeRef = nextLoc;
}

/* Insert node after this node.
Before: this -- next
After: this -- node -- next
*/
void MileageTrackerNode::InsertAfter(MileageTrackerNode* nodeLoc) {
MileageTrackerNode* tmpNext;

tmpNext = nextNodeRef;
nextNodeRef = nodeLoc;
nodeLoc->nextNodeRef = tmpNext;
}

// Get location pointed by nextNodeRef
MileageTrackerNode* MileageTrackerNode::GetNext() {
return nextNodeRef;
}

void MileageTrackerNode::PrintNodeData(){
cout << miles << ", " << date << endl;
}

Solutions

Expert Solution

Answer:

Code:

// MileageTrackerNode.h

#ifndef MILEAGETRACKERNODEH
#define MILEAGETRACKERNODEH

#include <string>
using namespace std;

class MileageTrackerNode {
public:
MileageTrackerNode();
~MileageTrackerNode();
MileageTrackerNode(double milesInit, string dateInit);
MileageTrackerNode(double milesInit, string dateInit, MileageTrackerNode* nextLoc);

/* Insert node after this node.
Before: this -- next
After: this -- node -- next
*/
void InsertAfter(MileageTrackerNode* nodeLoc);
MileageTrackerNode* GetNext();
void PrintNodeData();

private:
double miles;

string date; // Node data
MileageTrackerNode* nextNodeRef; // Reference to the next node
};

#endif

//end of MileageTrackerNode.h

// MileageTrackerNode.cpp

#include "MileageTrackerNode.h"
#include <iostream>

MileageTrackerNode::MileageTrackerNode() {
miles = 0.0;
date = "";
nextNodeRef = nullptr;
}


MileageTrackerNode::~MileageTrackerNode() {
if(nextNodeRef != nullptr) {
delete nextNodeRef;
}
}

MileageTrackerNode::MileageTrackerNode(double milesInit, string dateInit) {
miles = milesInit;
date = dateInit;
nextNodeRef = nullptr;
}

MileageTrackerNode::MileageTrackerNode(double milesInit, string dateInit, MileageTrackerNode* nextLoc) {
miles = milesInit;
date = dateInit;
nextNodeRef = nextLoc;
}

/* Insert node after this node.
Before: this -- next
After: this -- node -- next
*/
void MileageTrackerNode::InsertAfter(MileageTrackerNode* nodeLoc) {
MileageTrackerNode* tmpNext;

tmpNext = nextNodeRef;
nextNodeRef = nodeLoc;
nodeLoc->nextNodeRef = tmpNext;
}

// Get location pointed by nextNodeRef
MileageTrackerNode* MileageTrackerNode::GetNext() {
return nextNodeRef;
}

void MileageTrackerNode::PrintNodeData(){
cout << miles << ", " << date << endl;
}

//end of MileageTrackerNode.cpp

// Main.cpp

#include "MileageTrackerNode.h"
#include <string>
#include <iostream>

using namespace std;
int main (int argc, char* argv[]) {

// References for MileageTrackerNode objects
MileageTrackerNode* headNode;
MileageTrackerNode* currNode;
MileageTrackerNode* lastNode;

double miles;
string date;
int i;

// Front of nodes list
headNode = new MileageTrackerNode(); // create a dummy head node
lastNode = headNode;

// Read in the number of nodes
cin>>i;

// loop i times
while(i>0)
{
// read in data and insert into the linked list
cin>>miles>>date;
// create a new node with the read data
currNode = new MileageTrackerNode(miles, date);
// insert currNode after lastNode
lastNode->InsertAfter(currNode);
lastNode = currNode; // update lastNode to currNode
i--; // decrement i
}

cout<<endl<<"List:"<<endl; // can remove this line, used for the separation between input and output
// Call the PrintNodeData() method
// to print the entire linked list
currNode = headNode->GetNext(); // set currNode to the first node i.e node after dummy head node
// loop till the end of list
while(currNode != nullptr)
{
currNode->PrintNodeData(); // display the data of currNode
currNode = currNode->GetNext(); // move to next node
}

return 0;
}

Output:

Note: If you have any doubtsor queries please comment I will get back to you.


Related Solutions

7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the FoodItem class (in files FoodItem.h and FoodItem.cpp)...
7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the FoodItem class (in files FoodItem.h and FoodItem.cpp) with constructors to initialize each food item. The default constructor should initialize the name to "None" and all other fields to 0.0. The second constructor should have four parameters (food name, grams of fat, grams of carbohydrates, and grams of protein) and should assign each private field with the appropriate parameter value. Ex: If the input is: M&M's 10.0 34.0 2.0 1.0 where M&M's...
7.27 LAB: Artwork label (classes/constructors) Given main(), complete the Artist class (in files Artist.h and Artist.cpp)...
7.27 LAB: Artwork label (classes/constructors) Given main(), complete the Artist class (in files Artist.h and Artist.cpp) with constructors to initialize an artist's information, get member functions, and a PrintInfo() member function. The default constructor should initialize the artist's name to "None" and the years of birth and death to 0. PrintInfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Complete the Artwork class (in files Artwork.h and Artwork.cpp) with constructors...
7.28 LAB: Artwork label (classes/constructors). Written in C++ Given main(), complete the Artist class (in files...
7.28 LAB: Artwork label (classes/constructors). Written in C++ Given main(), complete the Artist class (in files Artist.h and Artist.cpp) with constructors to initialize an artist's information, get member functions, and a PrintInfo() member function. The default constructor should initialize the artist's name to "None" and the years of birth and death to 0. PrintInfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Complete the Artwork class (in files Artwork.h and...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...
Given main(), complete the Car class (in files Car.h and Car.cpp) with member functions to set...
Given main(), complete the Car class (in files Car.h and Car.cpp) with member functions to set and get the purchase price of a car (SetPurchasePrice(), GetPurchasePrice()), and to output the car's information (PrintInfo()). Ex: If the input is: 2011 18000 2018 where 2011 is the car's model year, 18000 is the purchase price, and 2018 is the current year, the output is: Car's information: Model year: 2011 Purchase price: 18000 Current value: 5770 Note: printInfo() should use three spaces for...
9.7 LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class,...
9.7 LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 Sortedlist.java import java.util.Scanner; public class SortedList { public static void main (String[] args)...
10.5 LAB: Air-traffic control (queue using a linked list) Given a partial main() and PlaneQueue class,...
10.5 LAB: Air-traffic control (queue using a linked list) Given a partial main() and PlaneQueue class, write the push() and pop() methods for PlaneQueue. Then complete the main() to read in whether flights are arriving or have landed at an airport. An "arriving" flight is pushed onto the queue. A "landed" flight is popped from the front of the queue. Output the queue after each plane is pushed or popped. Entering -1 exits the program. Ex: If the input is:...
6.23 LAB: Python insert/update sqlite3 datafiles Given is a Python program that connects to a sqlite...
6.23 LAB: Python insert/update sqlite3 datafiles Given is a Python program that connects to a sqlite database and has one table called writers with two columnns: name - the name of a writer num - the number of works the writer has written The writers table originally has the following data: name, num Jane Austen,6 Charles Dickens,20 Ernest Hemingway,9 Jack Kerouac,22 F. Scott Fitzgerald,8 Mary Shelley,7 Charlotte Bronte,5 Mark Twain,11 Agatha Christie,73 Ian Flemming,14 J.K. Rowling,14 Stephen King,54 Oscar Wilde,1...
6.23 LAB: Python insert/update sqlite3 datafiles Given is a Python program that connects to a sqlite...
6.23 LAB: Python insert/update sqlite3 datafiles Given is a Python program that connects to a sqlite database and has one table called writers with two columnns: name - the name of a writer num - the number of works the writer has written The writers table originally has the following data: name, num Jane Austen,6 Charles Dickens,20 Ernest Hemingway,9 Jack Kerouac,22 F. Scott Fitzgerald,8 Mary Shelley,7 Charlotte Bronte,5 Mark Twain,11 Agatha Christie,73 Ian Flemming,14 J.K. Rowling,14 Stephen King,54 Oscar Wilde,1...
public class Runner{ public static void main(String[] args){           MylinkedList ll = new MylinkedList(10.1);...
public class Runner{ public static void main(String[] args){           MylinkedList ll = new MylinkedList(10.1);        ll.append(15.6);        ll.append(10.5);        ll.append(8.11);        ll.print();               ll.initiateIterator();        Object o = null;        while ( (o=ll.nextObject())!=null){            System.out.println((Double)(o)+100.1);        } Your solution here    // Iterate, find, and report the largest number               MylinkedList lb = new MylinkedList();        lb.append( new Rectangle(10.1, 20.2) );   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT