Question
Write a Java program that has the following inputs and outputs: Iterator Use the attached code. The attached code contains 4 packages: 1- iterator: contains
Write a Java program that has the following inputs and outputs:
Iterator Use the attached code.
The attached code contains 4 packages: 1- iterator: contains 1 interface that contains methods of Iterator that you implement in all custom iterators. 2- arraylist: contains class of custom ArrayList, custom ArrayList Iterator and test. this package is your reference in solving the assignment. 3- linkedlist: contains class of custom LinkedList and its Node. 4- doublelinkedlist: contains class of custom DoubleLinkedList and its Node.
Create Main class for LinkedList Iterator and DoubleLinkedList Iterator. Write a comment for each method that you write.
I do not want the same dream that there is an error in LinkedList, DoubleLinkedList
else if(i
public int indexOf(Object o) { ListNode index = head; if(size == 0) { System.out.println("The list is empty"); return -1; } else { for(int i = 0; i
} return -1; } }
public Object get(int i) { ListNode index; index = tail;
if( size == 0 ) { System.out.print("The List is empty"); return null; } else { for(int j = 0; j
public void bubblesort() {
}
}
class ListNode {
Object data; ListNode next;
ListNode() { this( null, null ); }
ListNode( Object data ) { this( data, null ); }
ListNode( Object data, ListNode next ) { this.data = data; this.next = next; }
}
class LinkedListIterator implements ListIterator {
private LinkedList list; private ListNode current;
public LinkedListIterator( LinkedList list ) { this.list = list; current = list.head; }
public boolean hasPrevious() { return list.size > 0 && current != list.head; }
public Object previous() { Object o = null;
if( hasPrevious() ) { ListNode previous = list.head; while( previous.next != current ) previous = previous.next; current = previous; o = current.data; }
return o; }
public boolean hasNext() { return list.size > 0 && current != null; }
public Object next() { Object o = null;
if( hasNext() ) { o = current.data; current = current.next; }
return o; }
}
------------------------------------------------------------------------------------------------------------------- public interface List {
// insert an element at the end of the list public void insert( Object o );
// insert an element at the ith position public void insert( Object o, int i );
// remove the first occurrence of the object public Object remove( Object o );
// remove the ith element public Object remove( int i );
// find the index of the first occurrence of the object public int indexOf( Object o );
// find whether the object is in the list or not public boolean contains( Object o );
// return the ith element public Object get( int i );
// return the size of the list public int size();
// return whether the list is empty of not public boolean isEmpty();
// remove all objects from the list public void clear();
// print all elements in the list public void print();
// return an iterator for the list public ListIterator iterator();
} -------------------------------------------------------------------------------------------------------------------- public interface ListIterator extends Iterator {
public boolean hasPrevious();
public Object previous();
}
import java.util.Scanner; public class ListTest public void test( List list ) / insert list.insert( new Integer( 20) ); list.insert( new Integer( 30) ); list.insert( new Integer( 40)); list.print0; list.insert( new Integer( 10), 0); list.insert( new Integer( 15), 1); list.insert( new Integer( 50), 20); list.print0; /ll indexOf) and contains) System.out.println(list.indexOf new Integer( 50))); System.out.println(list.contains( new Integer( 20))); System.out.println(list.contains( new Integer( 35))); /l remove0, size0, and isEmpty0 list.removel new Integer( 20)); list.removel new Integer 35)); list.print0; list.remove( 0); list.print0; list.remove( list.size0 - 1); list.remove( list.size) -1); list.print0: System.out.println(list.isEmpty0); /l get0 for( int i = 0; i size) i size; ift size >= elementslength ) resize(); int index - size; while( index >i) elements[index] elements[index - 1]; -index elementso public Object remove( int i) if i = size ? null : elements[i]; public void print) for( int i = 0; i size) i size; fsizeO lsize) inserto); return; h ListNode node = new ListNode( 0 ); if(-= 0 ) node.next = head head node se int index = 0 ListNode current = head while( index
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started