Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. import java.util.EmptyStackException;
  2.  
  3.  
  4. public class stack_array<T> implements stack{
  5.  
  6. private T[] stack;
  7. private int lastPos = 0;
  8.  
  9. private stack_array(int length){
  10. stack = (T[]) new Object[length];
  11. }
  12.  
  13. public <T> T pop( T[] a) throws EmptyStackException {
  14. if (this.lastPos < 0){
  15. throw new EmptyStackException();
  16. }
  17. else
  18. {
  19. this.lastPos --;
  20. return (T) stack[lastPos];
  21. }
  22. }
  23.  
  24. @SuppressWarnings("hiding")
  25. public void insert(T a){
  26. if (this.lastPos >= stack.length){
  27. throw new ArrayIndexOutOfBoundsException();
  28. }
  29. else
  30. {
  31. this.lastPos ++;
  32. stack[lastPos] = a;
  33. }
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement