Thread: Edit stack code to make queue

  1. #1
    Registered User
    Join Date
    Nov 2008

    Edit stack code to make queue

    edit: delete this post. Figured out problem.
    Last edited by mikeman; 02-15-2010 at 09:50 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Quote Originally Posted by mikeman
    I'm attempting to alter a stack class and make it a queue. I don't think it should be that difficult, as I'm essentially changing it from first in last out (stack) to first in first out (queue).
    It is not quite so easy, methinks. The stack class that you have is implemented with a fixed size array. It is rather easy to implement a stack like this because you just need to add/remove elements from the end of the array by keeping track of the array size (and copying new elements, of course).

    But with a queue, this becomes problematic: you need to implement a ring buffer, or periodically shift elements (which tends to be slow), otherwise your queue's maximum size would be reduced as elements are removed from the queue, until you eventually have a queue that cannot store anything. You may find it easier to use a tailed singly linked list instead.

    (That said, std::queue uses a std::deque by default, as does std::stack.)
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Pushing a Queue Onto Stack
    By programming1985 in forum C Programming
    Replies: 4
    Last Post: 10-22-2008, 07:44 PM
  3. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  4. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 09:38 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM