sorting a queue

This is a discussion on sorting a queue within the C++ Programming forums, part of the General Programming Boards category; ok I know...I have already posted about sorting a linked list, and as anon said it would be better to ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    44

    sorting a queue

    ok I know...I have already posted about sorting a linked list, and as anon said it would be better to use sort() to do this rather doing it yourself, it was a good practice though

    How about sorting a queue. I was told that it is a possible interview question...How would you do it. I am not asking for code just some clues. I understand that I cannot use iterators to move through the queue. Am I to use only front and but??? But after that? How can I refer to a middle element in the queue?Can I refer to a middle element?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think that is the point of a queue. It is a container adaptor that represents the idea that you put things in at one end and they come out of the second end in the same order. Therefore it doesn't make sense to sort it.
    If you want them to come out of the other end in sorted order, you could use a priority_queue.

    Edit: and if you really want a sortable queue, then you might use a deque, which allows you to pop_front and push_back efficiently.
    Last edited by anon; 05-15-2008 at 08:42 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

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, 09:17 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 10:09 PM
  4. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 12:39 PM
  5. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 09:38 AM

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