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

I want you to see if my answer is correct. Thank you in advance! Question: 6. Im

ID: 3916766 • Letter: I

Question

I want you to see if my answer is correct. Thank you in advance!

Question:

6. Imagine using a doubly linked list to implement the queue ADT for values of type int. Skeleton code is provided below. Implement the enqueue and dequeue methods. Instead of using the methods of the List ADT, manipulate the nodes directly. You have access to the head and tails nodes directly. This doubly linked list uses sentinel nodes and you can assume that head and tail have already been initialized properly.

class LinkedListQueue {

private static class Node {

Node prev;

Node next;

int data;

}

Node head;

Node tail;

public enqueue(int x) { // write this};

public int dequeue() { // write this};

}

My answer:

public void enqueue(int x){

Node newNode = new Node();

newNode.data = x;

newNode.prev = tail.prev;

newNode.next = tail;

tail.prev.next = newNode;

tail.prev = newNode;

} // end of enqueue

public int dequeue(){

if (head.next == null){

System.out.println("Nothing in queue");

return null;

}else{

int value = tail.prev.data;

tail.prev.prev.next = tail;

tail.prev = tail.prev.prev;

return value;

}

}//end of dequeue

Explanation / Answer

If you have any doubts, please give me comment...

public void enqueue(int x){
Node newNode = new Node();
newNode.data = x;
newNode.prev = tail'
newNode.next = null;
if(tail==null){
tail = newNode;
head = newNode;
}
else
tail.next = newNode;
} // end of enqueue

public int dequeue(){
if (head == null){
System.out.println("Nothing in queue");
return null;
}else{
int value = head.data;
head = head.next;
return value;
}
}//end of dequeue

enqueue: Insert element at last

dequeue: delete element at first

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