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

IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
Using java: Implement a basic doubly-linked list that implements a priority system sorting the elements that...
Using java: Implement a basic doubly-linked list that implements a priority system sorting the elements that are inserted. Sort based on the speed of the warrior. Driver code: public class LinkedListDriver { public static void main(String[] args) { LinkedList list = new SortedDoublyLinkedList(); System.out.println(list); Warrior krogg = new Warrior("Krogg", 30, 50, 200); list.insert(krogg); System.out.println(list); Warrior gurkh = new Warrior("Gurkh", 40, 45, 180); list.insert(gurkh); System.out.println(list); Warrior brynn = new Warrior("Brynn", 45, 40, 190); list.insert(brynn); System.out.println(list); Warrior dolf = new Warrior("Dolf", 20,...
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 C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
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 Java program to implement a double-linked list with addition of new nodes at the...
Write a Java program to implement a double-linked list with addition of new nodes at the end of the list. Add hard coded nodes 10, 20, 30, 40 and 50 in the program. Print the nodes of the doubly linked list.
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).
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT