package com.vzt.Test.PQ;
public class PQUsingLL {
public static void main(String[] vj) {
PriorityQ obj = new PriorityQ();
System.out.println("INSERTING...");
obj.enqueue("A",1);
obj.enqueue("B",2);
obj.enqueue("C",3);
obj.enqueue("D",4);
obj.displayList();
System.out.println("REMOVING...");
obj.dequeue();
obj.dequeue();
obj.dequeue();
obj.dequeue();
}
}
class PriorityQ {
private LinkList list;
public PriorityQ() {
list = new LinkList();
}
public void enqueue(String x, int p) {
list.insert(x, p);
}
public void dequeue() {
list.remove();
}
public void displayList() {
System.out.println("PRINTING...");
list.display();
}
}
class LinkList {
private OneNode first;
public LinkList() {
first = null;
}
public boolean isEmpty() {
return (first == null);
}
public void insert(String x, int p) {
OneNode newNode = new OneNode(x, p);
OneNode previous = null;
OneNode current = first;
while (current != null && p > current.priority) {
previous = current;
current = current.next;
}
if (previous == null) {
newNode.next = first;
first = newNode;
}
else {
previous.next = newNode;
newNode.next = current;
}
}
public OneNode remove() {
if(null == first) {
return null;
}
OneNode temp = first;
first = first.next;
temp.displayNode();
return temp;
}
public void display() {
OneNode current = first;
while (current != null) {
current.displayNode();
current = current.next;
}
System.out.println(" ");
}
public String peek() {
return first.info;
}
}
class OneNode {
String info;
int priority;
OneNode next;
public OneNode(String x, int p) {
info = x;
priority = p;
next = null;
}
public void displayNode() {
System.out.println("Data = " + info);
}
}
This comment has been removed by the author.
ReplyDelete