Circular Linked List

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • morris11
    New Member
    • Mar 2008
    • 8

    Circular Linked List

    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?

    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);
    }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by morris11
    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?
    You have to find a definition of your last node; I'd say that if the next node of a
    node is the first node then the node itself happens to be the last node.

    kind regards,

    Jos

    Comment

    • morris11
      New Member
      • Mar 2008
      • 8

      #3
      Originally posted by JosAH
      You have to find a definition of your last node; I'd say that if the next node of a
      node is the first node then the node itself happens to be the last node.

      kind regards,

      Jos
      I got the idea.
      Thank you very much

      Comment

      Working...