Thursday, January 5, 2012

Array [XII] Double-Ended Queue.


 

A double-ended queue  (dequeue, often abbreviated to deque, pronounced deck) is an abstract data type that implements a queue for which elements can only be added to or removed from the front or the rear.
·           An input-restricted deque is one where deletion can be made from both ends, but insertion can only be made at one end.
·           An output-restricted deque is one where insertion can be made at both ends, but deletion can be made from one end only.
Both the basic and most common list types in computing, queues and stacks can be considered specializations of deques, and can be implemented using deques.

Source Code


#include <stdio.h>
#include <conio.h>
int dq[10];
int front = -1;
int rear = -1;

  void Push_Right()
      {
           if((front == 0 && rear == 9) || (front == rear+1))
              {
                   printf("\n\tQueue Full\n");
                   return;
              }
           if (front == -1)
              {
                   front = 0;
                   rear = 0;
              }
           else   if(rear == 9)
              {
                   rear = 0;
              }
           else
              {
                   rear = rear+1;
              }
          printf("\n\tInput An Element : ");
           scanf("%d", &dq[rear]);
      }

  void Push_Left()
      {
           if((front == 0 && rear == 9) || (front == rear+1))
              {
                   printf("\n\tQueue is Full \n");
                   return;
              }
           if (front == -1)
              {
                   front = 0;
                   rear = 0;
              }
           else if(front== 0)
              {
                   front=9;
              }
           else
              {
                   front=front-1;
              }
           printf("\n\tInput an Eelement : ");
           scanf("%d", &dq[front]);
      }

  void Pop_Left()
      {
           if (front == -1)
              {
                   printf("\n\tQueue Empty!\n");
                   return ;
              }
           printf("\n\tElement Removed from Queue = : %d\n",dq[front]);
           if(front == rear)
              {
                   front = -1;
                   rear=-1;
              }
           else if(front ==9)
              {
                   front = 0;
              }
           else
              {
                   front = front+1;
              }
      }
  void Pop_Right()
      {
           if (front == -1)
              {
                   printf("\n\tQueue is Empty!\n");
                   return ;
              }
           printf("\n\tElement Removed  : %d\n",dq[rear]);
           if(front == rear)
              {
                   front = -1;
                   rear=-1;
              }
           else if(rear == 0)
              {
                   rear=9;
              }
           else
              {
                   rear=rear-1;
              }
      }

  void Disp()
      {
           int fp = front,rp = rear;
           if(front == -1)
              {
                   printf("\n\tQueue is Empty!\n");
                   return;
              }
           printf("\n\tQueue elements : \t ");
           if( fp <= rp )
              {
                   while(fp <= rp)
                        {
                            printf("%d ",dq[fp]);
                            fp++;
                        }
              }
           else
              {
                   while(fp <= 9)
                        {
                            printf("%d ",dq[fp]);
                            fp++;
                        }
                   fp = 0;
                   while(fp <= rp)
                        {
                            printf("%d ",dq[fp]);
                            fp++;
                        }
              }
           printf("\n");
           getch();
   }


  void Input_Menu()
      {
           int ch=0;
           clrscr();
           while(ch<5)
              {
                   printf("\n\t * Input Restricted Menu *\n");
                   printf("\n\t1. Insert at Rear");
                   printf("\n\t2. Delete from front");
                   printf("\n\t3. Delete from rear");
                   printf("\n\t4. Display");
                   printf("\n\t5. Quit");
                   printf("\n\tEnter your choice : ");
                   scanf("%d",&ch);
                   switch(ch)
                        {
                            case 1:Push_Right();break;
                            case 2:Pop_Left();break;
                            case 3:Pop_Right();break;
                            case 4:Disp();break;
                        }
              }
       }

  void Output_Menu()
      {
           int ch=0;
           clrscr();
           while(ch<5)
              {
                   printf("\n\t * Output Restricted Menu *\n");
                   printf("\n\t1. Push at Rear");
                   printf("\n\t2. Push at Front");
                   printf("\n\t3. Pop from Front");
                   printf("\n\t4. Display");
                   printf("\n\t5. Quit");
                   printf("\n\tEnter A choice : ");
                   scanf("%d",&ch);
                   switch(ch)
                        {
                            case 1: Push_Right();break;
                            case 2: Push_Left();break;
                            case 3: Pop_Left();break;
                            case 4: Disp();break;
                        }
              }
      }



  void main()
      {
           int ch;
           clrscr();
           while(ch!=3)
              {
                   printf("\n\t * Main Menu *\n");
                   printf("\n\t1. Input Restricted");
                   printf("\n\t2. Output Restricted");
                   printf("\n\t3. Exit");
                   printf("\n\tEnter A choice : ");
                   scanf("%d",&ch);
                   switch(ch)
                        {
                            case 1:Input_Menu();break;
                            case 2:Output_Menu();break;
                            case 3:printf("\n\t..Quit...");getch();exit(0);
                            default: printf("Wrong choice\n");
                        }
              }
      }

No comments: