import java.io.*;
class node
{
public int x;
public node next;
}
class CircularLinkedList
{
public node first;
int count=0;
CircularLinkedList()
{
first=null;
}
void add (int v)
{
node temp=new node();
temp.x=v;
if(first==null)
first=temp;
else
{
node ptr=first;
for(int i=1;i
ptr=ptr.next;
ptr.next=temp;
}
temp.next=first;
count++;
}
void insert(int p,int v)
{
node ptr=first,temp;
int n = p==1?count+1:p;
for(int i=1;i
ptr=ptr.next;
temp=new node();
temp.x=v;
if(p==1)
{
temp.next=first;
first=temp;
}
else
temp.next=ptr.next;
ptr.next=temp;
count++;
}
void del(int p)
{
node ptr=first,temp;
int n = p==1?count+1:p;
for(int i=1;i
ptr=ptr.next;
if(p==1)
{
temp=first;
first=first.next;
}
else
temp=ptr.next;
ptr.next=ptr.next.next;
temp=null;
count--;
}
void show()
{
System.out.println("\nList Elements:");
node ptr=first;
for(int i=1;i<=count;i++)
{
System.out.print("\t"+ptr.x);
ptr=ptr.next;
}
}
}
class CListTest
{
public static void main(String as[]) throws Exception
{
String con="";
int x,op,p,v;
CircularLinkedList l1=new CircularLinkedList();
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println("Enter elements to create");
do
{
x=Integer.parseInt(br.readLine());
l1.add(x);
System.out.print("Add more?(y,n):");
con=br.readLine();
}while(con.equals("y"));
l1.show();
do
{
System.out.println("\n 1.Insert\n 2.Delete \n 3.Display \n 4.Exit");
System.out.println("\nSelect an option:");
op=Integer.parseInt(br.readLine());
if(op==1)
{
System.out.println("Enter Position to insert:");
p= Integer.parseInt(br.readLine());
System.out.println("Enter Value to insert:");
v= Integer.parseInt(br.readLine());
l1.insert(p,v);
}
if(op==2)
{
System.out.println("Enter Position to delete:");
p= Integer.parseInt(br.readLine());
l1.del(p);
}
l1.show();
}while(op<4);
}
}
/* Output: */
Enter elements to create
30
Add more?(y,n):y
40
Add more?(y,n):y
50
Add more?(y,n):n
List Elements:
30 40 50
1.Insert
2.Delete
3.Display
4.Exit
Select an option:1
Enter Position to insert:2
Enter Value to insert:99
List Elements:
30 99 40 50
1.Insert
2.Delete
3.Display
4.Exit
Select an option: 4
0 comments:
Post a Comment