Thread: convert queue into a stack

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    3

    convert queue into a stack

    I am a newbie so bear with me. Can anyone give me a simple sample code of how to empty a queue and put the contents into a stack. I need to check if the stack has room for the contents each time as well as check when the queue is empty. This can be in one function and simple just so I get an idea. Thans in advance.

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Ok, two things here:

    1. I'm am posting this because it appears that you are posting a homework assignment or other project and you are asking for someone else to do all of the work.

    Please don't ask people to do all your work for you, See the announcement on Homework at the top of the forum to see what is acceptable ro PM. Basically people are happy to help, but they're not going to do it all for you.

    Show us what you've got, show your code (using code tags), or where you're confused and someone will be happy to help you I'm sure. If it's something that you absolutely don't understand how it works, like you have no clue how qsort works, then ask a general question about the function and I'm sure someone will explain it. Though they may not give you all of the code for it, but someone will explain the concept.



    2. This is a fairly common topic, I know I've seen it quite a few times in the past. Why don't you use the board's Search function and look for it.

    If you honestly don't understnad the concept then try searching and if you still you have no clue then say so. I'm sure soeone will point you in the right direction.

    Then try your own, if you hit a brickwall, come back, someone will help

    Good Luck,
    Kermi3

    PS. I know some of that post was repetititive some of it was a "form" post hehe. Sorry for making you read things repetitively.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    I am using STL, here is what I have so far. Somehow I need to check if the stack is full or not before pushing. Or I need to determine what the overflow error returns and handle it somehow.

    Stack<int> stackNbrs;
    Queue<int> queueNbrs;

    while(!queueNbrs.empty()){
    stackNbrs.push(queueNbrs.retrieve());
    queueNbrs.serve();
    }

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is to push one element into the second object.

    - pop x
    - push x into y

    Kuphryn

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. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  4. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  5. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM