Develop an algorithm to implement Queue using Stack data structure.
#include <stdio.h>
#include <stdlib.h>
void pushX(int);
void pushY(int);
int popX();
int popY();
void En();
void De();
void output();
void create();
int stack1[50], stack2[50];
int topX = -1, topY = -1;
int count = 0;
int main()
{
int option;
printf("\nImplementin Queue using stack \n\n");
printf("\n1.ENQUEUE");
printf("\n2.DEQUEUE");
printf("\n3.DISPLAY");
printf("\n4.EXIT");
printf("\n");
create();
while (1)
{
printf("\nEnter your option : ");
scanf("%d", &option);
switch (option)
{
case 1:
En();
break;
case 2:
De();
break;
case 3:
output();
break;
case 4:
exit(0);
default:
printf("\nInvalid option\n");
}}}
void create()
{
topX = topY = -1;
}
void pushX(int element)
{
stack1[++topX] = element;
}
int popX()
{
return(stack1[topX--]);
}
void pushY(int element)
{
stack2[++topY] = element;
}
int popY()
{
return(stack2[topY--]);
}
void En()
{
int info, i;
printf("Enter the info : ");
scanf("%d", &info);
pushX(info);
count++;
}
void De()
{
int i;
for (i = 0;i <= count;i++)
{
pushY(popX());
}
popY();
count--;
for (i = 0;i <= count;i++)
{
pushX(popY());
}}
void output()
{
int i;
if(topX == -1)
{
printf("\nQueue is empty\n");
}
else
{
printf("\nElements of the queue : ");
for (i = 0;i <= topX;i++)
{
printf(" %d ", stack1[i]);
}
printf("\n");
}}
Comments
Leave a comment