import java.util.*;
class Elem2
{
public Elem2 next;
public Elem2 prev;
public Object data;
};
class SimpleDoublyList
{
static Elem2 front = null;
static Elem2 back = null;
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
SimpleDoublyList ll=new SimpleDoublyList();
while (true)
{
System.out.println("Please enter your choice\n 1 for insertion \n 2 for display \n 3 for delete\n 4 for exit");
int a=sc.nextInt();
switch (a)
{
case 1:
System.out.println("Please enter data");
String s=sc.next();
ll.insert(s);
break;
case 2:
ll.display();
break;
case 3:
System.out.println("Enter the element that you want to delete");
String el=sc.next();
ll.delete(el);
case 4:
ll.exit();
break;
default:
System.out.println("Please enter correct choice");
}
}
}
public static void insert(Object o)
{
Elem2 e=new Elem2();
e.data=o;
if (front==null)
{
front = e;
}
else
{
back.next=e;
}
e.prev=back;
back=e;
}
public static void display()
{
for (Elem2 e=front;e!=null ;e=e.next )
{
System.out.println(e.data);
}
}
private static void exit()
{
System.exit(0);
}
private static void delete(Object o)
{
Elem2 temp=front;
Elem2 prev=null;
do
{
if(temp.data.equals(o))
{
prev.next=temp.next;
break;
}
prev=temp;
temp=temp.next;
}
while (temp!=null);
System.out.println("List after deletion");
for (Elem2 e=front;e!=null ;e=e.next )
{
System.out.println(e.data);
}
}
}
No comments:
Post a Comment