Hi guys here is a code on Circular Linked List in Java! Program :
import java.util.Scanner;
interface CllAdt{
public void insert();
public void delete();
public void display();
public int len();
}
class CNode{
int data;
CNode link;
}
public class CQSll extends CNode implements CllAdt{
CNode first;
Scanner s=new Scanner(System.in);
public CQSll(){
first=null;
}
public void insert(){
System.out.println("Enter the position to insert a node :");
int pos=s.nextInt();
System.out.println("Enter the data into a node :");
int ele=s.nextInt();
CNode p=new CNode();
p.data=ele;
p.link=null;
CNode temp;
if(pos<1 || pos>len()+1)
System.out.println("Invalid position and insertion is not possible");
else if(pos==1&&len()==0)
{
first=p;
first.link=first;
System.out.println("Entered position is(1st):"+pos);
}
else if((pos==1)&&(len()!=0)){
temp=first.link;
while(temp.link!=first)
temp=temp.link;
p.link=first;
first=p;
temp.link=first;
System.out.println("Entered position is(1st):"+pos);
}
else if(pos==len()+1){
temp=first;
while(temp.link!=first)
temp=temp.link;
temp.link=p;
p.link=first;
System.out.println("Entered position is(Last) :"+pos);
}
else
{
temp=first;
for(int i=1;i<pos-1;i++)
temp=temp.link;
p.link=temp.link;
temp.link=p;
System.out.println("Entered position is:"+pos);
}
}
public void delete(){
System.out.println("Enter the position to delete a node :");
int pos=s.nextInt();
CNode p,temp;
if(pos<1||(pos>len()+1)||(len()==0))
System.out.println("Invalid position and deletion is not possible");
else if(pos==1&& len()==1){
temp=first;
first=null;
System.out.println("Only one i.e. Deleted item:"+temp.data);
}
else if(pos==1&&len()!=1){
temp=first.link;
while(temp.link!=first)
temp=temp.link;
p=first;
first=p.link;
temp.link=first;
System.out.println("From begininning i.e. Deleted item :"+p.data);
}
else if(pos==len()){
temp=first;
for(int i=1;i<pos-1;i++)
temp=temp.link;
p=temp.link;
temp.link=first;
System.out.println("From Ending i.e. Deleted item :"+p.data);
}
else{
temp=first;
for(int i=1;i<pos-1;i++)
temp=temp.link;
p=temp.link;
temp.link=p.link;
System.out.println("Deleted item :"+p.data);
}
}
public void display(){
CNode temp;
if(first==null)
System.out.println("Circular Linked list is empty :");
else{
System.out.println("List of elements :");
System.out.println("First->");
temp=first;
System.out.println(temp.data+"-->");
temp=first.link;
while(temp!=first){
System.out.println(temp.data+"-->");
temp=temp.link;
}
System.out.println("End");
}
}
@Override
public int len(){
int count=1;
CNode temp=first;
if(temp==null) return(0);
else if(temp.link==first) return(1);
else{
while(temp.link!=first){
count++;
temp=temp.link;
}
return count;
}
}
public static void main(String args[]){
System.out.println("Circular Linked List ADT");
Scanner s=new Scanner(System.in);
CQSll c=new CQSll();
String ch="y";
while(ch.equalsIgnoreCase("y")){
System.out.println("\n CLL Operations :");
System.out.println("1. Insert 2. Delete 3. Display 4. Length 5. Exit :");
System.out.println("Enter the option :");
int opt=s.nextInt();
switch(opt){
case 1: c.insert();break;
case 2: c.delete();break;
case 3: c.display();break;
case 4: System.out.println("Length of CLL is :"+c.len());
case 5: System.exit(0);
case 6: System.out.println("Invalid option!");
}
System.out.println("\nDo you want to continue(y/n)");
ch=s.next();
}
}
}
No comments:
Post a Comment