Untitled
unknown
plain_text
2 years ago
2.2 kB
10
Indexable
import java.util.*;
public class DoublyLinkedList {
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList <Integer> dbllist = new LinkedList <Integer>();
int i=1, choice, element, position;
Scanner sc = new Scanner(System.in);
System.out.println("1.Enter an element at the beginning");
System.out.println("2.Enter an element at the end");
System.out.println("3.Enter an element at the specific position");
System.out.println("4.Delete an element");
System.out.println("5.Display the elements");
System.out.println("6.Exit");
do {
System.out.println("Enter your choice:");
choice = sc.nextInt();
switch(choice) {
case 1:
System.out.print("Enter the element to be inserted:");
element= sc.nextInt();
dbllist.addFirst(element);
System.out.println("Successfullyinserted");
break;
case 2:
System.out.print("Enter the element to be inserted:");
element = sc.nextInt();
dbllist.addLast(element);
break;
case 3:
System.out.print("Enter the element to be inserted:");
element = sc.nextInt();
System.out.println("Enter the position ");
position = sc.nextInt();
dbllist.add(position, element);
break;
case 4:
System.out.println("Enter the to be deleted");
Integer rm_ele;
rm_ele = sc.nextInt();
if(dbllist.contains(rm_ele)) {
dbllist.remove(rm_ele);
System.out.println("Remaining elements are:");
Iterator itr = dbllist.iterator();
while(itr.hasNext()) {
System.out.print(itr.next()+"<--->");
}
System.out.println("NULL");
}
else {
System.out.println("Elementt not found");
}
break;
case 5:
System.out.println("The elements of the doubly linked list are:");
Iterator itr = dbllist.iterator();
while(itr.hasNext()) {
System.out.print(itr.next()+"<--->");
}
System.out.println("NULL");
break;
case 6:
System.out.println("Program terminated");
i=0;
break;
default : System.out.println("Enter valid option");
}
}while(i==1);
}
}
Editor is loading...
Leave a Comment