Comp Sci Creating a DoublyLinkedList Iterator in Java: Tips and Tricks

  • Thread starter Thread starter Live4eva_2
  • Start date Start date
  • Tags Tags
    Java Tips
AI Thread Summary
Creating a DoublyLinkedList iterator in Java can be challenging, particularly when deciding whether to implement the iterator as a separate class or within the main list class. The discussion highlights confusion over the visibility of inner classes and the inability to access the `Node<T> current` variable from the `LinkedList<T>` class. It is noted that multiple inner classes are allowed, but proper visibility and instantiation must be ensured. The user is advised to seek further assistance on dedicated forums for more targeted help. Understanding class structure and access modifiers is crucial for resolving these issues.
Live4eva_2
Messages
28
Reaction score
0
Hi I'm struggling to create a DoublyLinkedList<T> class in Java.
Well,it's actually the iterator that's freaking me out..
Most tutorials and documents I've read use three classes for this:Node<T>,DoublyLinkedList<T> and LinkedListIterator<T>.

I can see the point in creating Node and DoublyLinkedList<T> classes but i don't know why I can't simply just include the 2 iterator reference variables(next and previous) in the DoublyLinkedList<T> class instead of writing an additional iterator class.
 
Physics news on Phys.org
OK nevermind that..Let me pose another question:
public class LinkedList<T>
{
public Node<T> first;
public Node<T> last;
protected class Node<T>
{
public Node<T> next;
public Node<T> last;


}
public class Iterator<T>
{
protected Node<T> current;
protected Node<T> previous;


}


}
I've only included the classes and variables in order to illustrate the visibility.
when I complile I'm getting the msg that "Node<T> current" isn't available in LinkedList<T> class...why not !?Am I only allowed one inner class?Also i ensured that my iterator was constructed within the LinkedList constructor so i don't think that it could be a problem with instantiating the iterator object.
 

Similar threads

Replies
2
Views
4K
Replies
12
Views
2K
Replies
1
Views
1K
Replies
3
Views
2K
Replies
12
Views
4K
Replies
1
Views
2K
Replies
1
Views
10K
Back
Top