Like Tree1Likes
  • 1 Post By Salem

Stacks and Queues

This is a discussion on Stacks and Queues within the C Programming forums, part of the General Programming Boards category; Hi Iam trying to make a queue only by using one stack, but i really cannot figure out how to. ...

  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
    21,461
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    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
    }
    antros48 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 10: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, 04:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21