Iterator for doubly linked list java

In this post, we are going to implement the iterator for doubly linked list in java. In previous post, we have already seen the iterator for singly linked list. The basic difference between iterators of singly and doubly linked list is, in case of singly linked list, we can iterator in forward direction only but in case of doubly linked list, we can iterate in both forward and backward directions. It’s recommended to first have a look for Iterator for Singly Linked list.

 

Since, doubly linked list has two reference pointers says next & previous, we need to implement the iterator and reverse iterator both to iterate in forward as well as backward directions. Java provides two interfaces java.lang.Iterable<T>java.util.Iterator<T> which we used to iterate the linked list in forward direction. To iterate in backward direction of doubly linked list, we need to create two interfaces ReverseIterable<T> & ReverseIterator<T> and doubly linked list will implements these two interfaces. Below is the complete implementation of iterator for doubly linked list in JAVA.

ReverseIterable.java :

 

ReverseIterator.java :

Now, implements java.lang.Iterable<T>java.util.Iterator<T>ReverseIterable<T> and ReverseIterator<T> interfaces to DoublyLinkedList class and override the below methods.

public Iterator<T> iterator() from java.lang.Iterable<T>

public boolean hasNext() from java.util.Iterator<T>

public T next() from java.util.Iterator<T>

public ReverseIterator<T> reverseIterator() from ReverseIterable<T>

public boolean hasPreviuos() from ReverseIterator<T>

public T previous() from ReverseIterator<T>

 

Iterator design pattern works on Iterator point. Iterator point defines the current position of iterator reference start from one end of the collection. In doubly linked list, we can traverse through the data in both forward and backward directions so we need to define two iterator points iteratorPointer & reverseIteratorPointer.

Iterator points are the key members for Iterator design pattern. We need to properly manage the iterator points on any modification done on doubly linked list. Default position of iterator pointer should be the root node and the default position of reverser iterator pointer should be the last node in doubly linked list.We need to reset the iterator points every time when we add/delete/modify the doubly linked list. To do so, we will have one more method called resetIteratorPointers() where we simply assign the iterator pointer as root node and reverse iterator pointer as last node of doubly linked list.

DoublyNode.java :

 

DoublyLinkedList.java :

 

Item.java :

 

TestDoublyLinkedList.java :

 

Output:

*********Iterating Doubly Linked List in Forward Direction.**********
Item [itemId=101, itemName=DELL Latitude 5647, price=300.0]
Item [itemId=102, itemName=Mac Book, price=1000.0]
Item [itemId=103, itemName=Mac Book Pro, price=2000.0]
Item [itemId=104, itemName=HP Elitebook, price=700.0]

*********Iterating Doubly Linked List in Backward Direction.**********
Item [itemId=104, itemName=HP Elitebook, price=700.0]
Item [itemId=103, itemName=Mac Book Pro, price=2000.0]
Item [itemId=102, itemName=Mac Book, price=1000.0]
Item [itemId=101, itemName=DELL Latitude 5647, price=300.0]

Printing Items Using forEach
Item [itemId=101, itemName=DELL Latitude 5647, price=300.0]
Item [itemId=102, itemName=Mac Book, price=1000.0]
Item [itemId=103, itemName=Mac Book Pro, price=2000.0]
Item [itemId=104, itemName=HP Elitebook, price=700.0]

 

Hope you like this post. Please leave your comments/suggestions.

 

 

You may like:

Introduction of Doubly Linked List(DLL)

Delete Operation in Doubly Linked List

Reverse a Doubly Linked List

Iterator For Doubly Linked List

Top