I am trying to create a circular list that includes insert() , find() and remove() methodes. also a step() method that moves current along to the next link.
I still need to display the list by breaking the circle at some arbitrary point to print it on the screen. Does anyone have an idea how to do that?
I still need to display the list by breaking the circle at some arbitrary point to print it on the screen. Does anyone have an idea how to do that?
Code:
import javax.swing.JOptionPane;
public class CLL
{
public class Link
{
int item=0;
Link next;
public Link()
{
//this.item = 0;
Link next = null;
}
public Link(int i, Link n)
{
item = i;
next = n;
}
}
Link head;
public void insert(int item)
{
if(head == null)
{
head = new Link(item,null);
head.next = head;
}
else
{
head.next = new Link(item,head.next);
}
}
public void remove(int key)
{
Link current = head;
do
{
if(current.next.item == key )
{
Link temp = current.next;
current = temp.next;
if(temp == head)
{
head = head.next;
}
temp = null;
break;
}
current = current.next;
} while(current != head);
}
public int find(int key)
{
Link current = head;
while(current!=null && !(current.item == key))
{
current = current.next;
if(current!= null)
{
return current.item;
}
}
return 0;
}
public static void step(Link current)
{
current= current.next;
}
public static void main(String args[])
{String output ="";
output+="it's working";
JOptionPane.showMessageDialog(null,output);
}
}
Comment