Pages

Thursday, 6 February 2020

Output Restricted Double Ended Queue ( DEQUE )

Output Restricted Double Ended Queue ( DEQUE )
        It is also called as double ended queue, in which insertion and deletion operations are performed from both the ends i.e we can insert an element from rear end or from the front end , we can delete an element from rear end or from the front end. Hence it is commonly referred to as DEQUE,there are two types of Deque.
1.Input Restricted Deque
2.Output Restricted Deque

The following are the operation performed in Input Restricted Deque
 1.Insert at rear
 2.Delete from front
 3.Delete from rear

The following are the operation performed in Output Restricted Deque
 1.Insert at front
 2.Insert at rear
 3.Delete from front


 /* Output restricted dequeue using array*/
import java.util.Scanner;
class rkdqoutput
{
  static int[] a;
  static int i,n,item,front=-1,rear=-1,ch;
  static Scanner dqo=new Scanner(System.in);
  public static void main(String[] args)
  {
   System.out.printf("\nEnter the length of the output restricted dequeue :\n");
   n=dqo.nextInt();
   a=new int[n];
   while(true)
   {
    System.out.printf("\n    MENU \n\n");
    System.out.printf(" 1.Insert at rear\n");
    System.out.printf(" 2.Insert at front\n");
    System.out.printf(" 3.Delete from front\n");
    System.out.printf(" 4.Display\n");
System.out.printf(" 5.Quit\n");
System.out.printf("Enter ur choice: ");
    ch=dqo.nextInt();
    switch(ch)
    {
     case 1:insert_rear();break;
     case 2:insert_front();break;
     case 3:del_front();break;
case 4:display();break;
     case 5:System.exit(1);break;
     default:
      System.out.printf("u have entered wrong choice");break;
    }
   System.out.printf("\n");
  }
}

static int insert_rear()
{

if((front == 0 && rear == n-1) || (front == rear+1)|| (rear==n-1))
{
System.out.printf("Dequeue Overflow\n");
return rear;
}
else
if (front == -1)  /* if queue is initially empty */
   front = 0;
else
if(rear == n-1)  /*right is at last position of queue */
rear = 0;
rear = rear+1;
System.out.printf("Input the element for adding in dequeue : ");
    item=dqo.nextInt();
    a[rear]=item;
return rear;
}/*End of insert_rear()*/

static int insert_front()
{
int item;
if((front==0 && rear == n-1) || (front == rear+1))
{
System.out.printf("Dequeue Overflow \n");
return front;
}
else
if(front == n )
{
System.out.printf("\n Insertion is not possible\n");
return front;
}
else
if (front == -1)/*If queue is initially empty*/
  {
   front = 0;
   rear = 0;
  }
else
if(front== 0)
front=n-1;
else
front=front-1;
System.out.printf("Input the element for adding in dequeue : ");
item=dqo.nextInt();
    a[front]=item;
return front;
}/*End of insert_front()*/


static int del_front()
{
    if (front == -1)
{
System.out.printf("Queue Underflow\n");
return front;
}
else
System.out.printf("Element deleted from dequeue is : %d\n",a[front]);
a[front]='\0';
if(front == rear) /*Queue has only one element */
{
front = -1;
rear=-1;
}
else
if(front == n-1)
front = 0;
else
front = front+1;
return front;
}/*End of delete_front()*/

static int display()
{
 if(front==-1 && rear==-1)
 {
  System.out.printf("\n dequeue is empty \n");
  return rear;
 }
 else
 {
  System.out.printf("\n dequeue elements are:");
  for(i=0;i<n;i++)
   {
   if(a[i]==0)
     System.out.printf("\t");
   else
     System.out.printf("%d\t",a[i]);
   }
 }
 System.out.printf("\n front = %d rear = %d \n",front,rear);
 return rear;
}/*End of display() */
}

No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...