1.Real-world situation:
A train coach means an individual compartment/bogie of a train where passengers sit.
Example:
Sleeper coach (S1, S2…)
AC coach (A1, B1…)
General coach
Each of these is a separate unit, but they are connected together to form a full train.
Real-world mapping
Add a new coach in middle → no shifting needed
Remove a damaged coach → just reconnect neighbors
Program:
import java.util.LinkedList;
public class TrainExample {
public static void main(String[] args) {
LinkedList<String> train = new LinkedList<>();
train.add("S1");
train.add("S2");
train.add("S3");
System.out.println(train);
train.add(1, "A1");
System.out.println(train);
train.remove("S2");
System.out.println(train);
}
}
output:
[S1, S2, S3]
[S1, A1, S2, S3]
[S1, A1, S3]
2.Real-world situation:
Imagine a bank or support center:
Customers are waiting in a queue.
Normally → First Come First Serve
But sometimes:
VIP customer arrives
Emergency case comes
Senior citizen given priority
So the order of customers changes dynamically
Why this fits LinkedList :
In this scenario:
Add customer at end → normal case
Insert customer in middle → priority case
Remove customer → after service
Reorder queue → based on priority
These operations happen frequently
Program:
package linkedlist;
import java.util.LinkedList;
public class scenario2 {
public static void main(String[] args) {
LinkedList<String> queue = new LinkedList<>();
queue.add("Customer1");
queue.add("Customer2");
queue.add("Customer3");
System.out.println(queue);
queue.addFirst("VIP Customer");
System.out.println(queue);
queue.removeFirst();
System.out.println(queue);
}
}
output:
[Customer1, Customer2, Customer3]
[VIP Customer, Customer1, Customer2, Customer3]
[Customer1, Customer2, Customer3]
3. Real-world situation:
In apps like Spotify / YouTube Music:
You are playing songs in a queue
Normally songs play in order
But user actions can change it anytime:
Add song to end
Remove a song
“Play next” → insert song immediately after current song
Why this fits LinkedList :
Add song at end → normal queue
Insert song in middle → “Play Next”
Remove song → user deletes from queue
Reorder songs → drag & drop
These are frequent insert/delete operations
Program:
package linkedlist;
import java.util.LinkedList;
public class scenario3 {
public static void main(String[] args) {
LinkedList<String> playlist = new LinkedList<>();
playlist.add("Song1");
playlist.add("Song2");
playlist.add("Song3");
System.out.println(playlist);
playlist.add(1, "Play next");
System.out.println(playlist);
playlist.remove("Song2");
System.out.println(playlist);
}
}
output:
[Song1, Song2, Song3]
[Song1, Play next, Song2, Song3]
[Song1, Play next, Song3]
Top comments (0)