Thursday, 26 July 2018

Priority Queue Using Doubly Linked List (Custom Implementation)


package com.vzt.Test.PQ;

public class PQUsingDLL {
public static void main(String[] vj) {
PriorityQUsingDLL obj = new PriorityQUsingDLL();

System.out.println("INSERTING...");
obj.enqueue(2, 3);
obj.enqueue(3, 4);
obj.enqueue(4, 5);
obj.enqueue(5, 2);
obj.enqueue(6, 7);
obj.enqueue(1, 1);

obj.displayQ();

System.out.println("REMOVING...");
obj.dequeue();
obj.dequeue();

obj.displayQ();
}
}

class PriorityQUsingDLL {
private DoublyLinkList list;

public PriorityQUsingDLL() {
list = new DoublyLinkList();
}

public void enqueue(int x, int p) {
list.insert(x, p);
}

public void dequeue() {
list.remove();
}

public void displayQ() {
System.out.println("PRINTING...");
list.display();
}
}

class DoublyLinkList {

private Node first = null;
private Node last = null;

public DoublyLinkList() {
first = null;
last = null;
}

public boolean isEmpty() {
return (first == null);
}

public void insert(int n, int p) {
Node newNode = new Node(n, p);
if (first == null) {
first = newNode;
last = newNode;
} else {
if (p <= first.priority) {
newNode.next = first;
first.prev = newNode.next;
first = newNode;
}
else if (p > last.priority) {
last.next = newNode;
newNode.prev = last.next;
last = newNode;
}
else {
Node start = first.next;
while (start.priority > p)
start = start.next;
start.prev.next = newNode;
newNode.next = start.prev;
newNode.prev = start.prev.next;
start.prev = newNode.next;
}
}
}

public Node remove() {
if (first == null) {
last = null;
return null;
}
Node temp = first;
first = first.next;
temp.displayNode();
return temp;
}

public void display() {
Node current = first;

while (current != null) {
current.displayNode();
current = current.next;
}

System.out.println(" ");
}
public int peek() {
return first.info;
}
}

class Node {
int info;
int priority;
Node prev, next;

Node(int x, int p) {
info = x;
priority = p;
prev = null;
next = null;
}

public void displayNode() {
System.out.println("Data = " + info);
}
}

Priority Queue Using Linked List (Custom Implementation)


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);
    }

}