Thursday, 5 January 2017

Binary Tree size recursion and non recursion

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.

Write a program to find the size of a binary tree using recursion and non recursion

 */
package bin_tree_size_height_depth;

import java.util.Scanner;

/**
 *
 * @author shabhatn
 */

class binary_node
{
    binary_node left , right;
    int data;
   
    public binary_node()
    {
        left = right = null;
       
    }
   
    public binary_node(int data)
    {
        left = right = null;
        this.data = data;
    }

    public binary_node getLeft() {
        return left;
    }

    public void setLeft(binary_node left) {
        this.left = left;
    }

    public binary_node getRight() {
        return right;
    }

    public void setRight(binary_node right) {
        this.right = right;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }
   
   
}

class binary_tree
{
    binary_node root;
    int size;
   
    public binary_tree()
    {
        root = null;
        size = 0;
    }
   
    public boolean isEmpty()
    {
        return root == null;
    }
   
    public void insert(int data)
    {
        root = insert_recursion(root,data);
    }
   
    public binary_node insert_recursion(binary_node node,int data)
    {
        if(node==null)
        {
            node = new binary_node(data);
        }
        else
        {
            if(node.getLeft()==null)
            {
                node.left = insert_recursion(node.left,data);
            }
            else
            {
                node.right = insert_recursion(node.right,data);
            }
        }
        return node;
    }
   
    public void insert_nonrecursion(int data)
    {
        binary_node nptr = new binary_node(data);
        binary_node temp;
        ll_queue lq = new ll_queue();
        if(root==null)
        {
            root = nptr;
        }
        else
        {
           
            lq.enqueue(root);
           
            while(!lq.isEmpty())
            {
                temp = lq.dequeue();
                if(temp.getLeft()!=null)
                {
                   
                    lq.enqueue(temp.getLeft());                  
                }
                else
                {
                    temp.setLeft(nptr);
                    break;
                }
                if(temp.getRight()!=null)
                {
                    lq.enqueue(temp.getRight());                  
                }
                else
                {
                    temp.setRight(nptr);
                    break;
                }
                 
            }
        }
       
    }
    public void Level_order_traversal()
    {
        LevelOrderTraversal(root);
    }
   
    public void LevelOrderTraversal(binary_node node)
    {
        ll_queue lq = new ll_queue();
        binary_node temp;
        if(node!=null)
        {
            lq.enqueue(node);
            while(!lq.isEmpty())
            {
                temp = lq.dequeue();
                System.out.print(temp.getData()+ " ");
                if(temp.getLeft()!=null)
                    lq.enqueue(temp.getLeft());
                if(temp.getRight()!=null)
                    lq.enqueue(temp.getRight());
               
            }
           
        }
    }
   
    public void size()
    {
        int x = size_recursion(root);
        System.out.print("size recursion-->" + x);
    }
   
    public int size_recursion(binary_node node)
    {
        int left , right , count=0;
        if(node==null)
            return 0;
        else
        {
            count++;
            left = size_recursion(node.left);
            right = size_recursion(node.right);
           
           count = left+right+count;
           
        }
        return count;
    }
   
    public int size_nonrecursion()
    {
        int count = 0;
       
        if(root==null)
            return 0;
        else
        {
            ll_queue lq = new ll_queue();
            binary_node temp;
            lq.enqueue(root);
            while(!lq.isEmpty())
            {
                temp = lq.dequeue();
                count++;
                if(temp.getLeft()!=null)
                    lq.enqueue(temp.getLeft());
                if(temp.getRight()!=null)
                    lq.enqueue(temp.getRight());
            }
            return count;
           
        }
    }
    public void display()
     {
         char c;
         Scanner sc = new Scanner(System.in);
         int data;
         do
         {
             data = sc.nextInt();
             insert(data);
            // insert_nonrecursion(data);
             System.out.println("do you want to insert more");
             c = sc.next().charAt(0);
         }while(c=='Y'||c=='y');
         System.out.println("Levelorder");
         Level_order_traversal();
         size();
         int x = size_nonrecursion();
         System.out.println("non recursion size-->"+x);
     }
   
}


class ll_queue
{
    ll_node front,rear;
    int size;
   
    public ll_queue()
    {
        front = rear = null;
        size = 0;
    }
    public boolean isEmpty()
    {
        return front==null;
    }
   
    public void enqueue(binary_node node)
    {
        ll_node nptr = new ll_node(node);
       
        if(front==null)
        {
            front = rear = nptr;
        }
        else
        {
            rear.setLink(nptr);
            rear = nptr;
        }
        size++;
    }
   
    public binary_node dequeue()
    {
        binary_node temp;
        if(front==null)
            return null;
        else
        {
            temp = front.getNode();
            front = front.getLink();
        }
                   
        return temp;
    }
   
}



class ll_node
{
    ll_node link;
    binary_node node;
   
    public ll_node()
    {
        link = null;
        node = null;
    }
   
    public ll_node(binary_node node)
    {
        link = null;
        this.node = node;
    }

    public ll_node getLink() {
        return link;
    }

    public void setLink(ll_node link) {
        this.link = link;
    }

    public binary_node getNode() {
        return node;
    }

    public void setNode(binary_node node) {
        this.node = node;
    }
   
   
   
}


public class Bin_tree_size_height_depth {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       
        binary_tree bt = new binary_tree();
        bt.display();
    }
   
}

Wednesday, 4 January 2017

Binary tree insertion without recursion

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.

Write a program for binary tree insertion without recursion

 */
package non_recursion_bin_tree_insertion;

import java.util.Scanner;

/**
 *
 * @author shabhatn
 */
class binary_node
{
    binary_node left ,right;
    int data;
   
    public binary_node()
    {
        left = right = null;
    }
   
    public binary_node(int data)
    {
        left = right = null;
        this.data = data;
    }

    public binary_node getLeft() {
        return left;
    }

    public void setLeft(binary_node left) {
        this.left = left;
    }

    public binary_node getRight() {
        return right;
    }

    public void setRight(binary_node right) {
        this.right = right;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }
   
   
}

class binary_tree
{
    binary_node root;
    int size;
   
    public binary_tree()
    {
        root = null;
    }
   
    public boolean isEmpty()
    {
        return root==null;
    }
   
    public void insert(int data)
    {
       binary_node temp;
     
       binary_node nptr = new binary_node(data);
       ll_queue lq = new ll_queue();
     
       if(root==null)
       {
           root = nptr;
       }
       else
       {
           lq.enqueue(root);
           while(!lq.isEmpty())
           {
               temp = lq.dequeue();
             
              // System.out.println(temp.getData());
             
               if(temp.getLeft()!=null)
               {
                 
                   lq.enqueue(temp.getLeft());  
               }
               else
               {
                   temp.setLeft(nptr);
                   break;
               }
               if(temp.getRight()!=null)
               {
                 
                   lq.enqueue(temp.getRight());
                 
               }
               else
               {
                   temp.setRight(nptr);
                   break;
               }
           }
           lq.delete_queue();
       }
    }
     public void preorder()
    {
        preorder_traversal(root);
    }
    public void preorder_traversal(binary_node preO)
    {
          if(preO!=null)
        {
              System.out.print(preO.getData() + " ");
              preorder_traversal(preO.getLeft());
              preorder_traversal(preO.getRight());
           
        }
       
    }
    public void Postorder()
    {
        Postordertraversal(root);
    }
   
    public void Postordertraversal(binary_node node)
    {
        if(node!=null)
        {
            Postordertraversal(node.getLeft());
            Postordertraversal(node.getRight());
            System.out.print(node.getData()+ " ");
           
        }
    }

   
    public void Inorder()
    {
        Inordertraversal(root);
    }
   
    public void Inordertraversal(binary_node node)
    {
        if(node!=null)
        {
            Inordertraversal(node.getLeft());
            System.out.print(node.getData()+ " ");
            Inordertraversal(node.getRight());
        }
    }
   
    public void Level_order_traversal()
    {
        LevelOrderTraversal(root);
    }
   
    public void LevelOrderTraversal(binary_node node)
    {
        ll_queue lq = new ll_queue();
        binary_node temp;
        if(node!=null)
        {
            lq.enqueue(node);
            while(!lq.isEmpty())
            {
                temp = lq.dequeue();
                System.out.print(temp.getData()+ " ");
                if(temp.getLeft()!=null)
                    lq.enqueue(temp.getLeft());
                if(temp.getRight()!=null)
                    lq.enqueue(temp.getRight());
               
            }
            lq.delete_queue();
           
        }
    }
   
    public void display()
     {
         char c;
         Scanner sc = new Scanner(System.in);
         int data;
         do
         {
             data = sc.nextInt();
             insert(data);
             System.out.println("do you want to insert more");
             c = sc.next().charAt(0);
         }while(c=='Y'||c=='y');
         System.out.println("Preorder");
         preorder();
         System.out.println("Inorder");
         Inorder();
         System.out.println("Postorder");
         Postorder();
         System.out.println("Levelorder");
         Level_order_traversal();
     }
   
}

class ll_node
{
    ll_node link;
    binary_node data;
   
    public ll_node()
    {
        link = null;
        data = null;
    }
   
    public ll_node(binary_node data)
    {
        this.data = data;
        link = null;
    }

    public ll_node getLink() {
        return link;
    }

    public void setLink(ll_node link) {
        this.link = link;
    }

    public binary_node getData() {
        return data;
    }

    public void setData(binary_node data) {
        this.data = data;
    }
   
   
}

class ll_queue
{
    ll_node front , rear;
    int size;
   
    public ll_queue()
    {
        front = rear = null;
        size = 0;
    }
   
    public boolean isEmpty()
    {
        return front==null;
    }
   
    public void enqueue(binary_node data)
    {
        ll_node nptr = new ll_node(data);
       
        if(front==null)
        {
            front = rear = nptr;
        }
        else
        {
            rear.setLink(nptr);
            rear = nptr;
        }
        size++;
    }
   
    public binary_node dequeue()
    {
        binary_node data;
        if(isEmpty())
        {
            return null;
        }
        else
        {
            data = front.getData();
            front = front.getLink();
        }
        size--;
        return data;
    }
   
    public void delete_queue()
    {
        while(!isEmpty())
        {
            front = front.getLink();
            size--;
        }
       
    }
   
   
}


public class Non_recursion_bin_tree_insertion {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        binary_tree bt = new binary_tree();
        bt.display();
        // TODO code application logic here
    }
   
}

Tuesday, 3 January 2017

Search an element in binary tree using recursion and non recursion

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.

Write a program to search an element in binary tree using recursion and non recursion

 */
package search_binary_tree;

import java.util.Scanner;

/**
 *
 * @author shabhatn
 */

class ll_node
{
    ll_node link;
    binary_node data;
   
    public ll_node()
    {
        link = null;
        data = null;
    }
   
    public ll_node(binary_node data)
    {
        this.data = data;
        link = null;
    }

    public ll_node getLink() {
        return link;
    }

    public void setLink(ll_node link) {
        this.link = link;
    }

    public binary_node getData() {
        return data;
    }

    public void setData(binary_node data) {
        this.data = data;
    }
   
   
}

class ll_queue
{
    ll_node front , rear;
    int size;
   
    public ll_queue()
    {
        front = rear = null;
        size = 0;
    }
   
    public boolean isEmpty()
    {
        return front == null;
    }
   
    public void enqueue(binary_node data)
    {
        ll_node nptr = new ll_node(data);
       
        if(front==null)
        {
            front = rear = nptr;
        }
        else
        {
            rear.setLink(nptr);
            rear = nptr;
        }
        size++;
    }
   
    public binary_node dequeue()
    {
        binary_node node;
        if(isEmpty())
        {
            return null;
        }
        else
        {
            node = front.getData();
            front = front.getLink();
         
        }
        size--;
        return node;
    }
   
}



class binary_node
{
    binary_node left ,right;
    int data;
   
    public binary_node()
    {
        left = right = null;
    }
   
    public binary_node(int data)
    {
        left = right = null;
        this.data = data;
    }

    public binary_node getLeft() {
        return left;
    }

    public void setLeft(binary_node left) {
        this.left = left;
    }

    public binary_node getRight() {
        return right;
    }

    public void setRight(binary_node right) {
        this.right = right;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }
   
   
}

class binary_tree
{
    binary_node root;
    int size;
   
    public binary_tree()
    {
        root = null;
    }
   
    public boolean isEmpty()
    {
        return root==null;
    }
   
    public void insert(int data)
    {
        root = insert_binary(root,data);
    }
   
    public binary_node insert_binary(binary_node noder, int data)
    {
       
       
        if(noder==null)
        {
            noder = new binary_node(data);
        }
        else
        {
            if(noder.getLeft()==null)
            {
                noder.left = insert_binary(noder.left,data);
            }
            else
            {
                noder.right = insert_binary(noder.right,data);
            }
        }
        return noder;
    }
   
    public void search(int data)
    {
        int d,d1;
        d = search(root,data);
        if(d!=-999)
        {
        System.out.println("data found-->" + d);
        }
        else
        {
            System.out.println("data not found recursioncode :Shas " +d );
        }
        d1 = search_nonrecursion(root,data);
        if(d1!=-998)
        {
        System.out.println("data found-->" + d);
        }
        else
        {
            System.out.println("data not found nonrecusrioncode :Shas " +d1);
        }
       
    }
   
    public int search(binary_node root,int data)
    {
        int root_val,left,right,ser=-999;
        if(root!=null)
        {
            root_val = root.getData();
            if(root_val==data)
            {
                return data;
            }
            left = search(root.left,data);
            if(left==data)
                return data;
            right = search(root.right,data);
            if(right==data)
                return data;          
           
        }
           
        return ser;
    }
   
    public int search_nonrecursion(binary_node root,int data)
    {
        binary_node temp;
        int ser = -998;
        ll_queue lq = new ll_queue();
        if(root==null)
            return -998;
        lq.enqueue(root);
        while(!lq.isEmpty())
        {
            temp = lq.dequeue();
            if(temp.getData()==data)
                return data;
            if(temp.getLeft()!=null)
            {
                lq.enqueue(temp.getLeft());
            }
            if(temp.getRight()!=null)
            {
                lq.enqueue(temp.getRight());
            }
        }

           
       
        return ser;
       
    }
   
     public void display()
     {
         char c;
         Scanner sc = new Scanner(System.in);
         int data;
         do
         {
             data = sc.nextInt();
             insert(data);
             System.out.println("do you want to insert more");
             c = sc.next().charAt(0);
         }while(c=='Y'||c=='y');
       
         System.out.println("Enter data to search");
          int d = sc.nextInt();
         search(d);
     }
   
   
}


public class Search_binary_tree {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        binary_tree br = new binary_tree();
       
        br.display();
        // TODO code application logic here
    }
   
}

Monday, 2 January 2017

Binary tree max value using recursion and non - recursion

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.

Write a program to find binary tree maximum value using recursion and non recursion

 */
package findmax_binary_tree;

import java.util.Scanner;

/**
 *
 * @author shabhatn
 */


    /**
     * @param args the command line arguments
     */

class ll_node
{
    ll_node link;
    node data;
   
    public ll_node()
    {
        link = null;
        data = null;
       
    }
   
    public ll_node(node data,ll_node dnode)
    {
        link = dnode;
        this.data = data;
    }

    public ll_node getLink() {
        return link;
    }

    public void setLink(ll_node link) {
        this.link = link;
    }

    public node getData() {
        return data;
    }

    public void setData(node data) {
        this.data = data;
    }
   
   
}

class ll_queue
{
    ll_node front , rear;
    int size;
   
    public ll_queue()
    {
        front = rear = null;
    }
   
    public boolean isempty()
    {
        return front==null;
    }
   
    public void enqueue(node n)
    {
        ll_node nptr = new ll_node(n,null);
        if(front==null)
        {
            front = rear = nptr;
        }
        else
        {
            rear.setLink(nptr);
            rear = nptr;
        }
       
    }
   
    public node dequeue()
    {
        if(isempty())
        {
            return null;
        }
        else
        {
            node d = front.getData();
            front = front.getLink();
            return d;
        }
    }
}




 class node
 {
     node left, right ;
     int data;
     public node()
     {
         left = null;
         right = null;
         data = 0;
     }
     public node(int data)
     {
         left = right = null;
         this.data = data;
     }

        public node getLeft() {
            return left;
        }

        public void setLeft(node left) {
            this.left = left;
        }

        public node getRight() {
            return right;
        }

        public void setRight(node right) {
            this.right = right;
        }

        public int getData() {
            return data;
        }

        public void setData(int data) {
            this.data = data;
        }
   
 }

 class binary_tree
 {
     node root;
     int size;
   
     public binary_tree()
     {
         root = null;
     }
   
     public boolean isEmpty()
     {
         return root==null;
     }
     public void insert(int data)
     {
         root = insert_binary(root,data);
     }
   
     public node insert_binary(node noder,int data)
     {
         if(noder==null)
         {
             noder = new node(data);
           
         }
         else
         {
             if(noder.getLeft()==null)
             {
                 noder.left = insert_binary(noder.left,data);
             }
             else
             {
                 noder.right = insert_binary(noder.right,data);
             }
         }
       
         return noder;
     }
   
     public void max()
     {
         int maxr = find_max_recursion(root);
       
         System.out.println(" max value from recurssion function is  ->" +maxr);
       
         int maxnr = find_max_nonrecursion(root);
       
         System.out.println(" max value from nonrecurssion function is  ->" +maxnr);
       
     }
   
     public int find_max_recursion(node root)
     {
         int root_val,left,right,max=-1;
       
         if(root!=null)
         {
             root_val = root.getData();
             left = find_max_recursion(root.getLeft());
             right = find_max_recursion(root.getRight());
             if(left>right)
                 max = left;
                         else
                 max = right;
             if(root_val>max)
                 max = root_val;
           
         }
       
         return max;
       
     }
     public int find_max_nonrecursion(node root)
     {
         int max = -1;
         node temp;
         ll_queue lq = new ll_queue();
         if(root==null)
             return -1;
         lq.enqueue(root);
         while(!lq.isempty())
         {
             temp = lq.dequeue();
             if(max<temp.getData())
                 max = temp.getData();
             if(temp.getLeft()!=null)
             {
             lq.enqueue(temp.getLeft());
             }
             if(temp.getRight()!=null)
             {
             lq.enqueue(temp.getRight());
             }
           
         }
       
         return max;
       
     }
   
     public void display()
     {
         char c;
         Scanner sc = new Scanner(System.in);
         int data;
         do
         {
             data = sc.nextInt();
             insert(data);
             System.out.println("do you want to insert more");
             c = sc.next().charAt(0);
         }while(c=='Y'||c=='y');
         max();
     }
   
     }

 public class FindMax_binary_tree
 {

    public static void main(String[] args) {
        // TODO code application logic here
        binary_tree br = new binary_tree();
        br.display();
    }
   
}

Friday, 30 December 2016

Breadth-First Search/Level order Traversal in a Binary Tree

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package levelordertraversalbfs;

/* Write a program for Level order traversal in Binary tree

Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root and explores the neighbor nodes first, before moving to the next level neighbors.

Here we are using Linked List queue to Store tree node for Level order traversing

*/

import java.util.Scanner;

/**
 *
 * @author shabhatn
 */

class LLNode
{
    LLNode link;
    Node data;
   
    public LLNode()
    {
        link = null;
        data = null;
    }
    public LLNode(Node data , LLNode n)
    {
        this.data = data;
        link = n;
    }

    public LLNode getLink() {
        return link;
    }

    public void setLink(LLNode link) {
        this.link = link;
    }

    public Node getData() {
        return data;
    }

    public void setData(Node data) {
        this.data = data;
    }
   
}

class LL_Queue
{
    LLNode front , rear;
   
    int size;
   
    public LL_Queue()
    {
        front =null;
        rear =null;
        size = 0;
    }
    public int getSize()
    {
        return size;
    }
    public boolean isEmpty()
    {
        return front==null;
    }
   
    public void enqueue(Node data)
    {
        LLNode nptr = new LLNode(data,null);
       
        if(front==null)
        {
            front = nptr;
            rear = nptr;
           
        }
        else
        {
            rear.setLink(nptr);
            rear = nptr;
           
        }
        size++;
       
    }
    public Node dequeue()
    {
        if(isEmpty())
        {
         return null;
        }
        else
        {
            Node d = front.getData();
            front = front.getLink();
            return d;
        }
       
    }
}

class Node
{
    Node Left , Right;
    int data;
   
    public Node()
    {
        Left = null;
        Right = null;
        data = 0;
    }
    public Node(int d)
    {
        Left = null;
        Right = null;
        data = d;
    }

    public Node getLeft() {
        return Left;
    }

    public void setLeft(Node Left) {
        this.Left = Left;
    }

    public Node getRight() {
        return Right;
    }

    public void setRight(Node Right) {
        this.Right = Right;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }
   
}

class Binary_tree
{
    Node root;
   
    public Binary_tree()
    {
        root = null;
    }
    public boolean isEmpty()
    {
        return root ==null;
    }
    public void insert(int data)
    {
        root = insert_tree(root,data);
    }
    public Node insert_tree(Node node, int data)
    {
        if(node==null)
        {
            node  = new Node(data);
        }
        else
        {
            if(node.getLeft()==null)
            {
                node.Left = insert_tree(node.Left,data);
            }
            else
            {
                node.Right = insert_tree(node.Right,data);
            }
        }
        return node;
    }
   
    public void Level_order()
    {
        Level_order_traversal(root);
    }
    public void Level_order_traversal(Node root)
    {
        Node temproot;
        LL_Queue lq = new LL_Queue();
        if(root==null)
        {
            System.out.print("No data");
        }
        else
        {
        lq.enqueue(root);
        while(!lq.isEmpty())
        {
            temproot = lq.dequeue();
            System.out.print("-->"+temproot.getData());
            if(temproot.getLeft()!=null)
            {
                lq.enqueue(temproot.getLeft());
            }
            if(temproot.getRight()!=null)
            {
                lq.enqueue(temproot.getRight());
            }
        }
        }
    }
   
}



public class LevelOrderTraversalBFS {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       
        Binary_tree bt = new Binary_tree();
       
        char ch;
        Scanner sc = new Scanner(System.in);
        do
        {
     
        System.out.println("enter data");
       
        int data = sc.nextInt();
       
        bt.insert(data);
        System.out.println("enter y to add more values to binary tree");
       
        ch = sc.next().charAt(0);
        }while(ch=='y'||ch=='Y');
        System.out.println();
        System.out.print("Level order traversal :->");
        bt.Level_order();
    }
   
}

Thursday, 29 December 2016

Linked List Queue Implementation

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.

Write a Program for Linked List queue Implementation

 */
package linkedlistqueue;

import java.util.Scanner;

/**
 *
 * @author shabhatn
 */

class Node
{
    Node link;
    int data;
   
    public  Node()
    {
        link = null;
        data = 0;
    }
    public Node(int data , Node n)
    {
        this.data = data;
        link = n;
    }

    public Node getLink() {
        return link;
    }

    public void setLink(Node link) {
        this.link = link;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }
   
}

class LL_queue
{
    Node front, rear;
    int size;
   
    public LL_queue()
    {
        front = rear = null;
        size = 0;
    }
    public int getSize()
    {
        return size;
    }
    public boolean isEmpty()
    {
        return front==null;
    }
    public void enqueue(int data)
    {
        Node nptr = new Node(data , null);
       
        if(front==null)
        {
            front = rear = nptr;
        }
        else
        {
            rear.setLink(nptr);
            rear = nptr;
        }
    }
   
    public void dequeue()
    {
        if(isEmpty())
        {
            System.out.println("No data present");
        }
        else
        {
            int data = front.getData();
            System.out.println("Dequeued data is :"+data);
            front  = front.getLink();
        }
    }
   
    public void display()
    {
        Node current = front;
        while(current.getLink()!=null)
        {
            System.out.println("->"+current.getData());
            current = current.getLink();
        }
        System.out.println("->"+current.getData());
    }
   
}



public class LinkedListQueue {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        Scanner sc = new Scanner(System.in);
        char choice;
        LL_queue lq = new LL_queue();
       
        do
        {
        System.out.println("Press 1 for enqueue");
        System.out.println("Press 2 for dequeue");
        System.out.println("Press 3 for display");
        int option = sc.nextInt();
        switch(option)
        {
            case 1:
                do
                {
                    System.out.println("Insert data:->");
                int data = sc.nextInt();
                lq.enqueue(data);
                System.out.println("If you want to insert more data press Y");
                choice = sc.next().charAt(0);
                }while(choice=='Y'||choice=='y');
                break;
                case 2:
                do
                {
               
                lq.dequeue();
                System.out.println("If you want to delete more data press Y");
                choice = sc.next().charAt(0);
                }while(choice=='Y'||choice=='y');
                break;
                case 3:
                lq.display();
                break;
                default:
                System.out.println(" no valid option -- exit");
                break;
       
           
        }
        System.out.println("Do you want to go to main menu");
        choice = sc.next().charAt(0);
        }while(choice=='Y'||choice=='y');
       
       
        // TODO code application logic here
    }
   
}

Wednesday, 28 December 2016

Dynamic Circular Array Queue

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

 /* Write a Program for dynamic circular array queue */
package dynamic_array_queue;

import java.util.Scanner;

/**
 *
 * @author shabhatn
 */

class dynamic_circular_queue
{
    int queue[];
    int size ;
    int front;
    int length;
    int rear;
   
    public dynamic_circular_queue(int size)
    {
        this.size = size;
        queue = new int[size];
        front = rear = -1;
        length =0;
    }
   
    public boolean isEmpty()
    {
        return front==-1;
    }
   
    public boolean isFull()
    {
        return (rear+1)%size==front;
    }
   
    public int getsize()
    {
        return length;
    }
   
    public void resize_array()
    {
        int old_array[];
        int oldsize = size;
        size = 2*size;
        old_array = queue;
        queue = new int[size];
       
        for(int i=0;i<old_array.length;i++)
        {
            queue[i] = old_array[i];
        }
       
        if(rear<front)
        {
            for(int i =0;i<front;i++)
            {
                queue[i+oldsize] = this.queue[i];
            }
            rear = rear+oldsize;
        }
    }
    public void enqueue(int data)
    {
        if(isFull())
        {
            resize_array();
            rear = (rear+1)%size;
            queue[rear] = data;
        }
        else
        {
            if(front==-1)
            {
                front=0;
                rear=0;
                queue[rear]=data;
            }
            else
            {
                rear = (rear+1)%size;
                queue[rear] = data;
               
                System.out.println(data);
            }
            length++;
        }
    }
   
    public void dequeue()
    {
        if(isEmpty())
        {
            System.out.println("Queue is empty");
        }
        else
        {
            int data = queue[front];
            System.out.println("Dequeued data--->" + data);
            length--;
           
            if(front==rear)
            {
                front =-1;
                rear = -1;
            }
            else
            {
                front = (front+1)%size;
            }
        }
    }
    public void display()
    {
        char choice;
        Scanner sc = new Scanner(System.in);
       
        char ch;
       
        do
        {
        System.out.println("press 1 to enqueue");
        System.out.println("press 2 to dequeue");
        System.out.println("press 3 to see all inserted data");
        int c = sc.nextInt();
        switch(c)
                {
        case 1:
        do
        {
            System.out.print("Insert data");
            int data = sc.nextInt();
            enqueue(data);
            System.out.print("Do you want to insert more data");
          choice = sc.next().charAt(0);
        }while(choice =='Y'||choice=='y');
        break;
        case 2:
        do
        {
            dequeue();
            System.out.print("Do you want to delete more data");
          choice = sc.next().charAt(0);
        }while(choice =='Y'||choice=='y');
        break;
        case 3:
            if(front>=0)
            {
               
        for(int i=front;i<queue.length && front!=-1;i++)
        {
            System.out.println("-->"+queue[i]);
        }
            }
           if(front>=0&&rear<=front)
            {
       for(int i=0;i<=rear;i++)
        {
            System.out.println("-->"+queue[i]);
        }
               
                }
           
        break;
        default:
        System.out.print("invalid input exit");
            break;
           
                }
        System.out.print("Do you want to go back to menu press y");
         ch = sc.next().charAt(0);
        }while(ch=='Y'||ch=='y');
    }
   
}

public class Dynamic_array_queue {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         System.out.print("Enter the  size of queue");
        Scanner sc = new Scanner(System.in);
        int size = sc.nextInt();
        dynamic_circular_queue aq = new dynamic_circular_queue(size);
        aq.display();
    }
   
}