Menu
support@nursinghomework.org
+1 714-215-3097

(Solved) : Would Code Bold Method Package List Public Class List Private Node First Private Node Last Q35619338

How would you code the bold method?

package list;

public class List<E>
{
private Node first;
private Node last;
private int length;
private class Node
{
public E data;
public Node next;
public Node prev;
}
// 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)
{
Node newNode = new Node();
newNode.data = data;
if(length == 0)
{
first = newNode;
last = newNode;
}
else
{
newNode.prev = last;
last.next = newNode;
last = newNode;
}
length++;
}
/**
* Removes the first node in the linked list and returns thisnode’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 thisnode’s
* data value.
* Throws an exception if the list is empty.
*/
public E removeLast() throws Exception
{
if(length == 0)
throw new Exception(“Non-empty list expected”);
E data = last.data;
if(length == 1)
{
first = null;
last = null;
}
else
{
last = last.next;
last.prev = null;
}
length–;
return data;
}
  
public E getFirst() throws Exception
{
if (first == null)
throw new Exception(“Non-empty list expected”);
return first.data;
}

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;
}
  
  
/**
* A generic inner ListIterator class that implements the
* ListIteratorInterface. It is used as a cursor to traverse thelist.
*/
private class ListIterator implementsListIteratorInterface<E>
{
  
private Node position; // the node on the left of theiterator
private Node previousPosition; // the previous iteratorposition
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 theiterator;
* 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 theiterator;
* otherwise, returns false.
*/
@Override
public boolean hasPrevious()
{
if (position == null)
return last != null;
else
return position.prev != null;
}

  
/**
* Resets the iterator’s previousPosition to its currentposition,
* resets the iterator’s current position to the node on theright
* of its current position (moves the iterator, in the forward
* direction, past the node on the right), and returns thedata
* value in the node that the iterator just passed over.
* Throws an exception if the iterator is at the end of thelist.
*/
public E next() throws Exception
{
if (!hasNext())
throw new Exception(“next called when the iterator is “
+ “at the end of the list”);
previousPosition = position;
if (position == null)
position = first;
else
position = position.next;
  
isAfterNext = true;
return position.data;
}

  
/**
* Resets the iterator’s previousPosition to its currentposition,
* resets the iterator’s current position to the node on theleft
* of its current position (moves the iterator, in thebackward
* 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 ofthe
* list.
*/
public E previous() throws Exception
{
if (!hasPrevious())
throw new Exception(“next called when the iterator is “
+ “at the end of the list”);
previousPosition = position;
if (position == null)
position = last;
else
position = position.prev;
  
isAfterNext = true;
return position.data;
}

  
/**
* 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 callto
* either next or previous.
* Throws an exception if remove is not called immediately aftera
* call to either next or previous.
*/
public void remove() throws Exception
{
// Implement this method.
}

  

Leave a Reply

Your email address will not be published. Required fields are marked *