Thursday, 26 July 2018

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

}

Tuesday, 9 September 2014

Thursday, 17 July 2014

Three times the sum of digits of the number equals number itself.

// num = 27 = 3*(2+7)
// AIM: To find all such numbers

#include<stdio.h>
void main()
{
    long num = 1, i;
    int temp = 0;
    long  test = 0;
    long count = 0;
    while(count < 100)
    {
        i = num;
        test = 0;
        while(i != 0)
        {
            temp = i % 10;
            test = test + temp;
            i = i / 10;
        }
        if( (test*3) == num)
        {
            printf("found : %d", num);
            //only such number is 27, u can check upto infinite but no use, I already tried. :P
        }
        //printf("num = %d and test*3 = %d\n",num, test*3);
        count++;
        num++;
    }
}