Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is java Dequeue. A double-ended queue or deque (pronounced \"deck\") is a g

ID: 3588346 • Letter: T

Question

This is java

Dequeue. A double-ended queue or deque (pronounced "deck") is a generalization of a stack and a queue that supports adding and removing items from either the front or the back of the data structure. Create a generic data type Deque that implements the following API:

Corner cases. Throw a java.lang.NullPointerException if the client attempts to add a null item; throw a java.util.NoSuchElementException if the client attempts to remove an item from an empty deque; throw a java.lang.UnsupportedOperationException if the client calls the remove() method in the iterator; throw a java.util.NoSuchElementException if the client calls the next() method in the iterator and there are no more items to return.

Performance requirements. Your deque implementation must support each deque operation (including construction) in constant worst-case time and use space linear in the number of items currently in the deque. Additionally, your iterator implementation must support each operation (including construction) in constant worst-case time. Hint: you'll want to use a doubly-linked list.

Explanation / Answer

Given below is the implementation and the main() method will allow you to test it on the run. Sample output is shown. Hope it helps. If it did, please don't forget to rate it. Thank you.


import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Deque<Item> implements Iterable<Item> {
class Node
{
Item data;
Node prev;
Node next;
Node(Item t)
{
this(t, null, null);
}
Node(Item t, Node pr, Node nx)
{
data = t;
setPrevious(pr);
setNext(nx);
}
void setPrevious(Node p)
{
prev = p;
if(p != null)
p.next = this;
}
void setNext(Node n)
{
next = n;
if(n != null)
n.prev = this;
}
}
private Node head, tail;
private int size;
public Deque() // construct an empty deque
{
head = null;
tail = null;
size = 0;
}
public boolean isEmpty() // is the deque empty?
{
return size == 0;
}
public int size() // return the number of items on the deque
{
return size;
}
public void addFirst(Item item) // add the item to the front
{
if(item == null)
throw new NullPointerException("null not allowed");
Node n = new Node(item, null, head);
head = n;
if(isEmpty())
tail = n;
size++;
}
public void addLast(Item item) // add the item to the end
{
if(item == null)
throw new NullPointerException("null not allowed");

Node n = new Node(item, tail, null);
tail = n;
if(isEmpty())
head = n;
size++;
}

public Item removeFirst() // remove and return the item from the front
{
if(isEmpty())
throw new NoSuchElementException("deque is empty");

Item val = head.data;
head = head.next;
size --;
if(head != null)
head.setPrevious(null);
else //now queue is empty
tail = null;
return val;
}

public Item removeLast() // remove and return the item from the end
{
if(isEmpty())
throw new NoSuchElementException("deque is empty");

Item val = tail.data;
tail = tail.prev;
size --;
if(tail != null)
tail.setNext(null);
else //now queue is empty
head = null;
return val;
}


public Iterator<Item> iterator() // return an iterator over items in order from front to end
{
return new DequeIterator();
}


class DequeIterator implements Iterator<Item>
{
Node current;
public DequeIterator() {
current = head;
}
@Override
public boolean hasNext() {
return current != null;
}
@Override
public Item next() {
if(!hasNext())
throw new NoSuchElementException("deque is empty");
Item val = current.data;
current = current.next;
return val;
}
public void remove() {
throw new UnsupportedOperationException();
}

}


private static void display(Deque d , String separator)
{
Iterator it = d.iterator();
while(it.hasNext())
System.out.print(it.next() + separator);
System.out.println();
}
public static void main(String[] args) // unit testing (required)
{
Deque<Integer> deq = new Deque<>();
int num;
int choice = 0;
Scanner keybd = new Scanner(System.in);

while(choice != 6)
{
System.out.println("1. Add First");
System.out.println("2. Add Last");
System.out.println("3. Remove First");
System.out.println("4. Remove last");
System.out.println("5. Display");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
choice = keybd.nextInt();
try
{
switch(choice)
{
case 1:
System.out.print("Enter number to insert in first: ");
num = keybd.nextInt();
deq.addFirst(num);
System.out.println(num + " added to first");
break;
case 2:
System.out.print("Enter number to insert in last: ");
num = keybd.nextInt();
deq.addLast(num);
System.out.println(num + " added to last");
break;

case 3:
num = deq.removeFirst();
System.out.println(num + " removed from first");
break;
case 4:
num = deq.removeLast();
System.out.println(num + " removed from last");
break;
case 5:
display(deq, " ");
break;
case 6:
break;
default:
System.out.println("Invalid choice");
}
}
catch(Exception e)
{
System.out.println("Exception occured: " + e.getMessage());
}
}
}
}

output

1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 1
Enter number to insert in first: 2
2 added to first
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 1
Enter number to insert in first: 4
4 added to first
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 1
Enter number to insert in first: 6
6 added to first
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 5
6 4 2
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 2
Enter number to insert in last: 8
8 added to last
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 2
Enter number to insert in last: 10
10 added to last
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 2
Enter number to insert in last: 12
12 added to last
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 5
6 4 2 8 10 12
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 3
6 removed from first
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 4
12 removed from last
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 5
4 2 8 10
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 3
4 removed from first
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 3
2 removed from first
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 3
8 removed from first
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 5
10
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 3
10 removed from first
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 5

1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 3
Exception occured: deque is empty
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 4
Exception occured: deque is empty
1. Add First
2. Add Last
3. Remove First
4. Remove last
5. Display
6. Exit
Enter your choice: 6

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote