In: Computer Science
1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named PriorityQueue The priority queue MUST be implemented using a linked list.
2 test program
checks that a newly constructed priority queue is empty
o checks that a queue with one item in it is not empty
o checks that items are correctly entered that would go at the front of the queue
o checks that items are correctly entered that would go at the end of the queue
o checks that items are correctly entered that would go in the middle of the queue
o checks that you can enqueue several items, dequeue ALL the items, check that the queue is now empty, enqueue new items, display the queue – in other words, give your code and queue a real workout.
// PriorityQueueInterface is here
public interface QueueInterface<T> {
public void enqueue(T item); // Add to the rear of the queue.
public T dequeue(); // Delete and return the item
// at the front of the queue.
public T front(); // Return the item at the front
// of the queue, do not delete.
public boolean isEmpty(); // Check if the queue is empty.
public boolean isFull(); // Check if the queue is full.
public String toString(); // Return a printable display
// of the items in the queue.
}
//integernode class is here
public class IntegerNode implements
Comparable<IntegerNode>
{
Integer i; // The integer stored in the node.
public IntegerNode(Integer x) // The constructor initializes
{ // the instance variable i.
this.i = x;
}
/*
* compareTo() compares this objects integer to
* another (rhs - right hand side)
* objects integer value.
*
* If this node has a smaller integer value
* it has higher priority and in this sense
* is "bigger" than the rhs object and compareTo()
* returns +1.
*/
public int compareTo(IntegerNode rhs)
{
if (this.i > rhs.i) // This object has lower priority
return -1; // ... so return -1.
else if (this.i < rhs.i) // This object has higher
priority
return 1; // ... so return +1.
else // The objects have equal priority
return 0; // ... so return 0.
}
public String toString()
{ // The object's string representation
return i.toString(); // is just that of its integer.
}
}
priority queue:In computer science, a priority queue is an abstract data type similar to a regular queue or stack data structure in which each element additionally has a "priority" associated with it.
Priority Queue Using Linked List:
Operations on priority Queue:
1.push():inserts new data into a queue.
2.pop():removes the element with highest priority in queue.
3.peek()/top():gets the element with highest priority in queue without removing it.
The list is so created that the highest priority element is always at the head of the list. The list is arranged in descending order of elements based on their priority of elements in O(1) time.
To insert an element we traverse the list and find the proper position to insert the node so that the overall order of the priority queue is maintained. This makes the PUSH operation take O(N)time. The POP operation and PEEK operation are performed in constant time.
The ALGORITHM is mentioned below:
Algorithm:
PUSH(HEAD,DATA,PRIORITY)
step1:
create new node with DATA and PRIORITY
Step2:
check if HEAD has lower priority.if true follow Steps 3-4 and end.else goto Step 5.
step 3:
NEW -> NEXT = HEAD
step 4:
HEAD = NEW
step5:
Set TEMP to head of the list
step6:
While TEMP -> NEXT != NULL and TEMP ->NEXT -> PRIORITY > PRIORITY
step 7:
TEMP= TEMP -> NEXT
[END OF LOOP]
step 8:
NEW-> NEXT=TEMP ->NEXT
step 9:
TEMP ->NEXT = NEW
step 10:
End
POP(HEAD)
step 2:set the head of the list to the next node in the list. HEAD =HEAD -> NEXT.
step 3: Free the node at the head of the list
step 4:End
PEEK(HEAD):
step 1: Return HEAD -> DATA
step 2:End
Implementation of ALGORITHM:
//code to implement priority queue
import java.util.*;
class PriorityQueue
{
static class Node{
int data;
int priority;
Node next;
}
static Node node= new Node();
//function to create new node
static Node newNode(int d, int p)
{
Node temp= new Node();
temp.data=d;
temp.priority=p;
temp.next=null;
return temp;
}
//return the value at head
static int peek(Node head)
{
return (head).data;
}
//remove the element with highest priority from the list
static Node pop(Node head)
{
Node temp = head;
(head) = (head).next;
return head;
}
//function to push according to priority
static Node push(Node head, int d, int p)
{
Node start = (head);
//create new Node
Node temp = newNode(d, p);
//special case: The head of list has lesser priority that new node. so insert new node before head node and change head node.
if((head).priority > p) {
temp.next= head;
(head) = temp;
}
else
{
while (start.next != null && start.next.priority <p) {
start = start.next;
}
temp.next=start.next;
start.next=temp;
}
return head;
}
//function to check is list is empty
static int isEmpty(Node head)
{
return((head) == null)?1:0;
}
//driver code
public static void main(String args[])
{
//create priority queue
//7.4.5.6
Node pq= newNode(4,1);
pq= push(pq,5,2);
pq= push(pq,6,3);
pq=push(pq,7,0);
while(isEmpty(pq)==0){
System.out.printf("%d",peek(pq));
pq=pop(pq);
}
}
}
output:
7 4 5 6