- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
This program demonstrates how to implement a stack using two queues such that the push operation runs in linear time (O(n)) and the pop operation runs in constant time (O(1)).
Approach
We use two queues (q1 and q2) to simulate stack behavior:
Push Operation (O(n)): The new element is enqueued into q2, and all elements from q1 are dequeued and enqueued into q2. Finally, the names of q1 and q2 are swapped.
Pop Operation (O(1)): The front element of q1 is dequeued.
C Code Implementation
#include <stdio.h>#include <stdlib.h>// Define a structure for the queuetypedef struct Queue {int *arr;int front, rear, size, capacity;} Queue;// Function to create a queueQueue* createQueue(int capacity) {Queue* queue = (Queue*)malloc(sizeof(Queue));queue->capacity = capacity;queue->front = 0;queue->size = 0;queue->rear = capacity - 1;queue->arr = (int*)malloc(capacity * sizeof(int));return queue;}// Check if the queue is emptyint isEmpty(Queue* queue) {return (queue->size == 0);}// Enqueue an elementvoid enqueue(Queue* queue, int item) {if (queue->size == queue->capacity) return; // Overflow conditionqueue->rear = (queue->rear + 1) % queue->capacity;queue->arr[queue->rear] = item;queue->size++;}// Dequeue an elementint dequeue(Queue* queue) {if (isEmpty(queue)) return -1; // Underflow conditionint item = queue->arr[queue->front];queue->front = (queue->front + 1) % queue->capacity;queue->size--;return item;}// Get the front element of the queueint front(Queue* queue) {if (isEmpty(queue)) return -1;return queue->arr[queue->front];}// Define a structure for the stacktypedef struct Stack {Queue *q1, *q2;} Stack;// Create a stack using two queuesStack* createStack(int capacity) {Stack* stack = (Stack*)malloc(sizeof(Stack));stack->q1 = createQueue(capacity);stack->q2 = createQueue(capacity);return stack;}// Push operation: O(n)void push(Stack* stack, int x) {enqueue(stack->q2, x); // Enqueue into q2// Move all elements from q1 to q2while (!isEmpty(stack->q1)) {enqueue(stack->q2, dequeue(stack->q1));}// Swap q1 and q2Queue* temp = stack->q1;stack->q1 = stack->q2;stack->q2 = temp;}// Pop operation: O(1)int pop(Stack* stack) {return dequeue(stack->q1); // Dequeue from q1}// Get the top element of the stackint top(Stack* stack) {return front(stack->q1); // Front of q1 is the top of the stack}// Main function to test the implementationint main() {Stack* stack = createStack(10);push(stack, 10);push(stack, 20);push(stack, 30);printf("Top element: %d\n", top(stack)); // Output: 30printf("Popped: %d\n", pop(stack)); // Output: 30printf("Top element: %d\n", top(stack)); // Output: 20return 0;}Copied!✕Copy Implement Stack using Queues - GeeksforGeeks
Sep 18, 2025 · We are implementing a stack using two queues (q1 and q2). The idea is to make the push (x) operation simple, and adjust the order during pop () and top () so that the stack behavior (Last-In …
See results only from geeksforgeeks.orgSign In
We are implementing a stack using two queues (q1 and q2). The idea is to make the push (x) operation simple, and adjust the order during pop () and top () so th…
Java LinkedList: How to Implement Stack and Queue Using ...
Jan 16, 2026 · This blog will guide you through using `LinkedList` to implement Stacks and Queues, leveraging Java’s built-in methods. We’ll cover core operations, code examples, edge cases, and best …
Write a Java program to Implement Stack using Queues
The code demonstrates how to implement a stack using queues in Java. Let's go through the code step by step: The code begins with the import statements import java.util.LinkedList; and import …
1. Implement stack using queue. Implementing a stack using ...
Oct 24, 2025 · Implementing a stack using queues is a fun challenge, as we need to simulate the Last-In-First-Out (LIFO) behavior of a stack using the First-In-First-Out (FIFO) behavior of a queue.
Implement Stack using Two Queues
Mar 27, 2009 · Given two queues with their standard operations (enqueue, dequeue, isempty, size), implement a stack with its standard operations (pop, push, isempty, size). There should be two …
- Reviews: 4
Implement Stack using Queues - LeetCode
Mar 4, 2011 · Implement Stack using Queues - Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the …
How can we Implement a Stack using Queue in Java?
This article will discuss how to implement a Stack using a Queue in Java. In Java, a Stack is a subclass (child class) of the Vector class, and it represents a LIFO stack of objects, which stands for Last-in …
225. Implement Stack using Queues - In-Depth Explanation
This problem asks you to implement a stack data structure using only two queues. A stack follows Last-In-First-Out (LIFO) principle, while a queue follows First-In-First-Out (FIFO) principle.
Java Coding Challenge - Implement Stack Using Two Queues
Sep 24, 2024 · The goal is to implement the basic operations of a stack— push(), pop(), peek(), and size() —using two queues. This challenge demonstrates a fundamental understanding of how data …
Implement a Stack Using Queues - HappyCoders.eu
Nov 27, 2024 · How to implement a stack with a queue (better said: with two …
- Reviews: 27
- People also ask
Deep dive into Implementing Stack Using Queues in Java