1 Replies - 228 Views - Last Post: 03 November 2017 - 06:40 PM Rate Topic: -----

#1 Haon210012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

	    // helper linked list class
	   	private class Node {
	   		private T item;
	   		private Node next;
	   		int priority;
	   		
	   		private void LinkedQueue() {
	   			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  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12147
  • View blog
  • Posts: 45,157
  • Joined: 27-December 08

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

Posted 03 November 2017 - 06:40 PM

I have a Linked List tutorial you may find helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1