import java.util.*;
class node
{
int data;
node left;
node right;
node(int d, node l,node r)
{
data=d;
left=l;
right=r;
}
}
class Link
{
node head;
node tail;
public void insertfront(int data)
{
int flag=0;
node newnode=new node(data,null,null);
if (head==null)
{
head=newnode;
tail=head;
tail.right=head;
head.left=tail;
flag=1;
}
else
{
newnode.right=head;
newnode.left=tail;
tail.right=newnode;
head=newnode;
flag=1;
}
if (flag==1)
{
System.out.println("element "+data+"is inserted.");
}
else
System.out.println("element "+data+"is not inserted.");
}
public void insertend(int data)
{
node newnode=new node(data,null,null);
node current=head;
while (current.right!=head)
{
current=current.right;
}
current.right=newnode;
newnode.left=current;
newnode.right=head;
tail=newnode;
}
public void insertatPosition(int data, int pos)
{
node newnode=new node(data,null,null);
node current=head;
node previous=head;
int i=1;
while (i!=(pos-1))
{
current=current.right;
i++;
}
newnode.right=current.right;
newnode.left=current;
current.right=newnode;
//current=current.right;
}
public void display()
{
node current=head;
do
{
System.out.print(" "+current.data);
current=current.right;
}while (current!=head);
System.out.println();
}
}
class Circular_Double
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
Link obj=new Link();
while (true)
{
System.out.println("*******Please select the option.*********");
System.out.println("\t1.Insert the element at first in the list."
+"\n\t2.Insert at element at end."
+"\n\t3.Insert element at certain positon."
+"\n\t4.Insert element at last."
+"\n\t5.Delete the element."
+"\n\t6.Search element."
+"\n\t7.display the Linked list."
+"\n\t8.Exit from the program.");
int num=scan.nextInt();
switch (num)
{
case 1:
System.out.println("Insert the element.");
int n=scan.nextInt();
obj.insertfront(n);
break;
case 2:
System.out.println("Insert the element.");
n=scan.nextInt();
obj.insertend(n);
break;
case 3:
System.out.println("Insert the element.");
n=scan.nextInt();
System.out.println("POSITION");
int p=scan.nextInt();
obj.insertatPosition(n,p);
break;
case 7:
obj.display();
break;
case 8:
System.exit(0);
break;
default :
System.out.println("U entered invalied data.So,enter valied data.");
}
}
}
}
0 Comments