8.14 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   Main.cpp #include "MileageTrackerNode.h" #include #include 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;

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter12: Exception Handling
Section: Chapter Questions
Problem 7PE
icon
Related questions
Question

8.14 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

 

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

File is marked as read only
Current file: MileageTrackerNode.h -
1 #ifndef MILEAGETRACKERNODEH
2 #define MILEAGETRACKERNODEH
3
4 #include <string>
5 using namespace std;
6.
7 class MileageTrackerNode {
public:
// Constructor
MileageTrackerNode();
8.
9.
10
11
// Destructor
-MileageTrackerNode();
12
13
14
// Constructor
MileageTrackerNode(double milesInit, string dateInit);
15
16
17
// Constructor
MileageTrackerNode(double milesInit, string dateInit, MileageTrackerNode* nextLoc);
18
19
20
21
/* Insert node after this node.
22
Before: this
next
23
After:
this
node
next
--
24
*/
25
void InsertAfter(MileageTrackerNode* nodeLoc);
26
// Get location pointed by nextNodeRef
MileageTrackerNode* GetNext();
27
28
29
30
void PrintNodeData();
31
private:
double miles;
string date;
MileageTrackerNode* nextNodeRef; // Reference to the next node
32
// Node data
// Node data
33
34
35
36 };
37
Transcribed Image Text:File is marked as read only Current file: MileageTrackerNode.h - 1 #ifndef MILEAGETRACKERNODEH 2 #define MILEAGETRACKERNODEH 3 4 #include <string> 5 using namespace std; 6. 7 class MileageTrackerNode { public: // Constructor MileageTrackerNode(); 8. 9. 10 11 // Destructor -MileageTrackerNode(); 12 13 14 // Constructor MileageTrackerNode(double milesInit, string dateInit); 15 16 17 // Constructor MileageTrackerNode(double milesInit, string dateInit, MileageTrackerNode* nextLoc); 18 19 20 21 /* Insert node after this node. 22 Before: this next 23 After: this node next -- 24 */ 25 void InsertAfter(MileageTrackerNode* nodeLoc); 26 // Get location pointed by nextNodeRef MileageTrackerNode* GetNext(); 27 28 29 30 void PrintNodeData(); 31 private: double miles; string date; MileageTrackerNode* nextNodeRef; // Reference to the next node 32 // Node data // Node data 33 34 35 36 }; 37
File is marked as read only
Current file: MileageTrackerNode.cpp
1 #include "MileageTrackerNode.h"
2 #include <iostream>
3
4 // Constructor
5 MileageTrackerNode::MileageTrackerNode() {
6.
miles = 0.0;
date = "".
nextNodeRef =
7
8
nullptr;
9 }
10
11 // Destructor
12 MileageTrackerNode::-MileageTrackerNode() {
if(nextNodeRef != nullptr) {
delete nextNodeRef;
13
14
}
16 }
15
17
18 // Constructor
19 MileageTrackerNode::MileageTrackerNode(double milesInit, string dateInit) {
miles = milesInit;
date = dateInit;
nextNodeRef = nullptr;
20
21
22
23 }
24
25 // Constructor
26 MileageTrackerNode::MileageTrackerNode(double milesInit, string dateInit, MileageTrackerNode* nextLoc) {
miles = milesInit;
date = dateInit;
nextNodeRef = nextLoc;
27
28
29
30 }
31
32 /* Insert node after this node.
33 Before: this
this --
next
34 After:
node
next
--
35 */
36 void MileageTrackerNode::InsertAfter(MileageTrackerNode* nodeloc) {
37
MileageTrackerNode* tmpNext;
38
tmpNext = nextNodeRef;
nextNodeRef =
39
40
nodeloc;
41
nodeLoc->nextNodeRef = tmpNext;
42 }
43
44 // Get location pointed by nextNodeRef
45 MileageTrackerNode* MileageTrackerNode::GetNext() {
46
return nextNodeRef;
47 }
48
49 void MileageTrackerNode::PrintNodeData(){
<« date << endl;
50
cout << miles << "
51 }
Transcribed Image Text:File is marked as read only Current file: MileageTrackerNode.cpp 1 #include "MileageTrackerNode.h" 2 #include <iostream> 3 4 // Constructor 5 MileageTrackerNode::MileageTrackerNode() { 6. miles = 0.0; date = "". nextNodeRef = 7 8 nullptr; 9 } 10 11 // Destructor 12 MileageTrackerNode::-MileageTrackerNode() { if(nextNodeRef != nullptr) { delete nextNodeRef; 13 14 } 16 } 15 17 18 // Constructor 19 MileageTrackerNode::MileageTrackerNode(double milesInit, string dateInit) { miles = milesInit; date = dateInit; nextNodeRef = nullptr; 20 21 22 23 } 24 25 // Constructor 26 MileageTrackerNode::MileageTrackerNode(double milesInit, string dateInit, MileageTrackerNode* nextLoc) { miles = milesInit; date = dateInit; nextNodeRef = nextLoc; 27 28 29 30 } 31 32 /* Insert node after this node. 33 Before: this this -- next 34 After: node next -- 35 */ 36 void MileageTrackerNode::InsertAfter(MileageTrackerNode* nodeloc) { 37 MileageTrackerNode* tmpNext; 38 tmpNext = nextNodeRef; nextNodeRef = 39 40 nodeloc; 41 nodeLoc->nextNodeRef = tmpNext; 42 } 43 44 // Get location pointed by nextNodeRef 45 MileageTrackerNode* MileageTrackerNode::GetNext() { 46 return nextNodeRef; 47 } 48 49 void MileageTrackerNode::PrintNodeData(){ <« date << endl; 50 cout << miles << " 51 }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Parallel Processing
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT