Creating a DoublyLinkedList Iterator in Java: Tips and Tricks

  • Context: Comp Sci 
  • Thread starter Thread starter Live4eva_2
  • Start date Start date
  • Tags Tags
    Java Tips
Click For Summary
SUMMARY

The discussion centers on the challenges of implementing a DoublyLinkedList iterator in Java. Users typically create three classes: Node, DoublyLinkedList, and LinkedListIterator. The original poster struggles with visibility issues, specifically why the Node current variable is inaccessible within the LinkedList class. The consensus is that proper encapsulation and class structure are essential for successful implementation.

PREREQUISITES
  • Understanding of Java class structures and inner classes
  • Familiarity with generics in Java
  • Knowledge of iterator design patterns
  • Basic understanding of linked list data structures
NEXT STEPS
  • Study Java inner classes and their visibility rules
  • Learn about implementing iterators in Java using the Iterator interface
  • Explore best practices for designing linked list data structures
  • Review examples of DoublyLinkedList implementations in Java
USEFUL FOR

Java developers, computer science students, and software engineers interested in data structure implementations and iterator design patterns.

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 ·
Replies
2
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 1 ·
Replies
1
Views
3K