Programs based on
linked list
Browser History (Back and Forward Navigation)
• Problem:Implement browser history where:User can visit new pages.Go back to previous page.Go forward to next page.
class Page {
String url;
Page prev, next;
Page(String url) {
this.url = url;
}
}
class Browser {
Page current;
void visit(String url) {
Page newPage = new Page(url);
if (current != null) {
current.next = newPage;
newPage.prev = current;
}
void back() {
if (current != null && current.prev != null)
current = current.prev;
}
void forward() {
if (current != null && current.next != null)
current = current.next;
}
void printCurrent() {
System.out.println("Current page: " + (current != null ? current.url : "None"));
}
}
Call Center Queue (Customer Service Line)🔍 Problem:A call center handles calls one by one in the order received (FIFO). Implement this.
class Call {
String customerName;
Call next;
Call(String name) {
this.customerName = name;
}
}
class CallQueue {
Call front, rear;
void enqueue(String name) {
Call newCall = new Call(name);
if (rear == null) {
front = rear = newCall;
} else {
rear.next = newCall;
rear = newCall;
}
void dequeue() {
if (front == null) return;
front = front.next;
if (front == null) rear = null;
}
void showFront() {
System.out.println("Now serving: " + (front != null ?
front.customerName : "None"));
}
}
M u s ic P la y lis t
• Model a playlist where:You can go to next or previous song.You can add/remove songs dynamically. Doubly Linked List
class Song {
String name;
Song prev, next;
Song(String name) { this.name = name; }
}
class Playlist {
Song current;
void addSong(String name) {
Song song = new Song(name);
if (current == null) current = song;
else {
song.prev = current;
current.next = song;
current = song;
}
}
void nextSong() {
if (current != null && current.next != null)
current = current.next;
void prevSong() {
if (current != null && current.prev != null)
current = current.prev;
}
void play() {
System.out.println("Playing: " + (current != null ? current.name :
"None"));
}
}
Train Coach Management
• Each train coach is connected. You can add, remove, or rearrange
coaches
• Solution
• Each coach = Node in a Doubly Linked List.
• Example Actions
• Add a coach at the end
• Remove a coach by name
• Insert a coach before or after a specific coach
• Display coaches from front to end
• Display coaches from end to front
class Coach {
String name;
Coach prev, next;
Coach(String name) {
this.name = name;
}
}
class Train {
Coach head = null, tail = null;
// Add coach at the end
void addCoach(String name) {
Coach newCoach = new Coach(name);
if (head == null) {
head = tail = newCoach;
} else {
tail.next = newCoach;
newCoach.prev = tail;
tail = newCoach;
}
}
•
// Remove coach by name
void removeCoach(String name) {
Coach current = head;
while (current != null) {
if (current.name.equals(name)) {
if (current.prev != null)
current.prev.next = current.next;
else
head = current.next;
if (current.next != null)
current.next.prev = current.prev;
else
tail = current.prev;
System.out.println("Removed coach: " + name);
return;
}
current = current.next;
}
System.out.println("Coach " + name + " not found.");
}
// Insert a coach after a specific coach
void insertAfter(String afterCoach, String newCoachName) {
Coach current = head;
while (current != null) {
if (current.name.equals(afterCoach)) {
Coach newCoach = new Coach(newCoachName);newCoach.next = current.next;
newCoach.prev = current;
if (current.next != null)
current.next.prev = newCoach;
else
tail = newCoach;
current.next = newCoach;
System.out.println("Inserted coach " + newCoachName + " after " + afterCoach);
return;
}
current = current.next;
}
System.out.println("Coach " + afterCoach + " not found.");
}
// Print coaches from front to end
void printForward() {
System.out.print("Train (front to end): ");
Coach current = head;
while (current != null) {
System.out.print(current.name + " ");
current = current.next;
}
System.out.println();
}
// Print coaches from end to front
void printBackward() {
System.out.print("Train (end to front): ");
Coach current = tail;
while (current != null) {
System.out.print(current.name + " ");
current = current.prev;
}
System.out.println();
}
Merge Product Listings from Two
VendorsTwo
• vendors list their products as arrays of product objects.
• Each product has:
• Product ID
• Name
• Price
• We will:
• Convert each vendor's array into a linked list of products.
• Merge both product lists into a single sorted linked list (by price).
• Display the final merged product catalog.
class Product {
int id;
String name;
double price;
Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
}
class Node {
Product product;
Node next;
Node(Product product) {
this.product = product;
this.next = null;
}
public class MergeProductListings {
// Convert Product[] array to Linked List
public static Node arrayToLinkedList(Product[] products) {
if (products.length == 0) return null;
Node head = new Node(products[0]);
Node current = head;
for (int i = 1; i < products.length; i++) {
current.next = new Node(products[i]);
current = current.next;
}
return head;
}
// Merge two sorted product lists by price
public static Node mergeSortedProductLists(Node list1, Node list2) {
Node dummy = new Node(null);
Node tail = dummy;
while (list1 != null && list2 != null) {
if (list1.product.price < list2.product.price) {
tail.next = list1;
list1 = list1.next;
} else {
tail.next = list2;
list2 = list2.next; }
tail = tail.next; }
• tail.next = (list1 != null) ? list1 : list2;
• return dummy.next;
• }
• // Print the merged product catalog
• public static void printProductList(Node head) {
• System.out.println("Product ID | Name | Price");
• System.out.println("-------------------------------");
• while (head != null) {
• Product p = head.product;
• System.out.printf("%10d | %-10s | %.2f\n", p.id, p.name, p.price);
• head = head.next;
• }
• }
• // Main method
• public static void main(String[] args) {
• Product[] vendorA = {
• new Product(101, "Shoes", 2999.00),
• new Product(102, "T-Shirt", 999.00),
• new Product(103, "Jeans", 1999.00)
• };
• Product[] vendorB = {
• new Product(201, "Cap", 499.00),
• new Product(202, "Bag", 1499.00),
• new Product(203, "Watch", 2499.00)
• };
• // Sort arrays by price (if not sorted already)
• java.util.Arrays.sort(vendorA, (a, b) -> Double.compare(a.price, b.price));
• java.util.Arrays.sort(vendorB, (a, b) -> Double.compare(a.price, b.price));
• // Convert arrays to linked lists
• Node listA = arrayToLinkedList(vendorA);
• Node listB = arrayToLinkedList(vendorB);
• // Merge sorted linked lists
• Node mergedCatalog = mergeSortedProductLists(listA, listB);
• // Display final product catalog
• System.out.println("Merged E-commerce Product Catalog:");
• printProductList(mergedCatalog);