WAP to implement a queue using one stack.
#include <stdio.h>
#include <stdlib.h>
void P1(int);
void P2(int);
int PopN();
int PopM();
void EnQ();
void deQ();
void show();
void make();
int stack1[100], stack2[100];
int topN = -1, top2 = -1;
int count = 0;
int main()
{
int option;
printf("\nQUEUE USING STACKS IMPLEMENTATION\n");
printf("\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.EXIT\n");
make();
while (1)
{
printf("\nEnter your option : ");
scanf("%d", &option);
switch (option)
{
case 1:
EnQ();
break;
case 2:
deQ();
break;
case 3:
show();
break;
case 4:
exit(0);
default:
printf("\nInvalid Choice\n");
}}}
void make()
{
topN = top2 = -1;
}
void P1(int element)
{
stack1[++topN] = element;
}
int PopN()
{
return(stack1[topN--]);
}
void P2(int element)
{
stack2[++top2] = element;
}
int PopM()
{
return(stack2[top2--]);
}
void EnQ()
{
int data, i;
printf("Enter the data : ");
scanf("%d", &data);
P1(data);
count++;
}
void deQ()
{
int x;
for (x = 0;x <= count;x++)
{
P2(PopN());
}
PopM();
count--;
for (x = 0;x <= count;x++)
{
P1(PopM());
}}
void show()
{
int i;
if(topN == -1)
{
printf("\nQUEUE is Empty\n");
}
else
{
printf("\nQUEUE ELEMENTS : ");
for (int x = 0;x <= topN;x++)
{
printf(" %d ", stack1[x]);
}
printf("\n");
}}
Comments
Leave a comment