Answered step by step
Verified Expert Solution
Link Copied!

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 Train(int 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 addTrain(int trainId, String arrivalTime, String departureTime){
Train newTrain = new Train(trainId, arrivalTime, departureTime);
if (head == null){
head = newTrain;
tail = newTrain;
} else if (arrivalTime.compareTo(head.arrivalTime)<0){
newTrain.next = head;
head.prev = newTrain;
head = newTrain;
} else {
Train current = head;
while (current.next != null && arrivalTime.compareTo(current.next.arrivalTime)>=0){
current = current.next;
}
newTrain.next = current.next;
if (current.next != null){
current.next.prev = newTrain;
} else {
tail = newTrain;
}
current.next = newTrain;
newTrain.prev = current;
}
}
void removeTrain(int trainId){
Train current = head;
while (current != null){
if (current.trainId == 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.println("Train ID: "+ current.trainId +"| Arrival: "+ current.arrivalTime +"| Departure: "+ current.departureTime);
current = current.next;
}
}
}
public class Main {
public static void main(String[] args){
TrainScheduleManager manager = new TrainScheduleManager();
manager.addTrain(1,"08:00","08:30");
manager.addTrain(2,"09:00","09:30");
manager.addTrain(3,"09:30","10:00");
manager.addTrain(4,"10:00","10:30");
System.out.println("Initial Train Schedule:");
manager.displaySchedule();
manager.removeTrain(2);
System.out.println("Updated Train Schedule after removing Train 2:");
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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems 6th Asian Conference Aciids 2014 Bangkok Thailand April 7 9 2014 Proceedings Part I 9 2014 Proceedings Part 1 Lnai 8397

Authors: Ngoc-Thanh Nguyen ,Boonwat Attachoo ,Bogdan Trawinski ,Kulwadee Somboonviwat

2014th Edition

3319054759, 978-3319054759

More Books

Students also viewed these Databases questions

Question

4. Devise an interview strategy from the interviewers point of view

Answered: 1 week ago