Hi I just want you to add an iterator in the LinkedStack.java class. The example
ID: 3840350 • Letter: H
Question
Hi I just want you to add an iterator in the LinkedStack.java class. The example is for the LinkedList.java is as follows:
package jsjf;
import jsjf.exceptions.*;
import java.util.*;
/**
* LinkedList represents a linked implementation of a list.
*
* @author Java Foundations
* @version 4.0
*/
public class LinkedList<T> implements ListADT<T>, Iterable<T>
{
protected int count;
protected LinearNode<T> head, tail;
protected int modCount;
/**
* Creates an empty list.
*/
public LinkedList()
{
count = 0;
head = tail = null;
modCount = 0;
}
public void addElement(T element)
{
LinearNode<T> node = new LinearNode<T>(element);
if (isEmpty())
head = node;
else
tail.setNext(node);
tail = node;
count++;
}
/**
* Removes the first element in this list and returns a reference
* to it. Throws an EmptyCollectionException if the list is empty.
*
* @return a reference to the first element of this list
* @throws EmptyCollectionException if the list is empty
*/
public T removeFirst() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("LinkedList");
T result = head.getElement();
head = head.getNext();
count--;
if (isEmpty())
tail = null;
return result;
}
/**
* Removes the last element in this list and returns a reference
* to it. Throws an EmptyCollectionException if the list is empty.
*
* @return the last element in this list
* @throws EmptyCollectionException if the list is empty
*/
public T removeLast() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("LinkedList");
LinearNode<T> current = head;
LinearNode<T> current1=null;
while (current != tail){
current1=current;
current = current.getNext();
// make current points to next element
}
T a=tail.getElement();
tail=current1;
if(tail==null)
head=null;
else
tail.setNext(null);
if(tail==null)
head=null;
else
tail.setNext(null);
count--;
return a;
}
/**
* Removes the first instance of the specified element from this
* list and returns a reference to it. Throws an EmptyCollectionException
* if the list is empty. Throws a ElementNotFoundException if the
* specified element is not found in the list.
*
* @param targetElement the element to be removed from the list
* @return a reference to the removed element
* @throws EmptyCollectionException if the list is empty
* @throws ElementNotFoundException if the target element is not found
*/
public T remove(T targetElement) throws EmptyCollectionException,
ElementNotFoundException
{
if (isEmpty())
throw new EmptyCollectionException("LinkedList");
boolean found = false;
LinearNode<T> previous = null;
LinearNode<T> current = head;
while (current != null && !found)
if (targetElement.equals(current.getElement()))
found = true;
else
{
previous = current;
current = current.getNext();
}
if (!found)
throw new ElementNotFoundException("LinkedList");
if (size() == 1) // only one element in the list
head = tail = null;
else if (current.equals(head)) // target is at the head
head = current.getNext();
else if (current.equals(tail)) // target is at the tail
{
tail = previous;
tail.setNext(null);
}
else // target is in the middle
previous.setNext(current.getNext());
count--;
modCount++;
return current.getElement();
}
/**
* Returns the first element in this list without removing it.
*
* @return the first element in this list
* @throws EmptyCollectionException if the list is empty
*/
public T first() throws EmptyCollectionException
{
if(isEmpty())
{
throw new EmptyCollectionException("LinkedList");
}
return head.getElement();
}
/**
* Returns the last element in this list without removing it.
*
* @return the last element in this list
* @throws EmptyCollectionException if the list is empty
*/
public T last() throws EmptyCollectionException
{
if(isEmpty())
{
throw new EmptyCollectionException("LinkedList");
}
return tail.getElement();
}
/**
* Returns true if the specified element is found in this list and
* false otherwise. Throws an EmptyCollectionException if the list
* is empty.
*
* @param targetElement the element that is sought in the list
* @return true if the element is found in this list
* @throws EmptyCollectionException if the list is empty
*/
public boolean contains(T targetElement) throws EmptyCollectionException
{
if(isEmpty())
{
throw new EmptyCollectionException("LinkedList");
}
return (count==0);
}
/**
* Returns true if this list is empty and false otherwise.
*
* @return true if the list is empty, false otherwise
*/
public boolean isEmpty()
{
return (count==0);
}
/**
* Returns the number of elements in this list.
*
* @return the number of elements in the list
*/
public int size()
{
return count;
}
/**
* Returns a string representation of this list.
*
* @return a string representation of the list
*/
public String toString()
{
String result = ""; // initialize it to empty string
LinearNode t = head; // start from top
while (t != null) // iterate until it reaches the end of the list (end of list is when current == null)
{
result = result + (t.getElement()).toString() + " "; // append it's value to result string
t = t.getNext(); // make current points to next element
}
return result; // return result string
}
/**
* Returns an iterator for the elements in this list.
*
* @return an iterator over the elements of the list
*/
public Iterator<T> iterator()
{
return new LinkedListIterator();
}
/**
* LinkedIterator represents an iterator for a linked list of linear nodes.
*/
private class LinkedListIterator implements Iterator<T>
{
private int iteratorModCount; // the number of elements in the collection
private LinearNode<T> current; // the current position
/**
* Sets up this iterator using the specified items.
*
* @param collection the collection the iterator will move over
* @param size the integer size of the collection
*/
public LinkedListIterator()
{
current = head;
iteratorModCount = modCount;
}
/**
* Returns true if this iterator has at least one more element
* to deliver in the iteration.
*
* @return true if this iterator has at least one more element to deliver
* in the iteration
* @throws ConcurrentModificationException if the collection has changed
* while the iterator is in use
*/
public boolean hasNext() throws ConcurrentModificationException
{
if (iteratorModCount != modCount)
throw new ConcurrentModificationException();
return (current != null);
}
/**
* Returns the next element in the iteration. If there are no
* more elements in this iteration, a NoSuchElementException is
* thrown.
*
* @return the next element in the iteration
* @throws NoSuchElementException if the iterator is empty
*/
public T next() throws ConcurrentModificationException
{
if (!hasNext())
throw new NoSuchElementException();
T result = current.getElement();
current = current.getNext();
return result;
}
/**
* The remove operation is not supported.
*
* @throws UnsupportedOperationException if the remove operation is called
*/
public void remove() throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
}
}
Can you please Add an iterator to the LinkedStack.java as there is an iterator in the LinkedList.java class:
package jsjf;
import jsjf.exceptions.*;
/**
* Represents a linked implementation of a stack.
*
* @author Java Foundations
* @version 4.0
*/
public class LinkedStack implements StackADT
{
private int count;
private LinearNode top;
/**
* Creates an empty stack.
*/
public LinkedStack()
{
count = 0;
top = null;
}
/**
* Adds the specified element to the top of this stack.
* @param element element to be pushed on stack
*/
public void push(T element)
{
LinearNode temp = new LinearNode(element);
temp.setNext(top);
top = temp;
count++;
}
/**
* Removes the element at the top of this stack and returns a
* reference to it.
* @return element from top of stack
* @throws EmptyCollectionException if the stack is empty
*/
public T pop() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("stack");
T result = top.getElement();
top = top.getNext();
count--;
return result;
}
/**
* Returns a reference to the element at the top of this stack.
* The element is not removed from the stack.
* @return element on top of stack
* @throws EmptyCollectionException if the stack is empty
*/
public T peek() throws EmptyCollectionException
{
if(isEmpty())
{
throw new EmptyCollectionException("stack");
}
return top.getElement();
}
/**
* Returns true if this stack is empty and false otherwise.
* @return true if stack is empty
*/
public boolean isEmpty()
{
return (count == 0);
}
/**
* Returns the number of elements in this stack.
* @return number of elements in the stack
*/
public int size()
{
return count;
/**
*keeping the track of the number
*of elements in the stack.
*/
}
/**
* Returns a string representation of this stack.
* @return string representation of the stack
*/
public String toString()
{
String result = ""; // initialize it to empty string
LinearNode t = top; // start from top
while (t != null) // iterate until it reaches the end of the list (end of list is when current == null)
{
result = result + (t.getElement()).toString() + " "; // append it's value to result string
t = t.getNext(); // make current points to next element
}
return result; // return result string
}
}
Thanks.
Explanation / Answer
private class LinkedStackIterator implements Iterator<T>
{
private int iteratorModCount; // the number of elements in the collection
private LinearNode<T> current; // the current position
/**
* Sets up this iterator using the specified items.
*
* @param collection the collection the iterator will move over
* @param size the integer size of the collection
*/
public LinkedStackIterator()
{
current = top;
iteratorModCount = modCount;
}
/**
* Returns true if this iterator has at least one more element
* to deliver in the iteration.
*
* @return true if this iterator has at least one more element to deliver
* in the iteration
* @throws ConcurrentModificationException if the collection has changed
* while the iterator is in use
*/
public boolean hasNext() throws ConcurrentModificationException
{
if (iteratorModCount != modCount)
throw new ConcurrentModificationException();
return (current != null);
}
/**
* Returns the next element in the iteration. If there are no
* more elements in this iteration, a NoSuchElementException is
* thrown.
*
* @return the next element in the iteration
* @throws NoSuchElementException if the iterator is empty
*/
public T next() throws ConcurrentModificationException
{
if (!hasNext())
throw new NoSuchElementException();
T result = pop();
return result;
}
/**
* The remove operation is not supported.
*
* @throws UnsupportedOperationException if the remove operation is called
*/
public void remove() throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
}
=======
I have implemented LinkedStackIterator, it uses top and pop() for check hasNext and pop().
Thanks, let me know if there is any concern.