Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8420041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:46:57+00:00 2026-06-10T02:46:57+00:00

I have implemented a Queue using 2 Stacks in Java where I follow this

  • 0

I have implemented a Queue using 2 Stacks in Java where I follow this algorithm:

enQueue(x)
Push x to stack1

deQueue()
1) If both stacks are empty then error.
2) If stack2 is empty while stack1 is not empty, push everything from stack1 to stack2.
3) Pop the element from stack2 and return it.

Now, the problem here is that the first deQueue() operation is very slow (since it transfers everything to stack2). Can we modify the algorithm somehow so that deQueue is O(1) always? Are there other alternatives?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:46:58+00:00Added an answer on June 10, 2026 at 2:46 am

    You can swap the two stacks.

    deQueue()

    1. If both stacks are empty then error.
    2. If stack2 is empty, while
      stack1 is not empty, swap the two stacks.
    3. Pop the element from
      stack2 and return it.

    Using a swap, the operation is always O(1)

    If you need a FIFO queue, use two queues. Only use a stack if you need LIFO behaviour.

    If this is the case, there is no difference between using one queue or two queues, so you may as well use just one queue. If you are using threads, use an ExecutorService which wraps a Queue and a thread pool.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a java program . This is basically a multi threaded service
Suppose we have a shared queue (implemented using an array), which two threads can
I have implemented a chat service and desktop client using Java, the program works
I have implemented correctly bump's api, and added this code: - (void) configureBump {
I have implemented clean URLs using the following in my .htaccess RewriteEngine on RewriteCond
I have implemented a very basic sign up using email address+name, although I would
I have a question regarding LinkedList that I'm using to implement a queue. How
I have two queues, one is implemented using an array for storage and the
I have implemented a Bellman-Ford algorithm to solve a problem (with a graph), but
I have got a Spring MDP implemented using Spring DefaultMessageListenderContainer listening to an input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.