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

No comments:

Post a Comment