Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this Java program. There are starter codes too. Thanks Listlteratorlnterface.java: package list; public interface ListIteratorInterface { boolean hasNext(); boolean hasPrevious(); E

I need help with this Java program. There are starter codes too. Thanks

image text in transcribed

image text in transcribed

Listlteratorlnterface.java:

package list;

public interface ListIteratorInterface { boolean hasNext(); boolean hasPrevious(); E next() throws Exception; E previous() throws Exception; void add(E data); void remove() throws Exception; void set(E data) throws Exception; }

List.java:

package list;

public class List

{

private Node first;

private Node last;

private int length;

private class Node

{

public E data;

public Node next;

public Node prev;

}

/**

* A generic inner ListIterator class that implements the

* ListIteratorInterface. It is used as a cursor to traverse the list.

*/

private class ListIterator implements ListIteratorInterface

{

private Node position; // the node on the left of the iterator

private Node previousPosition; // the previous iterator position

private boolean isAfterNext;

private boolean isAfterPrevious;

/**

* Constructs an iterator at the beginning of the linked list.

*/

public ListIterator()

{

position = null;

previousPosition = null;

isAfterNext = false;

isAfterPrevious = false;

}

/**

* Returns true if there is a node on the right of the iterator;

* otherwise, returns false.

*/

public boolean hasNext()

{

if (position == null)

return first != null;

else

return position.next != null;

}

/**

* Returns true if there is a node on the left of the iterator;

* otherwise, returns false.

*/

public boolean hasPrevious()

{

// Implement this method

}

/**

* Resets the iterator's previousPosition to its current position,

* resets the iterator's current position to the node on the right

* of its current position (moves the iterator, in the forward

* direction, past the node on the right), and returns the data

* value in the node that the iterator just passed over.

* Throws an exception if the iterator is at the end of the list.

*/

public E next() throws Exception

{

if (!hasNext())

throw new Exception("Called at the end of the list");

previousPosition = position; // Remember for remove.

if (position == null)

position = first;

else

position = position.next;

isAfterNext = true;

return position.data;

}

/**

* Resets the iterator's previousPosition to its current position,

* resets the iterator's current position to the node on the left

* of its current position (moves the iterator, in the backward

* direction, past the node on the left), and returns the data

* value in the node that the iterator just passed over.

* Throws an exception if the iterator is at the beginning of the

* list.

*/

public E previous() throws Exception

{

// Implement this method

}

/**

* Inserts a new node on the left of the iterator and resets

* the iterator position to this inserted node.

*/

public void add(E data)

{

if (position == null)

{

addFirst(data);

position = first;

}

else if(position.next == null)

{

addLast(data);

position = last;

}

else

{

Node newNode = new Node();

newNode.data = data;

newNode.next = position.next;

position.next.prev = newNode;

newNode.prev = position;

position.next = newNode;

position = newNode;

length++;

}

isAfterNext = false;

isAfterPrevious = false;

}

/**

* Removes the last node passed over by the iterator in a call to

* either next or previous.

* Throws an exception if remove is not called immediately after a

* call to either next or previous.

*/

public void remove() throws Exception

{

// Implement this method

}

/**

* Replaces the data value in the node on the left of the iterator

* with the input data value.

* Throws an exception if the node on the left of the iterator is

* null.

*/

public void set(E data) throws Exception

{

if (position == null)

throw new Exception("set call on a null node");

position.data = data;

}

}

// Constructs an empty linked list.

public List()

{

first = null;

last = null;

length = 0;

}

/**

* Returns the number of elements in the linked list.

*/

public int size()

{

return length;

}

/**

* Adds a new node at the beginning of the linked list.

*/

public void addFirst(E data)

{

Node newNode = new Node();

newNode.data = data;

if(length == 0)

{

first = newNode;

last = newNode;

}

else

{

newNode.next = first;

first.prev = newNode;

first = newNode;

}

length++;

}

/**

* Adds a new node at the end of the linked list.

*/

public void addLast(E data)

{

// Implement this method

}

/**

* Removes the first node in the linked list and returns this node's

* data value.

* Throws an exception if the list is empty.

*/

public E removeFirst() throws Exception

{

if(length == 0)

throw new Exception("Non-empty list expected");

E data = first.data;

if(length == 1)

{

first = null;

last = null;

}

else

{

first = first.next;

first.prev = null;

}

length--;

return data;

}

/**

* Removes the last node in the linked list and returns this node's

* data value.

* Throws an exception if the list is empty.

*/

public E removeLast() throws Exception

{

// Implement this method

}

/**

* Returns the data value in the first node of the linked list.

* Throws an exception if the list is empty.

*/

public E getFirst() throws Exception

{

if (first == null)

throw new Exception("Non-empty list expected");

return first.data;

}

/**

* Returns an iterator for iterating through the linked list.

*/

public ListIterator listIterator()

{

return new ListIterator();

}

/**

* @return a string representation of the elements of the linked list

* in the format [e0, e1, ..., en-2, en-1], where each ei is a data

* value in the list and e0 is the data value in the first node

* and en-1 is the data value in the last node. It returns [] if this

* list is empty.

*/

public String toString()

{

String string = "[";

if(length > 0)

{

Node node = first;

string += node.data;

while(node != last)

{

node = node.next;

string += ", " + node.data;

}

}

string += "]";

return string;

}

}

ListDemo.java:

package list;

public class ListDemo { public static void main(String[] args) { try { List list = new List(); list.addLast("Juliet"); System.out.println("Juliet added to list"); list.addLast("Bob"); System.out.println("Bob added to list"); list.addFirst("Tom"); System.out.println("Tom added to list"); list.addFirst("Mary"); System.out.println("Mary added to list"); System.out.println("list = " + list); System.out.println(list.removeFirst() + " removed from list"); System.out.println(list.removeLast() + " removed from list"); System.out.println("list = " + list); // | in the comments indicates the iterator position ListIteratorInterface iterator = list.listIterator(); // |TJ System.out.println("Iterator is at beginning of list"); iterator.add("Diana"); // D|TJ System.out.println("Diana added to list"); System.out.println("list = " + list); System.out.println("Iterator passed " + iterator.next()); // DT|J System.out.println("Iterator passed " + iterator.next()); // DTJ| iterator.add("Harry"); // DTJH| System.out.println("Harry added to list"); System.out.println("list = " + list); iterator = list.listIterator(); // |DTJH System.out.println("Iterator moved to beginning of list"); System.out.println("Iterator passed " + iterator.next()); // D|TJH System.out.println("Iterator passed " + iterator.next()); // DT|JH // Add more nodes after second node iterator.add("Nina"); // DTN|JH System.out.println("Nina added to list"); iterator.add("Romeo"); //DTNR|JH System.out.println("Romeo added to list"); System.out.println("list = " + list); String name = iterator.next(); System.out.println("Iterator passed " + name); // DTNRJ|H // Remove last traversed element iterator.remove(); // DTNR|H System.out.println(name + " removed from list"); System.out.println("list = " + list); System.out.println("Iterator passed " + iterator.previous());//DTN|RH System.out.println("Iterator passed " + iterator.previous());//DT|NRH name = iterator.previous(); System.out.println("Iterator passed " + name); // D|TNRH // Remove last traversed element iterator.remove(); // D|NRH System.out.println(name + " removed from list"); System.out.println("list = " + list); iterator = list.listIterator(); // |DNRH System.out.println("Iterator moved to beginning of list"); System.out.println(); // Print all elements backward and forward System.out.println("Forward version of list:"); while (iterator.hasNext()) System.out.println(iterator.next()); System.out.println(); System.out.println("Backward version of list:"); while (iterator.hasPrevious()) System.out.println(iterator.previous()); System.out.println(); System.out.println("Iterator is at beginning of list"); // |DNRH System.out.println("Iterator passed " + iterator.next()); // D|NRH iterator.add("Sara"); // DS|NRH System.out.println("Sara added to list"); System.out.println("list = " + list); System.out.println("Try to remove Sara from list: "); iterator.remove(); } catch(Exception e) { System.out.println(e); }

} }

Inked List with Doubly-Linked Nodes and Two External Pointers In today's lab you will imple named next and prev, that po respectively. Also, this linked list ha: to the first node in the list (the letter node), respectively. it and prev, that point to the node on the right and the node on the left, ement a generic linked list in which each node has two pointers, his linked list has two external pointers, named first and last, that point i the list (the left-most node) and the last node in the list (the right-most This project will have the following interface and classes: public interface ListiteratorInterface This interface is provided in the starter code. public class List Complete the implementation of the methods has Previous, previous, remove, addLast and removeLast in the starter code. public class ListDemo This class is provided in the starter code. The output of your program should look exactly like the output shown on the next page

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Modeling And Design

Authors: Toby J. Teorey, Sam S. Lightstone, Tom Nadeau, H.V. Jagadish

5th Edition

0123820200, 978-0123820204

More Books

Students also viewed these Databases questions

Question

=+b. Examine the chart in part (a) for any out-ofcontrol points.

Answered: 1 week ago

Question

You have

Answered: 1 week ago