Question

Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regular queue data structure, bu

Data Structure

Java code

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Java code to implement Priority Queue
// using Linked List
import java.util.* ;

class Solution
{
  
  
// Node
static class Node {
    Character data;
  
    // Lower values indicate higher priority
    int priority;
  
     Node next;
  
}

static Node node = new Node();
  
// Function to Create A New Node
static Node newNode(Character d, int p)
{
    Node temp = new Node();
    temp.data = d;
    temp.priority = p;
    temp.next = null;
  
    return temp;
}
  
// Return the value at head
static Character peek(Node head)
{
    return (head).data;
}
  
// Removes the element with the
// highest priority form the list
static Node pop(Node head)
{
    Node temp = head;
    (head) = (head).next;
    return head;
}  

// size method for counting size of queue
static int size(Node head)
{
    int cnt=0;
    Node start =(head);
    while(start!=null)
    {
        cnt=cnt+1;
        start=start.next;
    }
    return cnt;
}

//getHighestPriority() will return item which have highest priority
static Character getHighestPriority(Node head)
{
    Node start =(head);
    Node temp=(start);
    while(start.next!=null)
    {
        temp=start;
        start=start.next;
    }
    start=temp.next;
    return start.data;
}

//deleteHighestPriority() for removing maximum priority argument
static Node deleteHighestPriority(Node head)
{
    Node start =(head);
    Node temp=(start);
    while(start.next!=null)
    {
        temp=start;
        start=start.next;
    }
    start=temp.next;
    Character c = getHighestPriority(head);
    temp.next=null;
    System.out.println("\n\nRemove max priority argument");
    System.out.println("Removed element is : " + c);
    return head;
}
// insert() method for insert items according to priority
static Node insert(Node head, Character d, int p)
{
    Node start = (head);
  
    // Create new Node
    Node temp = newNode(d, p);
  
    // Special 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;
}

// mothod printqueue for printing priority queue
static void printqueue(Node pq)
{
    int cnt=0;
    Node temp=pq;
    while (isEmpty(temp)==0&&cnt<5) {
        System.out.print(peek(temp)+" ");
        temp=pop(temp);
        cnt++;
    }
}
  
// 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 a Priority Queue
  
    Character c;
    //inserting arguments P,Q,E
    System.out.println("Inserting arguments P,Q,E");
    Node pq = newNode('P',((int)'P'-(int)'A') );
    pq =insert(pq, 'Q',((int)'Q'-(int)'A' ));
    pq =insert(pq, 'E',((int)'E'-(int)'A') );
  
    System.out.println("Now Queue has size is "+size(pq)+" and Queue is: ");
    printqueue(pq);
  
    pq=deleteHighestPriority(pq);
    System.out.println("Now Queue has size is "+size(pq)+" and Queue is: ");
    printqueue(pq);
  
    System.out.println("\n\nInserting arguments X,A,M");
    pq =insert(pq, 'X',((int)'X'-(int)'A' ));
    pq =insert(pq, 'A',((int)'A'-(int)'A' ));
    pq =insert(pq, 'M',((int)'M'-(int)'A' ));
    System.out.println("Now Queue has size is "+size(pq)+" and Queue is: ");
    printqueue(pq);
  
    pq=deleteHighestPriority(pq);
    System.out.println("Now Queue has size is "+size(pq)+" and Queue is: ");
    printqueue(pq);
  
    System.out.println("\n\nInserting arguments P,L,E");
    pq =insert(pq, 'P',((int)'P'-(int)'A' ));
    pq =insert(pq, 'L',((int)'L'-(int)'A') );
    pq =insert(pq, 'E',((int)'E'-(int)'A') );
    System.out.println("Now Queue has size is "+size(pq)+" and Queue is: ");
    printqueue(pq);
  
  
    pq=deleteHighestPriority(pq);
    System.out.println("Now Queue has size is "+size(pq)+" and Queue is: ");
    printqueue(pq);
}
}

Add a comment
Know the answer?
Add Answer to:
Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regu...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the...

    In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the element of the heap with the smallest key is the root of the binary tree. On the other hand, a max-heap has as root the element with the biggest key, and the relationship between the keys of a node and its parent is reversed of that of a min-heap. We also discussed an array-based implementation of heaps. In this assignment, your task is to...

  • Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the...

    Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the max-heap data structure using an integer array of 10 cells. (Do not use Java in-built PriorityQueue class.) [In a max-heap, the root node and the intermediate node vales are always greater than their children.] First, take 10 integer values from the user and insert them in the max-priority queue. Then print the elements of the queue. After that, delete two elements from the queue...

  • A priority queue is a collection of items each having a priority. A priority queue supports three...

    A priority queue is a collection of items each having a priority. A priority queue supports three fundamental operations. You can ask a priority queue whether it is empty. You can insert an item into the priority queue with a given priority. You can remove the item from the priority queue that has the smallest priority. For example, suppose that you start with an empty priority queue and imagine performing the following steps. Insert item "one" with priority 10. Insert...

  • JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of...

    JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of this document) that specifies the protocols for a priority queue with generic values and priorities, where the priorities are discretely ordered rather than comparable. Implement the class ListofQueuesPQ implements PriorityQueue to fulfill the requirements of the interface (see the Javadoc in the interface’s source code). Use an ListofQueues in order to fulfill the needs of the interface. You will need to make sub-classes within...

  • JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of...

    JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of this document) that specifies the protocols for a priority queue with generic values and priorities, where the priorities are discretely ordered rather than comparable. Implement the class ArrayListPQ implements PriorityQueue to fulfill the requirements of the interface (see the Javadoc in the interface’s source code). Use an ArrayList in order to fulfill the needs of the interface. For each constructor and method in your...

  • In this lab, using C++, you will create an abstract data type, using a doubly-linked circular...

    In this lab, using C++, you will create an abstract data type, using a doubly-linked circular structure to store the values and links. You must create it by your own and not use any existing containers. You will need a QueueNode. You can use a struct for this, as each node will not have any associated functions. It will have members for data, and the next and previous pointers. Data will be positive integers. There will be no NULL pointers....

  • Part A is done, I do not know how to start these steps.. "In computer science,...

    Part A is done, I do not know how to start these steps.. "In computer science, an abstract data type (ADT) is a mathematical model for data types where a data type is defined by its behavior (semantics) from the point of view of a user of the data, specifically in terms of possible values, possible operations on data of this type, and the behavior of these operations." (Source: "Abstract Data Types." Wikipedia. Wikimedia Foundation, 2 Nov. 2016. Web. 9...

  • Queues This programming exercise introduces the Queue data structure. The students must create all the necessary...

    Queues This programming exercise introduces the Queue data structure. The students must create all the necessary methods for the queue and use the queue in a Java program. Step 1 - Create a new project in Netbeans Use the following naming convention: “Module4_Lastname_Assignment1”. Step 2 - Build a solution You have been asked to create a customer service program for a new phone store that will be opening soon. Since this company anticipates being the agent for a rising new...

  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

  • ArrayQueue

    Implement the ArrayQueue classIn the ‘Queues’ lecture, review the ‘Introduce next lab’ section.  See here that we can use a circular array to implement the queue data structure.  You must write a class named ArrayQueue that does this.ArrayQueue will be a generic class, that implements our generic QueueInterface interface.  This demonstrates the Java interface feature, where we have already implemented queue dynamically, using the LinkedQueue class covered during the lecture.Many classes are given to youDownload and unzip the Circular array project from Canvas, ‘Queues’ module, Example programs.  Open the project in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT