0% found this document useful (0 votes)
33 views

Queue Using Stack

This document defines the data structures and functions for implementing a queue using two stacks. It defines a Stack struct to represent a stack with capacity, top pointer, and array. It includes functions to create and manipulate stacks like push(), pop(), and isEmpty(). It also defines a Queue struct containing two Stack pointers and includes functions to enqueue and dequeue items to/from the queue by transferring items between the two stacks.

Uploaded by

prathamshetty874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Queue Using Stack

This document defines the data structures and functions for implementing a queue using two stacks. It defines a Stack struct to represent a stack with capacity, top pointer, and array. It includes functions to create and manipulate stacks like push(), pop(), and isEmpty(). It also defines a Queue struct containing two Stack pointers and includes functions to enqueue and dequeue items to/from the queue by transferring items between the two stacks.

Uploaded by

prathamshetty874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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