Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solver
- {
- public static void main(String args[])
- {
- }
- }
- class Node
- {
- int data;
- Node next;
- public Node(int d)
- {
- data = d;
- }
- }
- class pQueue // the smallest element is the highest priority
- {
- Node front;
- public void push(int d)
- {
- Node temp = new Node(d);
- if (isEmpty() || d <= front.data)
- {
- temp.next = front;
- front = temp;
- return;
- }
- Node prev = front, curr = front.next;
- while (curr != null)
- {
- if (d <= curr.data)break;
- prev = curr;
- curr = curr.next;
- }
- temp.next = prev.next;
- prev.next = temp;
- return;
- }
- public Node pop()
- {
- if (isEmpty())
- {
- System.out.println("Can't Pop, The Queue is Empty");
- return null;
- }
- Node temp = front;
- front = front.next;
- return temp;
- }
- public Node peek()
- {
- if (isEmpty())
- {
- System.out.println("The Queue is Empty");
- }
- return front;
- }
- public void clear()
- {
- while (isEmpty() == false)
- {
- pop();
- }
- System.out.println("Done");
- return;
- }
- public void display()
- {
- if (isEmpty())
- {
- System.out.print("The Queue is Empty");
- }
- Node curr = front;
- while (curr != null)
- {
- System.out.print(curr.data + " ");
- curr = curr.next;
- }
- System.out.println();
- return;
- }
- public boolean isEmpty()
- {
- return front == null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment