Question: Implement a custom LinkedList ADT with an iterator in Java. The LinkedList should support the basic operations: insertion deletion traversal using an iterator Requirements Implement

Implement a custom LinkedList ADT with an iterator in Java. The LinkedList should support the basic operations:
insertion
deletion
traversal using an iterator
Requirements
Implement a class named CustomLinkedList with the following methods:
insert(int data): Inserts a new node with the given data.
delete(int data): Deletes the first occurrence of a node with the given data.
iterator(): Returns an iterator for traversing the linked list.
Implement an inner class named LinkedListIterator within CustomLinkedList to serve as the iterator. The iterator should have the following methods:
hasNext(): Returns true if there is a next element, false otherwise.
next(): Returns the next element and moves the iterator to the next position.
Demonstrate the functionality of the CustomLinkedList by iterating through its elements using the custom iterator. To get things started, use the following starter code:
public class CustomLinkedList {
private Node head;
// Other methods...
public Iterator iterator(){
return new LinkedListIterator();
}
private class Node {
int data;
Node next;
Node(int data){
this.data = data;
this.next = null;
}
}
private class LinkedListIterator implements Iterator {
private Node current = head;
@Override
public boolean hasNext(){
return current != null;
}
@Override
public Integer next(){
if (!hasNext()){
throw new NoSuchElementException();
}
int data = current.data;
current = current.next;
return data;
}
}
// Other methods...
}
public class Main {
public static void main(String[] args){
CustomLinkedList linkedList = new CustomLinkedList();
// Insert some elements
linkedList.insert(1);
linkedList.insert(2);
linkedList.insert(3);
// Iterate and display elements
Iterator iterator = linkedList.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next()+"");
}
}
}
Testing:
Provide the functionality to read integer data from a text file.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!