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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:59:10+00:00 2026-06-07T12:59:10+00:00

I have a queue with n elements in it and the front is at

  • 0

I have a queue with n elements in it and the front is at 0. I need to create a stack of these numbers with 0 at the top.

It can only be done with EnQueue, DeQueue, Push, and Pop, and constant storage. I dont need an answer so much as an idea of how I could approach this problem.

Please don’t answer this for me, but just try to understand I’m new at programming and could just use an idea of what is a way this can be done.

  • Is it a Towers-of-Hanoi-like approach?
  • Does that only use a constant storage?

This isnt for homework, I just need some advice on how to proceed. My first idea, reversing the queue and then pushing it did not work. I even tried sketching out other situations with no avail. Then I wondered if dequeueing and pushing them all, then popping and enqueueing them all, then dequeue and push again.

  • Is this efficient?
  • Does this use constant storage?

I am still learning fundamental programming concepts. Please be nice! 🙂

  • 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-07T12:59:12+00:00Added an answer on June 7, 2026 at 12:59 pm

    What am I facing?

    The biggest problem you are facing is that your two containers aren’t directly compatible with each other.

    A queue is normally a FIFO1 container, while a stack is LIFO2. This means that you cannot just copy the data in sequential order from your queue to your stack, since that will make the elements appear in the "wrong" order (following your description).

    Another problem is that there is no good way (performance wise) to reverse a queue. A queue is a one-way container, internally an element only has to know about the next element in line, not about the previous one. This means that you cannot iterate through the queue starting at the back, and that iteration always is O(n).

    The same problem is with your stack.

    The things described earlier put together makes this quite a tedious problem, though there are solutions they aren’t always the most straight forward.


    Hints on how to solve this issue..

    You’ll need some sort of intermediate state to store your elements, or could we use the LIFO/FIFO properties of our containers to our advantage?

    Below is an implementation which does what you want, if you don’t want to know the answer to your question don’t hover with your mouse over this gray area.

    It will require some additional storage since space for an extra element will be allocated during copying from one container to another.. this is inevitable, though the storage is constant.

    Remember that the copy-initialization can be optimized by using rvalue-references and move in C++11.

    Can’t seem to get syntax highlighting working inside a spoiler..

    Sample implementation can be found here.

    It takes advantage of the fact that a queue is FIFO and stack LIFO, by copying the data queue to stack, then stack to queue and finally queue to stack again we have effectively reversed the order of elements in a way that will match your description.


    footnotes
       1. FIFO = First In First Out
       2. LIFO = Last In First Out

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

Sidebar

Related Questions

I have implemented a queue in C. I can add and remove elements. Now
I want to create a queue of Strings that will have many identical elements
I have a C# Queue<TimeSpan> containing 500 elements. I need to reduce those into
I have a Queue that contains a collection of objects, one of these objects
i have a question regarding this Haskell code: module Queue (Queue, emptyQueue, queueEmpty, enqueue,
starting with priority_queue s, I have a problem like this: I need elements to
I have two threads. Thread A is pulling some elements from queue and thread
Suppose that I have a priority queue which removes elements in increasing order, and
I need a storage type that I can set a max number of elements
how can i have jquery switch between the array below on an elements font

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.