Friday, 8 March 2013

Implement the following basic operations on double ended queues: a) Insertion at the rear end b) Insertion at the front end


a) insertion at the rear end on a double ended queue
/*function to insert an item at the rear end on a double ended queue*/
void insert_rear(int item, int q[], int *r)
{
     if (qfull(*r))/*check if queue is full*/
     {
          printf(“Queue Overflow\n”);
     }
     /*queue is not full*/
     q[++(*r)]= item;
}


/*function to check whether queue is full*/
int qfull(int r)
{    /*returns true if queue is full otherwise false*/
     return ( r==queue_size-1)?1:0;
}

b) Insertion at the front end on a double ended queue
/*function to insert an item at the from end on a double ended queue*/
void insert_front(int item,int q[], int *f, int *r)
{
     if ( *f ==0 && *r==-1)
          q[++(*r)]=item;
     else if ( *f !-0)
          q[--(*f)]=itme;
     else
          printf(“Front insertion not possible\n”);
}


/*main() function to implement these function*/
#include<stdio.h>
#include<process.h>
#define QUEUE_SIZE 5
void main()
{
int choice,item,f,r,q[10];
f=0, r=-1;
for(;;)
{
     printf(“1: Insert at Front End\n”);
     printf(“2: Insert at Front End\n”);
     scanf(“%d”,&choice);
     switch(choice)
     {
          case 1:
              printf(“Enter the item to be inserted\n”);
              scanf(“%d”,&item);
              insert_front(item,q,&f,&r);
              break;

          case 2:
              printf(“Enter the item to be inserted\n”);
              scanf(“%d”,&item);
              insert_rear(item,q,&r);
              break;
          default:
              exit(0);

     }
}
}