Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include <stdio.

h>
#include <stdlib.h>
// structure to represent a stack
struct Stack
{
int capacity;
int top;
int *array;
};
// function to create a stack of given capacity
struct Stack *craetestack(int capacity)
{
struct Stack *stack = (struct Stack *)malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (int *)malloc(stack->capacity * sizeof(int));
return;
}
struct Queue
{
struct Stack *stack1;
struct Stack *stack2;
};
struct Queue *createQueue()
{
struct Queue *Queue = (struct Queue *)malloc(sizeof(struct Queue));
}
int isEmpty(struct Stack *stack)
{
return stack->top == -1;
}
void push(struct Stack *stack, int item)
{
if (isFull(stack))
return;
stack->array[++stack->top] = item;
}
from SHREEDEVI SURESH (internal) to everyone: 8:22 PM
int isEmpty(struct Stack *stack)
{
return stack->top == -1;
}
void push(struct Stack *stack, int item)
{
if (isFull(stack))
return;
stack->array[++stack->top] = item;
}
int isFull(struct Stack *stack)
{
return stack->top == stack->capacity - 1;
}
int pop(struct Stack *stack)
{
if (isEmpty(stack))
return -1;
return stack->array[stack->top--];
}
void enqueue(struct Queue *queue, int item)
{
while (!isEmpty(queue->stack1))
{
push(queue->stack2, pop(queue->stack1));
}
push(queue->stack1, item);
while (!isEmpty(queue->stack2))
{
push(queue->stack1, pop(queue->stack2));
}
}
int dequeue(struct Queue *queue)
{
if (isEmpty(queue->stack1))
return -1;
return pop(queue->stack1);
}
void display(struct Queue *queue)
{
if (isEmpty(queue->stack1))
printf("Queue is empty\n");
return;
for (int i = queue->stack1->top; i >= 0; i--)
{

from SHREEDEVI SURESH (internal) to everyone: 8:23 PM


int main()
{
struct Queue *queue = createQueue();
while (1)
{
int ch, ele;
printf("1.Enqueue\n");
printf("2.dequeue\n");
printf("3.display\n");
printf("4.Quit\n");
printf("enter your choice\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("enter the element\n");
scanf("%d", &ele);
enqueue(queue, ele);
break;
case 2:
dequeue(queue);
break;
case 3:
display(queue);
break;
case 4:
exit(0);
defualt:
printf("invalid choice\n");
}
}
return 0;
}

You might also like