Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. import Lab2.ElementNotFoundException;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Deque<T> implements DequeADT<T> {
  6. private static final int DEFAULT_SIZE = 9999;
  7. private T[] array;
  8. private int count = 0;
  9. private int first = 0;
  10. private int last = 0;
  11.  
  12. public Deque(){
  13. array =(T[])new Object[DEFAULT_SIZE];
  14. }
  15.  
  16. @Override
  17. public int size() {
  18. return count;
  19. }
  20.  
  21. @Override
  22. public boolean isEmpty() {
  23. return count == 0;
  24. }
  25.  
  26. @Override
  27. public T getFirst() throws EmptyCollectionException{
  28. if (isEmpty())
  29. throw new EmptyCollectionException("queue");
  30. return array[first];
  31. }
  32.  
  33. @Override
  34. public T getLast() {
  35. if (isEmpty())
  36. throw new EmptyCollectionException("queue");
  37. return array[last-1];
  38. }
  39.  
  40. @Override
  41. public void addFirst(T element) {
  42. T[] tempArray = (T[])new Object[DEFAULT_SIZE + 1];
  43. for (int i = 0; i < count; i ++){
  44. tempArray[i+1] = array[i];
  45. }
  46. tempArray[0] = element;
  47. array = tempArray;
  48.  
  49. count++;
  50. last++;
  51. }
  52.  
  53. @Override
  54. public void addLast(T element) {
  55. array[last] = element;
  56. count ++;
  57. last++;
  58. }
  59.  
  60. @Override
  61. public T removeFirst() {
  62. T result = getFirst();
  63. count --;
  64. first ++;
  65. return result;
  66. }
  67.  
  68. @Override
  69. public T removeLast() {
  70. T result = getLast();
  71. count --;
  72. last--;
  73. return result;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement