Open links in new tab
  1. Copilot Search Branding
    • Work Report
    • Email
    • Rewrite
    • Speech
    • Title Generator
    • Smart Reply
    • Poem
    • Essay
    • Joke
    • Instagram Post
    • X Post
    • Facebook Post
    • Story
    • Cover Letter
    • Resume
    • Job Description
    • Recommendation Letter
    • Resignation Letter
    • Invitation Letter
    • Greeting Message
    • Try more templates
  1. 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 queue
    typedef struct Queue {
    int *arr;
    int front, rear, size, capacity;
    } Queue;

    // Function to create a queue
    Queue* 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 empty
    int isEmpty(Queue* queue) {
    return (queue->size == 0);
    }

    // Enqueue an element
    void enqueue(Queue* queue, int item) {
    if (queue->size == queue->capacity) return; // Overflow condition
    queue->rear = (queue->rear + 1) % queue->capacity;
    queue->arr[queue->rear] = item;
    queue->size++;
    }

    // Dequeue an element
    int dequeue(Queue* queue) {
    if (isEmpty(queue)) return -1; // Underflow condition
    int item = queue->arr[queue->front];
    queue->front = (queue->front + 1) % queue->capacity;
    queue->size--;
    return item;
    }

    // Get the front element of the queue
    int front(Queue* queue) {
    if (isEmpty(queue)) return -1;
    return queue->arr[queue->front];
    }

    // Define a structure for the stack
    typedef struct Stack {
    Queue *q1, *q2;
    } Stack;

    // Create a stack using two queues
    Stack* 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 q2
    while (!isEmpty(stack->q1)) {
    enqueue(stack->q2, dequeue(stack->q1));
    }

    // Swap q1 and q2
    Queue* 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 stack
    int top(Stack* stack) {
    return front(stack->q1); // Front of q1 is the top of the stack
    }

    // Main function to test the implementation
    int main() {
    Stack* stack = createStack(10);

    push(stack, 10);
    push(stack, 20);
    push(stack, 30);

    printf("Top element: %d\n", top(stack)); // Output: 30
    printf("Popped: %d\n", pop(stack)); // Output: 30
    printf("Top element: %d\n", top(stack)); // Output: 20

    return 0;
    }
    Copied!
    Feedback
  2. 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 …

  3. 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 …

  4. 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.

  5. 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
        Loading
        Unable to load answer
      By using this site you agree to the use of cookies for analytics, personalized content, and ads.Learn more about third party cookies|Microsoft Privacy Policy