0% found this document useful (0 votes)
1 views3 pages

8.reverse A Queue Using Stack

The document contains a C program that demonstrates how to reverse a queue using a stack. It includes functions for enqueueing and dequeueing elements from the queue, as well as pushing and popping elements onto and from the stack. The program prompts the user to input elements, displays the original queue, reverses it, and then displays the reversed queue.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

8.reverse A Queue Using Stack

The document contains a C program that demonstrates how to reverse a queue using a stack. It includes functions for enqueueing and dequeueing elements from the queue, as well as pushing and popping elements onto and from the stack. The program prompts the user to input elements, displays the original queue, reverses it, and then displays the reversed queue.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Reverse a queue using stack:

Program:
#include <stdio.h>
#define n 5

int queue[n];
int stack[n];
int top,rear,front;
top=-1;
rear=-1;
front=-1;

void enqueue(int x)
{
if(front==-1 && rear==-1)
{
front=rear=0;
queue[rear]=x;
}
else
{
rear++;
queue[rear]=x;
}
}
void dequeue()
{
int y;
y=queue[front];
push(y);
if(front==rear)
front=rear=-1;
else
front++;
}
void push(int z)
{
top++;
stack[top]=z;
}
void pop()
{
int a;
a=stack[top];
top--;
enqueue(a);
}
void display()
{
int i;
if((front==-1)&&(rear==-1))
{
printf("\nQueue is empty");
}
else
{
for(i=front;i<=rear;i++)
{
printf("%d\t",queue[i]);
}
}
}
int main()
{
int i,data;
for(i=0;i<n;i++)
{
printf("\nEnter the element to be inserted to the queue");
scanf("%d",&data);
enqueue(data);
}
printf("\nThe elements in the queue are");
display();
for(i=0;i<n;i++)
{
dequeue();
}
for(i=0;i<n;i++)
{
pop();
}
printf("\nThe elements in the reversed queue are");
display();
return 0;
}

You might also like