Tuesday, 4 September 2012

Double Ended Queue


/*Output Restricted Deque*/
/*Input From both ends And Output from Front*/
#include<stdio.h>
#include<conio.h>
#define max 5
typedef struct Dq
{
    int list[max];
    int rear,front;
}DQ;
void enquing_front(DQ*q,int item)
{
    int i;
    if(q->front==0 && q->rear==max-1)
    {
        printf("The deque is full\n");
        getche();
        exit(0);
    }
    if(q->front==-1)
        q->rear=q->front=0;
    else if(q->front>0)
        q->front--;
    else
    {
        for(i=q->rear;i>=0;i--)
        {
            q->list[i+1]=q->list[i];
        }
        q->rear++;
    }
    q->list[q->front]=item;
}
void enquing_end(DQ*q,int item)
{
    int i;
    if(q->front==0 && q->rear==max-1)
    {
        printf("The deque is full\n");
        getche();
        exit(0);
    }
    if(q->front==-1)
       q->rear=q->front=0;
    else if(q->rear==max-1)
    {
        for(i=q->front;i<=q->rear;i++)
        {
            q->list[i-1]=q->list[i];
        }
        q->front--;
    }
    else
        q->rear++;
    q->list[q->rear]=item;
}
int dequing(DQ*q)
{
    int val;
    if(q->front==-1)
    {
        printf("The deque is Empty\n");
        getche();
        exit(0);
    }
    val=q->list[q->front];
    if(q->front==q->rear)
    {
        q->front=q->rear=-1;
        return val;
    }
    q->front++;
    return val;
}
int main()
{
    DQ queue;
    queue.front=queue.rear=-1;
    enquing_end(&queue,1);
    enquing_end(&queue,2);
    enquing_front(&queue,0);
    enquing_front(&queue,-1);
    enquing_end(&queue,3);
    printf("%d\n",dequing(&queue));
    enquing_end(&queue,4);
    printf("%d\n",dequing(&queue));
    printf("%d\n",dequing(&queue));
    printf("%d\n",dequing(&queue));
    printf("%d\n",dequing(&queue));
    printf("%d\n",dequing(&queue));
    getche();
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
@Gnosioware Solutions