Question

Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists....

Write in Java (Not Javascript)

Provide an implementation of priority queue using double-ended doubly linked lists. Recall that double-ended means keeping first and last references and doubly linked feature allows us to go backwards, using a prev reference at each Link. Also, note that the greater the number, the lower the priority. For instance, 2 is of higher priority compared to 5.

Specifically, write a class LinkedListPriorityQ which implements the priority queue methods:

  • boolean isEmpty()
  • void enqueue(int item)
  • int dequeue()
  • int peek()

You need an implementation for the underlying linked list class. For this part, you can use any relevant code from lecture slides.

Your file to be submitted to Gradescope should be named LinkedListPriorityQ.java. and here is the skeleton:

class Link

{

    public int data; // assuming integer data

    public Link next; // reference to the next Link

    public Link prev; // reference to the previous Link

    //--------------------------------------------------------------

    // Constructor

    public Link(int data)

    {

        this.data=data;

        next=null;

        prev=null;

    }

}

class LinkedList

{

   

    /* you should implement this linked list class(can take parts of code from lecture slides) and be careful what type of linked list is suitable to be used for priority Queue */

}

public class LinkedListPriorityQ {

   //implement priority queue using the linked list class

}

Homework Answers

Answer #1

public int data;
public Node next;
public Node prev;

public Node(int data) {
this.data = data;
next = null;
prev = null;
}
}

class LinkedList {
public Node head;

public LinkedList(int data) {
Node a = new Node(data);
head = a;
}

public void traversal() {
Node temp = head; //temporary pointer to point to head

while(temp != null) { //iterating over linked list
System.out.print(temp.data+"\t");
temp = temp.next;
}

System.out.println("");
}

//new node before head
public void insertAtFront(Node n) {
n.next = head;
head.prev = n;
head = n;
}

//insert new node at last
public void insertAtTail(Node n) {
Node temp = head;

while(temp.next != null) {
temp = temp.next;
}

temp.next = n;
n.prev = temp;
}

//function to insert a node after a node
public void insertAfter(Node n, Node a) {
n.next = a.next;
a.next.prev = n;
a.next = n;
n.prev = a;
}

//function to delete
public void del(Node a) {
if(a.prev != null) { //node is not head
a.prev.next = a.next;
}
else { //node a is head
head = a.next;
}

if(a.next != null) {
a.next.prev = a.prev;
}
}
}
class LinkedListPriorityQ
{
private Node head;
private Node tail;
static class Node{
//data
int i;
// next node in the list
Node next;
// previous node in the list
Node prev;
Node(int i){
this.i = i;
}
public void displayData(){
System.out.print(i + " ");
}
}
// constructor
public LinkedListDeque(){
this.head = null;
this.tail = null;
}

public boolean isEmpty(){
return head == null;
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your...
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your Can class. You need to create a driver that makes several Can objects and places them in alphabetical order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. NOT USE your language's Library List . You will receive zero points. Write a LinkedList class. Include...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class...
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class and a Node class (please name your classes exactly ​as I did here). Please follow the below specifications for the two classes. Node.cpp ● This must be a generic class. ● Should contain a generic member variable that holds the nodes value. ● Should contain a next and prev Node* as denoted here. ● All member variables should be private. ● Use public and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT