Tuesday, December 13, 2011

BlueJ program on Dequeue


First of all we should have some idea about Deque. Queue means where insertion are normally done at the rear end and deletion is done from the front end. Dequeue is a special type of queue where insertion and deletion can be done from both the ends but no operation is permitted at the middle.

Here is the BlueJ program on Dequeue

import java.io.*;
public class stack
{
int arr[]=new int[10];
int f,r;
int i,n;
String str;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
stack()
{
 f=-1;
 r=-1;
}
public void push() throws IOException
{
if(r==9)
{
 System.out.println("Queue overflow...");
 return;
}
System.out.println("Specify the location ( Front or rear):");
 str=br.readLine().toLowerCase();

System.out.println("Enter the value to insert: ");
n=Integer.parseInt(br.readLine());
if(f==-1)
{
 arr[++f]=n;
 r=0;
}
else if(str.charAt(0)=='f')
{
for(i=r+1;i>f;i--)
arr[i]=arr[i-1];
arr[i]=n;
r++;
}
else
{
arr[++r]=n;
}
}

public void display()
{
if(f==-1)
return;
 for(i=f;i<=r;i++)
 System.out.print(" "+arr[i]);
}

public void pop() throws IOException
{
if(f==-1)
{
 System.out.println("Queue Underflow...");
 return;
}
System.out.println("Specify the location ( Front or rear):");
str=br.readLine().toLowerCase();
if(f==r)
{
 f=-1;
 r=-1;
 }
else if(str.charAt(0)=='f')
{
f++;
}
else
{
r--;
}
}
public static void main(String args[])throws Exception
{
char op;
BufferedReader br;
 stack ob=new stack();
 while(true)
 {
     br=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("\nPress 'p' for push, 'd' for pop and 'q' for quite:");
     op=(char)br.read();
     if(op=='p')
    ob.push();
    else if(op=='d')
    ob.pop();
    ob.display();
    if(op=='q')
    break;
    br=null;
}
}
}

Technical analysis of the BlueJ program on Dequeue

In this BlueJ program on Dequeue, I have taken array of size 10. The array will be used to perform the job of Dequeue in this program. Normally Queue or Dequeues are dynamic in nature, any number of values can be inserted in them. Array has some limitations, insertion can not exceed the size of the array.

Two variables ‘f’ and ‘r’ are taken in this BlueJ program on Dequeue to perform the push and pop operations. The variable ‘f’ will access the front location of the array and ‘r’ will access the rear location. Initially both are set to ‘0’.

While pushing values at rear end, the insertion is performed very easily just like setting values in an array but while values are inserted at the front location, the values are shifted one step to right to make room for the new value at the ‘0’ index.

Pop operations in this BlueJ program on Dequeue is performed by either increasing the value of ‘f’ or decreasing the value of ‘r’ as directed by user.


Sample input and output of BlueJ program on Dequeue


Press 'p' for push, 'd' for pop and 'q' for quite:
p
Specify the location ( Front or rear):
f
Enter the value to insert:
100
 100
Press 'p' for push, 'd' for pop and 'q' for quite:
p
Specify the location ( Front or rear):
f
Enter the value to insert:
200
 200 100
Press 'p' for push, 'd' for pop and 'q' for quite:
p
Specify the location ( Front or rear):
r
Enter the value to insert:
30
 200 100 30
Press 'p' for push, 'd' for pop and 'q' for quite:
d
Specify the location ( Front or rear):
f
 100 30
Press 'p' for push, 'd' for pop and 'q' for quite:
p
Specify the location ( Front or rear):
f
Enter the value to insert:
500
 500 100 30
Press 'p' for push, 'd' for pop and 'q' for quite:
p
Specify the location ( Front or rear):
f
Enter the value to insert:
600
 600 500 100 30
Press 'p' for push, 'd' for pop and 'q' for quite:

2 comments:

  1. Sir,Would you please give me a solution for printing the pattern in Java

    1
    12
    123
    1234
    12345

    using a 2-D array.

    ReplyDelete
  2. public class Series
    {
    int i,jx;
    int arr[][]=new int[5][5];
    public void take()
    {

    for(i=0;i<5;i++)
    {
    x=1;
    for(j=0;j<=i;j++)
    {
    arr[i][j]=x++;
    }
    }
    System.out.println("Elements are \n");
    for(i=0;i<5;i++)
    {
    for(j=0;j<=i;j++)
    {
    System.out.print(arr[i][j]+ " ");

    }
    System.out.println();
    }
    }
    public static void main(String args[])
    {
    Series ob=new Series();
    ob.take();
    }
    }

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner