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....
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...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue:...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue: public static final class LinkedQueue<T> implements QueueInterface<T> {     private Node<T> firstNode;     private Node<T> lastNode;     public LinkedQueue() {         firstNode = null;         lastNode = null;     }     @Override     public T getFront() {         if (isEmpty()) {             return null;         }         return firstNode.getData();     }     @Override     public boolean isEmpty() {         return firstNode == null;    ...
Used the next seudoucode pseudocode and implement in the next code below run in c ++...
Used the next seudoucode pseudocode and implement in the next code below run in c ++ this code What is the output of the following pseudocode, where num1 , num2 , and num3 are integer variables? num1 = 5 num2 = 1 num3 = 4 aQueue.enqueue(num2) aQueue.enqueue(num3) aQueue.dequeue() aQueue.enqueue(num1 - num2) num1 = aQueue.peek() aQueue.dequeue() num2 = aQueue.peek() aQueue.dequeue() cout << num2 << " " << num1 << " " << num3 << endl ____________________________________________________________________________________ a// Created by Frank M....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT