Inserting into a queue

Discussion in 'C++' started by JC, Oct 26, 2003.

  1. JC

    JC Guest

    Hello all,

    I was wondering if someone can give me some tips on this problem.

    I'm trying to take content of a queue (Xqueue) and copy it into another
    queue (Yqueue)
    I tried using a while and/or for loop.

    for example if queue X contains numbers (3,5,8,6)
    I want to copy number (3,5) into queue Y, then I want to delete number 8
    from queue X.
    I then want to copy number (6) to queue Y (to keep them in order)
    I'll then move them back to queue X which at this point will look like this
    (3,5,6)

    Thanks in advance you for all your help
    JC
    JC, Oct 26, 2003
    #1
    1. Advertising

  2. JC

    Jerry Coffin Guest

    In article <>,
    says...
    > Hello all,
    >
    > I was wondering if someone can give me some tips on this problem.
    >
    > I'm trying to take content of a queue (Xqueue) and copy it into another
    > queue (Yqueue)
    > I tried using a while and/or for loop.
    >
    > for example if queue X contains numbers (3,5,8,6)
    > I want to copy number (3,5) into queue Y, then I want to delete number 8
    > from queue X.
    > I then want to copy number (6) to queue Y (to keep them in order)
    > I'll then move them back to queue X which at this point will look like this
    > (3,5,6)


    Why don't you try to tell us more precisely what you want as a result
    instead of the way you're trying to produce that result?

    Right now, it's not clear exactly what criteria you're using to decide
    that 8 should be removed from the queue, nor is it clear exactly why you
    have two queues involved. Just for example, something like this might
    be useful: "I have an std::queue of int's and I want to remove from it
    any item that is greater than the number that follows it."

    Posting a minimal piece of compilable code that demonstrates what you're
    doing is also _very_ helpful.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Oct 26, 2003
    #2
    1. Advertising

  3. JC <> writes
    >
    >I'm trying to take content of a queue (Xqueue) and copy it into another
    >queue (Yqueue)
    >I tried using a while and/or for loop.


    If the queue is an STL container, would the insert() member function be
    what you're looking for?

    You can do things like:

    std::vector<int> foo;
    std::vector<int> bar;
    std::vector<int>::iterator foo_ptr;

    foo.push_back(42);
    foo.push_back(43);

    bar.push_back(1);
    bar.push_back(2);

    foo_ptr = std::find(foo.begin(), foo.end(), 42);
    if (foo_ptr != foo.end())
    {
    foo.insert(foo_ptr, 41);
    }

    // The iterator may have been invalidated so need to find "42" again.
    foo_ptr = std::find(foo.begin(), foo.end(), 42);
    if (foo_ptr != foo.end())
    {
    foo.insert (foo_ptr, bar.begin(), bar.end());
    }


    --
    Simon Elliott
    http://www.ctsn.co.uk/
    Simon Elliott, Oct 27, 2003
    #3
  4. JC

    JC Guest

    Hi Jerry,

    here is the main code, I've writen so far. Let me know if you need to see
    more....

    Thanks
    JC

    -----------------
    int main()
    {
    queue_num Q;
    queue_tempg T;
    int QCount, i, n, count;
    // char ans;

    Q.clear_queue();
    T.clear_tempg();
    i = 0;
    QCount = 0;
    count = 0;

    cout <<"\n\tEnter a number\n\t==> ";
    while ( !(Q.full_queue() ) )
    {
    cin >> n;
    Q.insert_queue(n);
    QCount++;
    count++;
    }

    int rem;
    cout <<"\n\tEnter a number to remove==> ";
    cin >> rem;

    if ( !(Q.empty_queue()) )
    {
    QCount = 0;
    while ( !(Q.empty_queue() ) && ( QCount <= maxqueue ) )
    {
    if (rem == Q.queuearray[QCount] )
    {
    cout << "\n\tI found number "<<rem";
    Q.delete_queue(QCount);

    while ( !(Q.empty_queue()) )
    {
    n = Q.queuearray[QCount];
    T.insert_tempg(n);
    QCount++;
    }
    }
    else
    {
    n = Q.queuearray[QCount];
    cout << "\n\tNumber "<<rem<<" was NOT found";
    T.insert_tempg(n);
    QCount++;
    }
    }
    }
    ---------------------------------
    "Jerry Coffin" <> wrote in message
    news:...
    > In article <>,
    > says...
    > > Hello all,
    > >
    > > I was wondering if someone can give me some tips on this problem.
    > >
    > > I'm trying to take content of a queue (Xqueue) and copy it into another
    > > queue (Yqueue)
    > > I tried using a while and/or for loop.
    > >
    > > for example if queue X contains numbers (3,5,8,6)
    > > I want to copy number (3,5) into queue Y, then I want to delete number 8
    > > from queue X.
    > > I then want to copy number (6) to queue Y (to keep them in order)
    > > I'll then move them back to queue X which at this point will look like

    this
    > > (3,5,6)

    >
    > Why don't you try to tell us more precisely what you want as a result
    > instead of the way you're trying to produce that result?
    >
    > Right now, it's not clear exactly what criteria you're using to decide
    > that 8 should be removed from the queue, nor is it clear exactly why you
    > have two queues involved. Just for example, something like this might
    > be useful: "I have an std::queue of int's and I want to remove from it
    > any item that is greater than the number that follows it."
    >
    > Posting a minimal piece of compilable code that demonstrates what you're
    > doing is also _very_ helpful.
    >
    > --
    > Later,
    > Jerry.
    >
    > The universe is a figment of its own imagination.
    JC, Oct 28, 2003
    #4
  5. JC wrote:
    > Hi Jerry,
    >
    > here is the main code, I've writen so far. Let me know if you need to see
    > more....
    >
    > Thanks
    > JC
    >
    > -----------------
    > int main()
    > {

    [...]
    > if (rem == Q.queuearray[QCount] )
    > {
    > cout << "\n\tI found number "<<rem";
    > Q.delete_queue(QCount);
    >
    > while ( !(Q.empty_queue()) )

    [...]

    > }
    >
    > "Jerry Coffin" <> wrote
    >> Posting a minimal piece of compilable code that demonstrates what you're
    >> doing is also _very_ helpful.


    Hi JC,

    your code is not compilable. One problem is that run-away string constant -- see
    that stray double quote at the end of the line writing to cout.

    And please don't top-post here. Thanks

    Christian
    =?ISO-8859-1?Q?Christian_Brechb=FChler?=, Oct 28, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul L. Du Bois

    Queue.Queue-like class without the busy-wait

    Paul L. Du Bois, Mar 24, 2005, in forum: Python
    Replies:
    29
    Views:
    1,044
    Antoon Pardon
    Apr 4, 2005
  2. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    672
    Russell Warren
    Jun 27, 2006
  3. Kceiw
    Replies:
    3
    Views:
    983
    Jim Langston
    Mar 14, 2006
  4. Gabriel Rossetti
    Replies:
    3
    Views:
    534
    Jerry Hill
    Apr 25, 2008
  5. Kris
    Replies:
    0
    Views:
    467
Loading...

Share This Page