Thread: Stacks and Queues

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    Stacks and Queues

    Hi
    Iam trying to make a queue only by using one stack, but i really cannot figure out how to. I don't know if that could be done without using any temporary place in memory
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by antros48
    Iam trying to make a queue only by using one stack, but i really cannot figure out how to. I don't know if that could be done without using any temporary place in memory
    I may be wrong, but I don't think it is possible to implement a queue using only one stack (and a small fixed number of helper variables). The problem is that you cannot access the first element pushed into the stack without popping all the other elements, but if you have no place to hold them, then you cannot pop them off the stack without losing them. (Note that I am assuming that you don't "cheat" by making use of the internals of the stack, e.g., by treating it as an array because it was implemented using an array).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well you could try something using recursion and the program stack to save each element.

    Something like
    Code:
    int pop_front ( &mystack ) {
      int temp = pop()
      if ( empty ) {
        result = temp
      } else {
        result = pop_front(mystack)
        push(temp)
      }
      return result
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check my answers on Stacks and Queues Please
    By ee1215 in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2011, 03:47 PM
  2. linked lists, stacks and queues
    By aniramg69 in forum C Programming
    Replies: 10
    Last Post: 11-29-2008, 11:58 AM
  3. Stacks and queues info
    By Emeighty in forum C++ Programming
    Replies: 4
    Last Post: 11-02-2008, 01:41 AM
  4. stacks and queues
    By j0hnb in forum C Programming
    Replies: 4
    Last Post: 04-16-2003, 09:44 PM
  5. using Stacks & Queues to compare Strings
    By eskimo083 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2003, 05:03 PM