Question: //Implement a Java Deque class using Generics and a circular array. //Need help on implementing on iterator import java.util.Deque; import java.util.NoSuchElementException; public class deque implements

//Implement a Java Deque class using Generics and a circular array. //Need help on implementing on iterator

import java.util.Deque; import java.util.NoSuchElementException;

public class deque implements Deque {

private Node head;

private Node tail;

private int size;

public deque() {

head = null;

tail = null;

size = 0;

}

@Override // void addFirst(E e)-Inserts the specified element at the front of this deque. public void addFirst(E element) { Node node = new Node(element);

if (head == null) {

head = node;

tail = node;

} else {

node.setNext(head);

head.setPrev(node);

head = node;

}

size++;

}

@Override //void addLast(E e)-Inserts the specified element at the end of this deque. public void addLast(E element) {

Node node = new Node(element);

if (tail == null) {

head = node;

tail = node;

} else {

tail.setNext(node);

node.setPrev(tail);

tail = node;

}

size++;

}

@Override //E removeFirst()-Retrieves and removes the first element of this deque. public E removeFirst() { if (head == null) {

throw new NoSuchElementException("Deque is empty!");

}

Item item = head.getItem();

head = head.getNext();

if (head == null) {

tail = null;

} else {

head.setPrev(null);

}

size--;

return item; }

@Override //E removeLast()-Retrieves and removes the last element of this deque. public E removeLast() { if (tail == null) {

throw new NoSuchElementException("Deque is empty!");

}

Item item = tail.getItem();

tail = tail.getPrev();

if (tail == null) {

head = null;

} else {

tail.setNext(null);

}

size--;

return item;

}

//int size()-Returns the number of elements in this deque. public int size() { return size;

}

//Iterator-iterator() Returns an iterator over the elements in this deque in proper sequence. public static

public static void main(String[] args) {

deque deque = new deque();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!