Answer to Question #247977 in C for xyz

Question #247977

Develop an algorithm to implement Queue using Stack data structure.


1
Expert's answer
2021-10-07T23:38:32-0400
#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");
}}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS