Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Caqn you please add an iterator to the following class: /** * Represents a linke

ID: 3840351 • Letter: C

Question

Caqn you please add an iterator to the following class:

/**
* Represents a linked implementation of a stack.
*
* @author Java Foundations
* @version 4.0
*/
public class DropOutStack<T> implements StackADT<T>
{
private int top = 0;
private T[] stack;
/**
* Creates an empty stack.
*/
public DropOutStack(int initialCapacity)
{
stack = (T[])(new Object[initialCapacity]);
}

/**
* Adds the specified element to the top of this stack.
* @param element element to be pushed on stack
*/
public void push(T element)
{
if (size() == stack.length) {
for (int i = 0; i < stack.length - 1; i++) {
   stack[i] = stack[i + 1];
}
       stack[stack.length - 1] = element;
} else {
   stack[top] = element;
   top++;
}
}

/**
* 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");

top--;
T result = stack[top];
stack[top] = null;

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 stack[top-1];
}

/**
* Returns true if this stack is empty and false otherwise.
* @return true if stack is empty
*/
public boolean isEmpty()
{
   return (top == 0);

}


/**
* Returns the number of elements in this stack.
* @return number of elements in the stack
*/
public int size()
{
return top;
/*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 = "";
               for (int i = top-1; i >= 0; i--)
       result += stack[i] + " ";
               return result;
} // return result string
}


Thanks

Explanation / Answer

Java has for loop to process iterable collections. Here
hasnext() returns true if there are more items in the stack
next() returns an item from collection.

Add this code in your class.

public Iterator<Item> iterator()
{
    return new LinkedStackIterator();
}

//inner class to implement iterator interface
private class LinkedStackIterator implements Iterator <Item>
{
    private int i = size;
    private Node first = head; //the first node

    public boolean hasNext()
    {
      return (i > 0);
    }

    public Item next()
    {
      Item item = first.item;
      first = first.next;
      i--;
      return item;
    }
   }
  
Here main method look like this to traverse each element

public class LinkedStackTest
{
public static void main (String a[])
{
    Stack <T> s = new DropOutStack<T>();
    s.push(45);
    s.push(66);
    s.push(73);
    s.push(84);
    s.push(67);
    s.push(70);

    System.out.println("Size of the stack: " + s.size());

    // iterate through stack
    System.out.println("Stack contains following items till this moment:");
    for (Integer i : s)
      System.out.println(i);
}
}