Reverse Queue Elements Using Stack In Solution C

Reverse Queue Elements Using Stack In Solution C

In this program reverse queue elements using stack.

First remove all elements from queue and insert into stack then remove all elements from stack and insert into queue.


#include
#define N 10
//queue
int q[N];
//stack
int a[N];
//top of  stack
int top=-1;
//front of queue
int f=-1;
//rear of queue
int r=-1;
//this function is used for insert new element in queue
void enqueue(int val)
{
    if(r>=N-1)
        printf("Queue overflow\n");
    else if(r==-1 && f== -1) 
    {
        f=r=0;
        q[r]=val;
    }
    else
    {
        r++;
        q[r]=val;
    }
}
//this fuction is used to remove element from queue
int dequeue()
{
    int tmp=0;
    if(f==-1)
        printf("queue underflown");
    tmp=q[f];
    if(f==r)
        f=r=-1;
    else
        f++;
    return tmp;
}
//this function is used to display queue element from front to rear
void display()
{
    int i;
    if(r==-1)
        printf("Queue underflown");
    else
    {
        for(i=f;i<=r;i++)
        {
            printf("%d t",q[i]);
        }
    }
}
//this function is used to check whether the stack is full or not
int isfull()
{
    if(top==N-1)
        return 1;
    else 
        return 0; 
}
//this function is used to check whether the stack is empty ot not
int isempty()
{
    if(top==-1)
        return 1;
    else
        return 0;
}
//this function is used to insert new item in stack
void push(int val)
{
    if(isfull()!=1)
    {
        top++;
        a[top]=val;
    }
}
//this function remove and return top element from stack
int pop()
{
    if(!isempty())
    {
        int tmp=a[top];
        top--;
        return tmp;
    }
}
//this function reverse queue element
void reverse()
{
    int i;
    for(i=f;i<=r;i++)
    {
        push(dequeue());
    }
    for(i=top;i>=0;i--)
    {
        enqueue(pop());
    }
}
int main()
{
    int n,i,val;
    printf("Enter size of queue");
    scanf("%d",&n);
    for(i=0;i
Reverse Queue Elements Using Stack
In this image show the output of above program
Reverse Queue Elements Using Stack In Solution C

Zala Digvijaysinh

MCA student at Dharamsinh Desai University


Leave a Comment

Your email address will not be published. Required fields are marked *