Inserting into a queue

J

JC

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
 
J

Jerry Coffin

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.
 
S

Simon Elliott

JC said:
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());
}
 
J

JC

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++;
}
}
}
---------------------------------
 
?

=?ISO-8859-1?Q?Christian_Brechb=FChler?=

JC said:
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 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top