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 a java code to solve the same problem but using different data structure than doubly linked list which is used in this example

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

Step: 3

blur-text-image

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

The Database Relational Model A Retrospective Review And Analysis

Authors: C. J. Date

1st Edition

0201612941, 978-0201612943

More Books

Students also viewed these Databases questions