Question
PLEASE DO A STEP BY STEP DETAILED CODE TRACE HOW THIS GETS EXECUTED // Java program to print itinerary in order import java.util.HashMap; import java.util.Map;
PLEASE DO A STEP BY STEP DETAILED CODE TRACE HOW THIS GETS EXECUTED
// Java program to print itinerary in order
import java.util.HashMap;
import java.util.Map;
public class printItinerary
{
// Driver function
public static void main(String[] args)
{
Map
dataSet.put("Chennai", "Banglore");
dataSet.put("Bombay", "Delhi");
dataSet.put("Goa", "Chennai");
dataSet.put("Delhi", "Goa");
printResult(dataSet);
}
// This function populates 'result' for given input 'dataset'
private static void printResult(Map
{
// To store reverse of given map
Map
// To fill reverse map, iterate through the given map
for (Map.Entry
reverseMap.put(entry.getValue(), entry.getKey());
// Find the starting point of itinerary
String start = null;
for (Map.Entry
{
if (!reverseMap.containsKey(entry.getKey()))
{
start = entry.getKey();
break;
}
}
// If we could not find a starting point, then something wrong
// with input
if (start == null)
{
System.out.println("Invalid Input");
return;
}
// Once we have starting point, we simple need to go next, next
// of next using given hash map
String to = dataSet.get(start);
while (to != null)
{
System.out.print(start + "->" + to + ", ");
start = to;
to = dataSet.get(to);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started