0

I am trying to write a java class CircularList which contains the usual Node inner class and the instance variables:

4
  • 2
    This seams like a homework.. did i guess it? Commented Nov 29, 2010 at 4:43
  • Hey Fellow Student! Plz try something and when you reach a point you are stuck we can push you a bit. Imo look at the first lecture slides and the first exercises from your lab practicals. Commented Nov 29, 2010 at 4:44
  • 2
    people will be willing to assist if you show them what you have tried so far. Commented Nov 29, 2010 at 4:44
  • retagged to homework and fixed that list ( which obviously shows that this is homework) Commented Nov 29, 2010 at 4:46

4 Answers 4

2

OK, I won't give you the full implementation of the class, but instead I'll give you some advices.

  1. You don't need to hold a reference to the last element. Place a Prev and Next reference to your nodes and your last node will be first.Prev
  2. Besides the fact that circular lists have no end, they are quite the same than regular lists, but where you find the last item like this:

    Node tmp = first;

    while ( tmp.Next != null ) tmp = tmp.Next;

In a circular list the idea is like this:

Node tmp = first;

while ( tmp.Next != first )
   tmp = tmp.Next;

Because you will never find a node pointing to null, unless the list is empty. One final advice, if you need to implement an indexer, remember that in a circular list there is no such thing as index out of range, because

list[count] = list[0] = list[count * k]

Keep that in mind, so calculating your index for those methods can be quite tricky. For positive indexes, the main idea is:

index = index % count;

For negative ones is slightly different. I hope I can help you with my words. If you want an implementations, I believe that there should be a few if you ask Google politely :)

Best of luck!

Sign up to request clarification or add additional context in comments.

2 Comments

Hi I added the program I came up with and need additional assistance. Thanks
Could you please look this over for me?
2

I'm not sure what your issue is, you seem to have everything needed. The only difference between that linked list and a normal one would be adding to the end.

In a regular linked list you would create a new node and make the last element point to that node. In this case, you change the pointer in the •Last node to point to the new node, and make the new node point to •First.

Removal works in the same way as a normal linked list. Whenever you want to remove a node, you find which other node points to that one (either previous, or in case of removing the first, check •Last) and make it point to wherever the one being deleted was pointing.

If this does not resolve your issue let me know and I'll try to help.

Just noticed someone has already asked the exact same question: Can I use java.util.LinkedList to construct a circular/cyclic linked list?

3 Comments

Hi I added the program I came up with and need additional assistance. Thanks
First of all I belive you have an error in removeLast(): By doing current.setNextNode(..) inside the while loop you are making every node point to the first. You want to have that outside the while, so only the second last node's pointer gets changed. For the question, you just call removeByPosition() with p as the argument. Change this function to set tail=current.getNextNode() at the end. Then keep calling it with p as argument until only 1 node is left.
Could you please look this over for me?
0

Hint... circular list should have next and previous instead of first and last. The logic matters

Comments

0
class Node    {
    int value;
    Node next;
    Node prev;
    Node(int initialValue)    {
        value = initialValue;
        next =  null;
        prev = null;
    }
    public int getValue()    {
        return this.value;
    }
}

class NodeList    {
    Node pointer;
    NodeList()    {
        pointer = null;
    }
    public void insertNode(int nodeValue)    {
        Node newNode = new Node(nodeValue);
        if( pointer == null )    {
            newNode.next = newNode;
            newNode.prev = newNode;
        }else if( pointer.next == null && pointer.prev == null && pointer != null )    {
            newNode.next = pointer;
            newNode.prev = pointer;
            pointer.prev = newNode;
            pointer.next = newNode;
        }
        else if( pointer != null )    {
            newNode.next = pointer.next;
            newNode.prev = pointer;
            pointer.next.prev = newNode;
            pointer.next = newNode;
        }
        pointer = newNode;
        System.out.println(“Successfully inserted : ” + pointer.getValue());
    }
    public void printRing( boolean direction )    {
        Node tempNode = pointer;
        do    {
            System.out.println( “Value = ” + tempNode.getValue() );
            tempNode = direction ? tempNode.next : tempNode.prev;
        } while( tempNode.value != pointer.value );    
    }    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.