Answered step by step
Verified Expert Solution
Question
1 Approved Answer
class Train { int trainId; String arrivalTime; String departureTime; Train prev; Train next; public Train ( int trainId, String arrivalTime, String departureTime ) { this.trainId
class Train
int trainId;
String arrivalTime;
String departureTime;
Train prev;
Train next;
public Trainint trainId, String arrivalTime, String departureTime
this.trainId trainId;
this.arrivalTime arrivalTime;
this.departureTime departureTime;
this.prev null;
this.next null;
class TrainScheduleManager
Train head;
Train tail;
TrainScheduleManager
head null;
tail null;
void addTrainint trainId, String arrivalTime, String departureTime
Train newTrain new TraintrainId arrivalTime, departureTime;
if head null
head newTrain;
tail newTrain;
else if arrivalTimecompareToheadarrivalTime
newTrain.next head;
head.prev newTrain;
head newTrain;
else
Train current head;
while currentnext null && arrivalTime.compareTocurrentnext.arrivalTime
current current.next;
newTrain.next current.next;
if currentnext null
current.next.prev newTrain;
else
tail newTrain;
current.next newTrain;
newTrain.prev current;
void removeTrainint trainId
Train current head;
while current null
if currenttrainId trainId
if current head
head current.next;
if head null
head.prev null;
else if current tail
tail current.prev;
tail.next null;
else
current.prev.next current.next;
current.next.prev current.prev;
break;
current current.next;
void displaySchedule
Train current head;
while current null
System.out.printlnTrain ID: current.trainId Arrival: current.arrivalTime Departure: current.departureTime;
current current.next;
public class Main
public static void mainString args
TrainScheduleManager manager new TrainScheduleManager;
manager.addTrain::;
manager.addTrain::;
manager.addTrain::;
manager.addTrain::;
System.out.printlnInitial Train Schedule:";
manager.displaySchedule;
manager.removeTrain;
System.out.printlnUpdated Train Schedule after removing Train :;
manager.displaySchedule;
i need an Analysis of the Algorithm complexity used in this code
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