Question

Create a nested inner class inside a file called LinkedList.java called the ListIterator. ListIterator: This public...

Create a nested inner class inside a file called LinkedList.java called the ListIterator.

ListIterator: This public class is nested within an LinkedList data structure and allows us to traverse the elements in any collection, access any data element and remove any data elements of the collection. It also adds the functionality to move forward and backward, which is new functionality. We are going to build this data structure from scratch.

  1. Instance variables (fields) are:
    mPrevious (Node) – the previous node visited in the LinkedList.  
    mNext (Node) – the next node about to be visited in the LinkedList.
    mCursor (int) - the position of the iterator in the LinkedList (starts at 0)

ListIteratorDemo: Create a new ListIteratorDemo.java file and in the public static void main(String[ ] args) method, perform the following:

  1. In the main method, create a new LinkedList to store Strings: LinkedList<String> coursesList = new LinkedList<>()
  2. Create a new ListIterator list on the coursesList.
  3. While the list iterator hasNext, call the next() method and print out the Element. This will traverse the LinkedList from head to tail.
  4. Now, we'll traverse backwards. After step 4, the list iterator is at the tail, so while the list iterator hasPrevious, call the previous() method and print out the Element. This will traverse the LinkedList from tail to head.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

LinkedList class.

 import java.util.NoSuchElementException; /** * Sample implementation of LinkedList. */ public class LinkedList<E> { private Node head; private Node last; private int size; public LinkedList() { head = null; last = null; size = 0; } public void add(E val) { if (head == null) { head = new Node(val); last = head; } else { Node newNode = new Node(val); last.next = newNode; newNode.prev = last; last = newNode; } size++; } public ListIterator iterator() { return new ListIterator(); } public class ListIterator { ListIterator() { mPrevious = null; mNext = head; mCursor = 0; } private Node mPrevious; private Node mNext; private int mCursor; public boolean hasNext() { return mNext != null; } public boolean hasPrevious() { return mPrevious != null; } public Node next() { if (!hasNext()) { throw new NoSuchElementException(); } // return mNext and make mNext to next of that. Node next = mNext; mNext = mNext.next; mPrevious = next; mCursor++; return next; } public Node previous() { if (!hasPrevious()) { throw new NoSuchElementException(); } Node prev = mPrevious; mPrevious = mPrevious.prev; mNext = prev; mCursor--; return prev; } } static class Node<E> { Node next; Node prev; E val; Node(E val) { this.val = val; } } } 

ListIteratorDemo class

 public class ListIteratorDemo { public static void main(String [] args) { LinkedList<String> coursesList = new LinkedList<>(); coursesList.add("Course1"); coursesList.add("Course2"); coursesList.add("Course3"); coursesList.add("Course4"); coursesList.add("Course5"); LinkedList.ListIterator iterator = coursesList.iterator(); // traverse it forward. System.out.println("Forward: "); while (iterator.hasNext()) { System.out.println(iterator.next().val); } // traverse it backward. System.out.println("Previous: "); while (iterator.hasPrevious()) { System.out.println(iterator.previous().val); } } } 

Screenshot of the code working with output.

public static void main(String [] args) { LinkedList<String> coursesList = new Linkedlist>(); coursesList.add(Course1); cou

Add a comment
Know the answer?
Add Answer to:
Create a nested inner class inside a file called LinkedList.java called the ListIterator. ListIterator: This public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test...

    Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test class: Remember your header with name, date, and assignment. Also include class names that will be tested. Psuedocode (level 0 or mixture of level 0 and algorithm [do not number steps]) is required if main() contains more than simple statements (for example, your program includes constructs for decisions (if/else), loops, and methods. For Secondary class(es): Include a JavaDoc comment that describes the purpose of...

  • //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args )...

    //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args ) { Scanner in = new Scanner( System.in ); LinkedList teamList = new LinkedList(); final int TEAM_SIZE = Integer.valueOf(in.nextLine()); for (int i=0; i<TEAM_SIZE; i++) { String newTeamMember = in.nextLine(); teamList.append(newTeamMember); } while (in.hasNext()) { String removeMember = in.nextLine(); teamList.remove(removeMember); }    System.out.println("FINAL TEAM:"); System.out.println(teamList); in.close(); System.out.print("END OF OUTPUT"); } } =========================================================================================== //PoD import java.util.NoSuchElementException; /** * A listnked list is a sequence of nodes with...

  • When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses...

    When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Any suggestions? public class LinkedList<T> {    Node<T> itsFirstNode;    Node<T> itsLastNode;    private int size;    public LinkedList() {        itsFirstNode = null;        itsLastNode = null;          size = 0;    }    public Iterator<T> getIterator() {        return new Iterator(this);    }    // THIS WILL NEED...

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element...

    Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /**...

    Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /** Removes all of the elements from this set */ void clear(); /** Returns true if this set contains no elements @return true if this set contains no elements */ boolean isEmpty(); /** Returns the number of elements in this set (its cardinality) @return the number of elements in this set */ int size(); /** Returns an iterator over the elements in this set @return...

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

  • Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given reference...

    Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable {    // ---------------- nested Node class...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT