# Implement Priority Queue with A Linked List (Java)

Page 1 of 1

## 1 Replies - 745 Views - Last Post: 03 November 2017 - 06:40 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407379&amp;s=34eaa6294a6398744b239f0dab2eae12&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Haon210012

Reputation: 0
• Posts: 1
• Joined: 03-November 17

# Implement Priority Queue with A Linked List (Java)

Posted 03 November 2017 - 06:21 PM

I am trying to implement a priority queue with a linked list in Java. It is for an assignment or I would NOT be using a linked list. I made an array based version but I find linked lists very confusing. I am using generic types with an interface. I have a basic linked list queue and I am try to add the priority aspect to dequeue as in it will dequeue based on priority but I have no idea where to start when it comes to a linked list. Here is what I have so far:

``` import java.util.*;
public class ListPriorityQueue<T> implements myQueue<T>{
private int n;         // number of elements on queue
private Node first;    // beginning of queue
private Node last;     // end of queue

private class Node {
private T item;
private Node next;
int priority;

first = null;
last  = null;
n = 0;
}
}

public ListPriorityQueue() {

public void enqueue(T item, int priority) {
Node oldlast = last;
last = new Node();
last.item = item;
last.next = null;

if (isEmpty()) {
first = last;
}
else {
oldlast.next = last;
}
n++;

}

public T dequeue() {
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
T item = first.item;
first = first.next;
n--;
if (isEmpty()) last = null;   // to avoid loitering
return item;
}

public T peek() {
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
return first.item;
}

public void display() {
System.out.print(this);
}

public int size() {
return n;
}

public boolean isFull() {
return false;
}

public boolean isRegular() {
// TODO Auto-generated method stub
return false;
}

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

}

```

Any suggestions or places to start? I know its a mess

Is This A Good Question/Topic? 0

## Replies To: Implement Priority Queue with A Linked List (Java)

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12243
• Posts: 45,332
• Joined: 27-December 08

## Re: Implement Priority Queue with A Linked List (Java)

Posted 03 November 2017 - 06:40 PM