Menu

[Solved]Given Mileagetrackernode Class Complete Main Insert Nodes Linked List Using Insertafter Me Q37147179

Given MileageTrackerNode class, complete main() to insert nodesinto the linked list (using the insertAfter() method). The firstuser-input value is the number of nodes in the linked list. Use theprintNodeData() method to print the entire linked list. DONOT print the dummy head node.

Ex. If the input is

32.27/2/183.27/7/184.57/16/18

the output is

2.2, 7/2/184.5, 7/16/183.2, 7/7/18

import java.util.Scanner;

public class MileageTrackerLinkedList {
public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);

// 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: Scan the number of nodes

// TODO: for the scanned number of nodes, scan
// in data and insert into the linked list
  

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

THE BELOW CLASS IS READ ONLY SO NO CODE CAN BE ADDED TOTHIS PART OF THE QUESTION!!!

public class MileageTrackerNode {
private double miles; // Node data   
private String date; // Node data
private MileageTrackerNode nextNodeRef; // Reference to the nextnode

public MileageTrackerNode() {
miles = 0.0;
date = “”;
nextNodeRef = null;
}

// Constructor   
public MileageTrackerNode(double milesInit, String dateInit){
this.miles = milesInit;
this.date = dateInit;
this.nextNodeRef = null;
}

// Constructor   
public MileageTrackerNode(double milesInit, String dateInit,MileageTrackerNode nextLoc) {
this.miles = milesInit;
this.date = dateInit;
this.nextNodeRef = nextLoc;
}

/* Insert node after this node.
Before: this — next   
After: this — node — next   
*/
public void insertAfter(MileageTrackerNode nodeLoc) {
MileageTrackerNode tmpNext;

tmpNext = this.nextNodeRef;
this.nextNodeRef = nodeLoc;
nodeLoc.nextNodeRef = tmpNext;
}

// Get location pointed by nextNodeRef   
public MileageTrackerNode getNext() {
return this.nextNodeRef;
}

public void printNodeData() {
System.out.println(+ this.miles + “, ” + this.date);
}
}

}
}

Expert Answer


Answer to Given MileageTrackerNode class, complete main() to insert nodes into the linked list (using the insertAfter() method). T… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *