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 7634343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:08:53+00:00 2026-05-31T07:08:53+00:00

Looking at some algorithm exercices on the net, I found an interesting one :

  • 0

Looking at some algorithm exercices on the net, I found an interesting one :

How would you implement a FIFO using a LIFO ?

I tried myself but I ended up with only one solution : each time we want the front element of the FIFO, copy the lifo into another lifo (excluding last element, which is the front), get the front element and remove it, then copy back the second LIFO into the first LIFO.

But this is of course horribly slow, it makes a simple loop like this :

for(!myfifo.empty()) {
  myfifo.pop();
}

going O(n²) instead of O(n) on a standard implementation of the FIFO.

Of course, LIFO are not made to do FIFO and we won’t certainly have the same complexity by using a “native” FIFO and a fake-FIFO based on a LIFO, but I think there is certainly a way of doing better than O(n²). Has anyone an idea about that ?

Thanks in advance.

  • 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-05-31T07:08:55+00:00Added an answer on May 31, 2026 at 7:08 am

    You can get amortized time complexity of O(1) per OP FIFO [queue] using 2 LIFOs [stacks].

    Assume you have stack1, stack2:

    insert(e):
       stack1.push(e)
    
    take():
       if (stack2.empty()):
          while (stack1.empty() == false):
                stack2.push(stack1.pop())
       return stack2.pop() //assume stack2.pop() handles empty stack already
    

    example:

    push(1)
    
    |1|  | |
    |-|  |-|
    
    push(2)
    |2|  | |
    |1|  | |
    |-|  |-|
    
    pop()
    push 2 to stack2 and pop it from stack1:
    |1|  |2|
    |-|  |-|
    push 1 to stack2 and pop it from stack2:
    | |  |1|
    | |  |2|
    |-|  |-|
    pop1 from stack2 and return it:
    | |  |2|
    |-|  |-|
    

    To get real O(1) [not amortized], it is much more complicated and requires more stacks, have a look at some of the answers in this post

    EDIT: Complexity analysis:

    1. each insert() is trivaially O(1) [just pushing it to stack1]
    2. Note that each element is push()ed and pop()ed at most twice, once from stack1 and once from stack2. Since there is no more ops then these, for n elements, we have at most 2n push()s and 2n pop()s, which gives us at most 4n * O(1) complexity [since both pop() and push() are O(1)], which is O(n) – and we get amortized time of: O(1) * 4n / n = O(1)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to implement 2D liquid simulator using GPU. I'm looking for some
I'm looking for some documentation on a heat map algorithm. I've found some implementations
I'm looking for a graph algorithm with some unusual properties. Each edge in the
I'm looking for an algorithm that rotates an image by some degrees (input). public
I'm looking for a practical application to use a genetic algorithm for. Some things
Looking for some sample code (C#) for a simple thread pool implementation. I found
Looking for some advice on the best way to implement localization along with client
I'm looking for some way of using the number-crunching ability of a GPU (with
I'm looking for an algorithm (or some other technique) to read the actual content
I am currently looking at this: some Genetic Algorithm This is some adapted code:

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.