Question
(Data Strcture)
Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a Singly Linked-L
public static void main(String[] args) PriorityQueue Q1new PriorityQueue () Mohammed); Q1. EnQueue (3, Fahd); Q1.EnQueue
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Priority Queue using Linked List

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

class Solution
{
  
  
// Node
static class Node {
   int data;
  
   // Lower values indicate 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;
}
  
// Return the value at head
static int 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;
}
  
// 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 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;
}
  
// Driver code
public static void main(String args[])
{
   // Create a 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


Methods for enqueue and dequeue:

// Method to add an key to the queue.   
void enqueue(int key)
{
  
// Create a new LL node
QNode temp = new QNode(key);

// If queue is empty, then new node is front and rear both
if (this.rear == null)
{
this.front = this.rear = temp;
return;
}

// Add the new node at the end of queue and change rear
this.rear.next = temp;
this.rear = temp;
}

// Method to remove an key from queue.   
QNode dequeue()
{
// If queue is empty, return NULL.
if (this.front == null)
return null;

// Store previous front and move front one node ahead
QNode temp = this.front;
this.front = this.front.next;
// If front becomes NULL, then change rear also as NULL
if (this.front == null)
this.rear = null;
return temp;
}


static class ArrayQmerge{
private List<Integer> queue = new ArrayList<Integer>();

public void enqueue(int val){
queue.add(val);
}

public String toString(){
return queue.toString();
}

public void mergeQs(ArrayQmerge q){
List<Integer> mergedQ = new ArrayList<Integer>(this.queue.size() + q.queue.size());
for (int i = 0, j = 0; i < this.queue.size() || j < q.queue.size();){
if (i < this.queue.size())
mergedQ.add(this.queue.get(i++));
if (j < q.queue.size())
mergedQ.add(q.queue.get(j++));
}
this.queue = mergedQ;
}
}

Priority Queue class

public static void main(String[] args) {
PriorityQueue Q1 = new PriorityQueue();
Q1.enqueue(1,Mohammed);
Q1.enqueue(3,"Fahd");
Q1.enqueue(8,"Khaled");
Q1.enqueue(9,"Osama");
Q1.enqueue(5,"Mostafa");
PriorityQueue Q2 = new PriorityQueue();
Q2.enqueue(2,"Hany");
Q2.enqueue(5,"Mostafa");
Q2.enqueue(9,"Osama");
Q2.enqueue(10,"Marwan");
Q2.enqueue(6,"Hazem");
System.out.println(Q1);
System.out.println(Q2);
Q1.mergeQs(Q2);
System.out.println(Q1);
System.out.println(Q2);
}

static class PriorityQueue{
private List<Integer> queue = new ArrayList<Integer>();

public void enqueue(int val){
queue.add(val);
}

public String toString(){
return queue.toString();
}

public void mergeQs(PriorityQueue q){
List<Integer> mergedQ = new ArrayList<Integer>(this.queue.size() + q.queue.size());
for (int i = 0, j = 0; i < this.queue.size() || j < q.queue.size();){
if (i < this.queue.size())
mergedQ.add(this.queue.get(i++));
if (j < q.queue.size())
mergedQ.add(q.queue.get(j++));
}
this.queue = mergedQ;
}
}

Add a comment
Know the answer?
Add Answer to:
(Data Strcture) Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a...
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
  • Queues and Stacks Purpose: To review queues and stacks in Java, and to use the built-in...

    Queues and Stacks Purpose: To review queues and stacks in Java, and to use the built-in Stack class and Queue interface. The Stack class is a generic class containing these methods: public void push(E value) - pushes a new value onto the Stack public E pop() - pops the next value off of the stack and returns it; throws EmptyStackExceptionl if stack is empty public E peek() - returns the next value on the Stack but does not pop it...

  • In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and...

    In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and the Stack ADT. In addition, you will be using two different implementations for each ADT: Array Based Linked You will also be writing a driver to test your Queue and Stack implementations and you will be measuring the run times and memory use of each test case. You will also be adding some functionality to the TestTimes class that you created for Homework 1....

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • Java:Netbeans-Use the LinkedList class to simulate a queue, and use the add method to simulate the...

    Java:Netbeans-Use the LinkedList class to simulate a queue, and use the add method to simulate the enqueue, and the remove method to simulate the dequeue method for a Queue. Remember to use FIFO. Need help with 0. Add New Microchips pushMicroChip(), popMicroChip() and . To simulate this, you will create a menu option 0, which will generate 100 microchip long objects, and place them into a stack of microchips. Those microchip objects will be generated by using the System.nanotime() method.  ...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

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