Question

In: Computer Science

write a java program to Implement a Priority Queue using a linked list. Include a main...

write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.

Solutions

Expert Solution

import java.util.* ;

public class Priority_Queue{

// java program to implement Priority_Queue
// using Linked_List

// Node
static class Node {
    int data;
  
    // Lower the values indicates the higher priority
    int priority;
  
     Node next;
  
}

static Node node = new Node();
  
// function to create A New_Node
static Node newNode(int d, int p)
{
    Node temp = new Node();
    temp.data = d;
    temp.priority = p;
    temp.next = null;
  
    return temp;
}
  
// returns the value at head
static int peek(Node head)
{
    return (head).data;
}
  
// function to remove node according to priority

static Node Dequeue(Node head)
{
    Node temp = head;
    (head) = (head).next;
    return head;
}  
  
// function to insert node according to priority
static Node Enqueue(Node head, int d, int p)
{
    Node start = (head);
  
    // Create new Node
    Node temp = newNode(d, p);
  
    // Rare case: The head of list has lesser
    // priority than new node. So insert new
    // node before head node and change head node.
    if ((head).priority > p) {
  
        // insert New_Node before head
        temp.next = head;
        (head) = temp;
    }
    else {
  
        // Traverse the list and find a
        // position to insert new node
        while (start.next != null &&
               start.next.priority < p) {
            start = start.next;
        }
  
        // Either at the ends of the list
        // or at required position
        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;
}
    //Main Function
    public static void main(String []args){
    Node pq = newNode(4, 1);
    // Enqueue operations
    pq =Enqueue(pq, 5, 2);
    pq =Enqueue(pq, 6, 3);
    pq =Enqueue(pq,23424,5);
    pq =Enqueue(pq, 7, 0);
  
    // functions dequeue's the priority_queue until its empty
    while (isEmpty(pq)==0) {
        System.out.printf("%d ", peek(pq));
        //Dequeue operations
        pq=Dequeue(pq);
    }     
    }

}

---------summary--------

what does main function do?

1.Creates a new node first for priority queue .

2.Performs some Enqueue operations.

3.Dequeue's the Priority queue until it's empty.

4.Print's the value of highest priority while dequeueing the queue.


Related Solutions

write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
Write a C program to implement the priority queue with the operations insert and extractmax. Sample...
Write a C program to implement the priority queue with the operations insert and extractmax. Sample : ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 2 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 4 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 6 enter any key to go to main menu...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and a method to get all the values of the queue. (This is not writing a file implementing the java class PriorityQueue, but rather you are writing a program that is a priority queue).
C++ Program 2–Implement a Priority queue using a SORTED list. Use Quicksort after adding a new...
C++ Program 2–Implement a Priority queue using a SORTED list. Use Quicksort after adding a new node.Example of quick sort below. Adopt to your program. #include <iostream> voidquickSort(inta[], intfirst, intlast); intpivot(inta[], intfirst, intlast); voidswap(int& a, int& b); voidswapNoTemp(int& a, int& b); voidprint(intarray[], constint& N); usingnamespacestd; intmain() { inttest[] = { 7, -13, 1, 3, 10, 5, 2, 4 }; intN = sizeof(test)/sizeof(int); cout << "Size of test array :"<< N << endl; cout << "Before sorting : "<< endl; print(test,...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO)....
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO). Use the driver to then test each of the methods. Simply download it and place it with the other source files. Create a class GenLLQueue which has the following: Internal class ListNode which contains: Instance variable data of type T Instance variable link of type ListNode Default constructor that sets both instance variables to null Instance Variables head which is of type ListNode which...
#include <iostream> #include <queue> //This contains the STL's efficient implementation of a priority queue using a...
#include <iostream> #include <queue> //This contains the STL's efficient implementation of a priority queue using a heap using namespace std; /* In this lab you will implement a data structure that supports 2 primary operations: - insert a new item - remove and return the smallest item A data structure that supports these two operations is called a "priority queue". There are many ways to implement a priority queue, with differernt efficiencies for the two primary operations. For this lab,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT