//important java util,lang,io
import java.util.*;
import java.lang.*;
import java.io.*;
// linked list simple code
//pl write a program in Java for a single linked list of simple code to display prev, current and next
//assuming that I have 3 nodes h1,h2,h3 and value is for h1 is 1, for h2 is 2 and h3 is 3
class Node {
int data;
Node prev;
Node next;
public Node(int data) {
// this.ministry = ministry this is contructor
this.data = data;
}
}
public class Main {
public static void main(String[] args) {
// Create the nodes
Node h1 = new Node(1);
Node h2 = new Node(2);
Node h3 = new Node(3);
// Link the nodes manually (assuming h1 is the head)
h1.next = h2;
h2.prev = h1;
h2.next = h3;
h3.prev = h2;
// Traverse the list and print previous, current, and next values
Node current = h1;
while (current != null) {
System.out.println("Previous: " + (current.prev != null ? current.prev.data : "None"));
System.out.println("Current: " + current.data);
System.out.println("Next: " + (current.next != null ? current.next.data : "None"));
System.out.println();
current = current.next;
}
}
}
/*
Output
Previous: None
Current: 1
Next: 2
Previous: 1
Current: 2
Next: 3
Previous: 2
Current: 3
Next: None
*/
To embed this project on your website, copy the following code and paste it into your website's HTML: