Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Lab2.ElementNotFoundException;
- import java.util.Arrays;
- public class Deque<T> implements DequeADT<T> {
- private static final int DEFAULT_SIZE = 9999;
- private T[] array;
- private int count = 0;
- private int first = 0;
- private int last = 0;
- public Deque(){
- array =(T[])new Object[DEFAULT_SIZE];
- }
- @Override
- public int size() {
- return count;
- }
- @Override
- public boolean isEmpty() {
- return count == 0;
- }
- @Override
- public T getFirst() throws EmptyCollectionException{
- if (isEmpty())
- throw new EmptyCollectionException("queue");
- return array[first];
- }
- @Override
- public T getLast() {
- if (isEmpty())
- throw new EmptyCollectionException("queue");
- return array[last-1];
- }
- @Override
- public void addFirst(T element) {
- T[] tempArray = (T[])new Object[DEFAULT_SIZE + 1];
- for (int i = 0; i < count; i ++){
- tempArray[i+1] = array[i];
- }
- tempArray[0] = element;
- array = tempArray;
- count++;
- last++;
- }
- @Override
- public void addLast(T element) {
- array[last] = element;
- count ++;
- last++;
- }
- @Override
- public T removeFirst() {
- T result = getFirst();
- count --;
- first ++;
- return result;
- }
- @Override
- public T removeLast() {
- T result = getLast();
- count --;
- last--;
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement