Search Tutorials

Loading...

Tuesday, 23 April 2013

Java code to insert,delete and display from Dequeue

/*
Java program to implement dequeue.
*/

import java.io.*;
class dequeue
{
int DQ[] = new int[100];
int n;
int front, rear;
static BufferedReader br = new BufferedReader(newInputStreamReader(System.in));
public dequeue(int nn)
{
n = nn;
front = rear = 0;
}
public void pushrear(int v)
{
if((rear+1)<n)
{
rear++;
DQ[rear] = v;
}
else
System.out.println("Overflow from rear !");
}
public void pushfront(int v)
{
if(front>=0)
{
DQ[front] = v;
front--;
}
else
System.out.println("Overflow from front !");
}
public int popfront()
{
int v;
if(front!=rear)
{
front++;
v = DQ[front];
return v;
}
else
return -9999;
}
public int poprear()
{
int v;
if(front!=rear)
{
v = DQ[rear];
rear--;
return v;
}
else
return -9999;
}
public void display()
{
if(front!=rear)
{
for(int i=front+1;i<=rear;i++)
System.out.println(DQ[i]);
}
else
System.out.println("No element in queue !");
}
public static void main() throws IOException
{
System.out.print("Enter the size of the queue : ");
int size = Integer.parseInt(br.readLine());
dequeue call = new dequeue(size);
int choice;
boolean exit = false;
while(!exit)
{
System.out.print("\n1 : Push from Front\n2 : Push from
Rear\n3 : Delete from Front\n4 : Delete from Rear\n5 : Display\n6 :
Exit\n\nYour Choice : ");
choice = Integer.parseInt(br.readLine());
switch(choice)
{
case 1 :
System.out.print("\nEnter number to be added : ");
int num = Integer.parseInt(br.readLine());
call.pushfront(num);
break;
case 2 :
System.out.print("\nEnter number to be added : ");
int num2 = Integer.parseInt(br.readLine());
call.pushrear(num2);
break;
case 3 :
int popped = call.popfront();
if(popped != -9999)
System.out.println("\nDeleted : " +popped);
break;
case 4 :
int popped2 = call.popfront();
if(popped2 != -9999)
System.out.println("\nDeleted : " +popped2);
break;
case 5 :
call.display();
break;
case 6 :
exit = true;
break;
default :
System.out.println("\nWrong Choice !");
break;
}
}
}
}

/**
* Algorithm for Push from Front :
* -----------------------------
* 1. Start
* 2. If(front>=0) then
* 3. dq[front]=v;
* 4. front=front-1;
*
else
* 5. "overflow from front"
* 6. End
*
* Algorithm for Push from Rear :
* ----------------------------
* 1. Start
* 2. If ((rear+1)<n) then
* 3. rear=rear+1;
* 4. dq[rear]=v;
*
else
* 5. "overflow from rear"
* 6. End
*
* Algorithm for Pop from Front :
* ----------------------------
* 1. Start
* 2. If(front!=rear) then
* 3. front=front+1;
* 4. v=dq[front];
*
else
* 5. "dequeue empty from front"
* 6. End
*
* Algorithm for Pop from Rear :
* ---------------------------
* 1. Start
* 2. If(front!=rear) then
* 3. v=dq[rear];
* 4. rear=rear-1;
*
else
* 5. "dequeue is empty from rear"
* 6. End
*/

/*
OUTPUT :
------
Enter the size of the queue : 6
1:Push from Front
2:Push from Rear
3:Delete from Front
4:Delete from Rear
5:Display
6:Exit

Your Choice : 1
Enter number to be added : 2
....
...
...
......
...
*/


1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Back to Top