Untitled
unknown
plain_text
2 years ago
2.5 kB
10
Indexable
import java.util.Scanner;
public class bazil {
Scanner input = new Scanner(System.in);
class Node{
int data;
Node rlink;
Node llink;
Node(int data){
this.data = data;
}
}
public Node head = null;
public Node tail = null;
//INSERTION OF NODES.
public void addNode(int item){
Node newNode = new Node(item);
if(head==null){
head = tail = newNode;
head.llink = null;
tail.rlink = null;
}else{
tail.rlink = newNode;
newNode.llink = tail;
tail = newNode;
tail.rlink = null;
}
}
//DELETION OF NODES.
public void delete(int key){
Node ptr = head;
if(ptr == null){
System.out.println("Empty Set.");
return;
}
while(ptr != null && ptr.data != key){
ptr = ptr.rlink;
}
if (ptr == null) {
System.out.println("Key not found in the list.");
return;
}
if(ptr == head){
head = head.rlink;
return;
}
if(ptr == tail){
tail.llink.rlink = null;
return;
}
ptr.llink.rlink = ptr.rlink;
ptr.rlink.llink = ptr.llink;
}
public void display(){
Node temp = head;
if(temp == null){
System.out.println("No Elements in the Set.");
}
while(temp != null){
System.out.println(temp.data);
temp = temp.rlink;
}
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
bazil obj = new bazil();
int data;
int key;
int ch = 0;
while(ch != 6){
System.out.println("1.Inserion \n2.Deletion \n3.Display \n4.Exit Operaton \nEnter your choice below: ");
ch = input.nextInt();
if(ch == 1){
System.out.println("Enter the data: ");
data = input.nextInt();
obj.addNode(data);
}else if(ch == 2){
System.out.println("Enter the element to delete: ");
data = input.nextInt();
obj.delete(data);
}else if(ch == 3){
obj.display();
}else if(ch == 4){
System.exit(0);
}
else{
System.out.println("Invalid Choice.");
}
}
}
}Editor is loading...
Leave a Comment