Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 08:40 PM
JC
Guest
 
Posts: n/a
Default Inserting into a queue

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


  #2  
Old July 19th, 2005, 08:40 PM
Jerry Coffin
Guest
 
Posts: n/a
Default Re: Inserting into a queue

In article <r6udnXQS-fkAZAaiRVn-gA@comcast.com>, keepit@secret.com
says...[color=blue]
> 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)[/color]

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.
  #3  
Old July 19th, 2005, 08:41 PM
Simon Elliott
Guest
 
Posts: n/a
Default Re: Inserting into a queue

JC <keepit@secret.com> writes[color=blue]
>
>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.[/color]

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/






  #4  
Old July 19th, 2005, 08:43 PM
JC
Guest
 
Posts: n/a
Default Re: Inserting into a queue

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" <jcoffin@taeus.com> wrote in message
news:MPG.1a05bc7d78f8b0c5989bb1@news.clspco.adelph ia.net...[color=blue]
> In article <r6udnXQS-fkAZAaiRVn-gA@comcast.com>, keepit@secret.com
> says...[color=green]
> > 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[/color][/color]
this[color=blue][color=green]
> > (3,5,6)[/color]
>
> 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.[/color]


  #5  
Old July 19th, 2005, 08:43 PM
Christian Brechbühler
Guest
 
Posts: n/a
Default Re: Inserting into a queue

JC wrote:[color=blue]
> 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()
> {[/color]
[...][color=blue]
> if (rem == Q.queuearray[QCount] )
> {
> cout << "\n\tI found number "<<rem";
> Q.delete_queue(QCount);
>
> while ( !(Q.empty_queue()) )[/color]
[...]
[color=blue]
> }
>
> "Jerry Coffin" <jcoffin@taeus.com> wrote[color=green]
>> Posting a minimal piece of compilable code that demonstrates what you're
>> doing is also _very_ helpful.[/color][/color]

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

  #6  
Old July 19th, 2005, 08:48 PM
Jerry Coffin
Guest
 
Posts: n/a
Default Re: Inserting into a queue

In article <UrGdnbNzptCPVgCiRVn-iw@comcast.com>, keepit@secret.com
says...[color=blue]
> Hi Jerry,
>
> here is the main code, I've writen so far. Let me know if you need to see
> more....[/color]

The most important thing I need to see is at least some attempt at
explaining what you're really trying to accomplish.

Right now, your code is written to say that it's a queue, but you seem
to want to do things that simply aren't possible with a queue. In
particular, with a queue you really only have two operations: you can
add items one end, and you can remove items from the other end. That's
it.

Right now, your code _seems_ intended to be able to find an item with a
particular value, and remove it from wherever it is -- potentially from
the middle of the collection. If that's what you want, then a queue is
NOT what you want. There are (various) data structures that will
support that, but a queue is not among them.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles