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

Hello! I am searching for help with this code. It\'s for java. I am stuck so hel

ID: 3807689 • Letter: H

Question

Hello! I am searching for help with this code. It's for java. I am stuck so help would be greatly appreciated. If you could leave comments in the code so I can get an idea of whats going on would be great. Thank you in advance!

Implement a referenced based Deque. To accomplish this, you will create first create an inner (i.e. inside the Deque class) class Node. The Node class will have the attributes:

T item

Node next

You will also create a class Deque<T> that has the following attributes:

Node head

Node tail

int nrItems

Also, it will have the following methods:

A constructor that creates an empty deque.

void addFirst(T item) - adds at the head

T removeFirst()   - removes from the head

T getFirst()       - returns the element at the head

void addLast(T item)   - adds at the tail

T removeLast()   - removes from the tail

T getLast()       - returns the element at the tail

int size()       - returns the number of items in the deque

boolean isEmpty()   - true if empty false otherwise

String toString()   - returns a String representation of the elements in the deque

void clear()       - empties the deque.

You will also create a main method that creates an object of the class Deque. You will:

fill it in with 20 random integers between 0 and 100.

Display it.

display a menu to the user with the following options:

add at beginning

add at end

remove from beginning

remove from end

display beginning

display end

display

clear

exit

Be sure to handle the special cases when the Deque has zero or one nodes!

Explanation / Answer

/*Node.java */
import java.util.*;

class Node
{
protected int data;
protected Node link;

/* Constructor */
public Node()
{
   /*Initially data and link will be null .. here link holds info of next node*/
link = null;
data = 0;
}
/* Constructor */
public Node(int d,Node n)
{
  
data = d;
link = n;
}
/* link to next Node */
public void setLink(Node n)
{
link = n;
}
/* Assign data to current Node */
public void setData(int d)
{
data = d;
}
/* Get link to next node */
public Node getLink()
{
return link;
}
/*Get data from current Node */
public int getData()
{
return data;
}
}

/*Dequeue.java*/

import java.util.*;

class Dequeue
{
private Node first, last;
private int size;

/* Constructor */
public Dequeue()
{
first = null;
last = null;
size = 0;
}
/* Check if queue is empty */
public boolean isEmpty()
{
return first == null;
}
/* Get the size of the queue */
public int getSize()
{
return size;
}
/* Clear dequeue logic is if you set first and last as null all the data in the queue will be lost */
public void clear()
{  
   first = null;
last = null;
size = 0;
}
/* Insert an element at First */
public void addFirst(int val)
{
   //create a node with value and link as null
Node nptr = new Node(val, null);
size++ ;
/*if first is null means no elements in queue then nothing need to be done. Also copy the node to last element since it is the first and last*/
if (first == null)
{
first = nptr;
last = first;
}
/*if not get the addr of first element and add it to new element ... replace first element with new node*/
else
{
nptr.setLink(first);
first = nptr;
}
}
/* Insert an element at end */
public void addLast(int val)
{
   /*this is similar to add first*/
Node nptr = new Node(val,null);
size++ ;
if (last == null)
{
last = nptr;
first = last;
}
else
{
last.setLink(nptr);
last = nptr;
}
}
/* Remove front element from the queue */
public int removeFirst()
{
   /*check if there is elements in a queue if no we can throw and exception */
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
/* Get the first element and its link and make it as null*/
Node ptr = first;
first = ptr.getLink();

if (first == null)
last = null;
size-- ;

return ptr.getData();
}
/* Remove last element from the queue */
public int removeLast()
{
   /*Similar to remove last instead of first get the last*/
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
int ele = last.getData();
Node s = first;
Node t = first;
while (s != last)
{
t = s;
s = s.getLink();
}
last = t;
last.setLink(null);
size --;

return ele;
}
/* Return first element of the queue */
public int getFirst()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return first.getData();
}
/* Return last element of the queue */
public int getLast()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return last.getData();
}
/* Display queue */
public void display()
{
System.out.print(" Dequeue = ");
if (size == 0)
{
System.out.print("Empty ");
return ;
}
Node ptr = first;
while (ptr != last.getLink() )
{
System.out.print(ptr.getData()+" ");
ptr = ptr.getLink();
}
System.out.println();
}
}

/*DequeueTest.java*/
import java.util.Scanner;

public class DequeueTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of class Dequeue */   
Dequeue dq = new Dequeue();
/* Perform Dequeue Operations */
System.out.println("Dequeue Test ");
char ch;
do
{
System.out.println(" Dequeue Operations");
System.out.println("1. Add at first");
System.out.println("2. Add at Last");
System.out.println("3. delete at first");
System.out.println("4. delete at Last");
System.out.println("5. Node at first");
System.out.println("6. Node at rear");
System.out.println("7. size");
System.out.println("8. check empty");
System.out.println("9. clear");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
dq.addFirst( scan.nextInt() );
break;
case 2 :
System.out.println("Enter integer element to insert");
dq.addLast( scan.nextInt() );
break;   
case 3 :
try
{
System.out.println("Removed Element = "+ dq.removeFirst());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 4 :
try
{
System.out.println("Removed Element = "+ dq.removeLast());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;   
case 5 :
try
{
System.out.println("Peek Element = "+ dq.getFirst());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 6 :
try
{
System.out.println("Peek Element = "+ dq.getLast());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;   
case 7 :
System.out.println("Size = "+ dq.getSize());
break;
case 8 :
System.out.println("Empty status = "+ dq.isEmpty());
break;
case 9 :
System.out.println(" Dequeue Cleared ");
dq.clear();
break;   
default :
System.out.println("Wrong Entry ");
break;
}
/* display dequeue */
dq.display();

System.out.println(" To Continue.. y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}

Sample output

Dequeue Test


Dequeue Operations
1. Add at first
2. Add at Last
3. delete at first
4. delete at Last
5. Node at first
6. Node at rear
7. size
8. check empty
9. clear
1
Enter integer element to insert
12

Dequeue = 12

To Continue.. y or n)

y

Dequeue Operations
1. Add at first
2. Add at Last
3. delete at first
4. delete at Last
5. Node at first
6. Node at rear
7. size
8. check empty
9. clear
2
Enter integer element to insert
13

Dequeue = 12 13

To Continue.. y or n)

y

Dequeue Operations
1. Add at first
2. Add at Last
3. delete at first
4. delete at Last
5. Node at first
6. Node at rear
7. size
8. check empty
9. clear
3
Removed Element = 12

Dequeue = 13

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