Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * custom implementation of a stack using Dr. Kuperman's interface
- */
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- public class MyStack<T> implements StackADT<T>, Iterable<Square>
- {
- //field
- private ArrayList<T> data;
- /**
- * constructor
- */
- public MyStack()
- {
- data = new ArrayList<T>();
- }
- @Override
- public void push(T item)
- {
- data.add(data.size(), item); //add past the current data
- }
- @Override
- public T pop() throws NoSuchElementException
- {
- if (this.isEmpty())
- throw new NoSuchElementException("Stack is empty");
- return data.remove(data.size() - 1);
- }
- @Override
- public T top() throws NoSuchElementException
- {
- if (this.isEmpty())
- throw new NoSuchElementException("Stack is empty");
- return data.get(data.size() - 1);
- }
- @Override
- public int size()
- {
- return data.size();
- }
- @Override
- public boolean isEmpty()
- {
- return data.size() == 0;
- }
- @Override
- public void clear()
- {
- data.removeAll(data);
- }
- @Override
- public Iterator<Square> iterator()
- {
- // TODO Auto-generated method stub
- return null;
- }
- }//end class
Add Comment
Please, Sign In to add comment