"Vmz" <al**************@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
HI,
I got two queues, I need to write void function that would check both
queues and remove from first queue all nodes that matches second queue.
Each node has two pointers that shows to previous and next nodes. Here
is what i got so far:
void check(queue que1, queue que2) {
node *temp
temp->next = Galimos.Pirmas;
for(;que1.First != NULL;) {
for(;que2.First != NULL; ) {
if(equal(que1.First->State, que2.First->State)) {
if(que1.First = tempp->next) {
que1.First = que1.First->next;
}
else {
que1.First->previous->next = que1.First->next;
}
que2.First = que2.First->next;
}
que1.First = que1.First->next;
}
}
}
================================================== ======
This seems to be wrong somewhere.
That tells us nothing. What specifically is 'wrong'. Doesn't
compile? Wrong results? (if so, state the expect results and
actual results). Also note that you have not provided enough
information to make any meaningful analysis.
Suggestions are wellcome :-)
OK I'll do what I can with the little you presented.
First, I'll assume that your 'rolling your own' queue type is
for academic purposes; otherwise I'd recommend to use the
standard library queue type.
Rather than trying to do the impossible task of analyzing that
without sufficient context, I'll offer more general advice:
Your stated specification implies several needed facilites:
0) The fundamental operations one would expect from a
queue type (e.g. add a node, delete a node, traverse
the nodes, etc.) I can only assume this has already
been done (and tested).
1) Iterate through a queue from beginning to end, being
able to extract the data value from any particular
node along the way. (If you've written your queue
type to be standard library container-compatible,
there are standard library functions you can use
'out of the box'.)
2) Remove a node from a queue
3) Compare the data value of a node from one queue with
that of a node from another queue.
Write (and thoroughly test) separate functions (most
should be member functions of 'queue') for each
of these tasks; then it should be a simple matter to
combine these to form the ultimate solution.
A couple examples of what I mean:
In your example above, in your 'check()' function, you
appear to be trying to write the low-level logic for
traversing a queue and removing a node. Those operations
don't belong in that function. They each belongs in their
own separate functions, which 'check()' can call when needed.
You repeatedly use expressions such as 'que1.First->State'.
This implies that your client code has direct access to
the 'queue' type internal details. Not Good, and very
error prone. IMO you should have created an 'accessor'
member function to get your node 'value', and call it as
needed. E.g. if(que1.state() == que2.state())
You're doing 'low level' processing in what is essentially
a 'high level' function. Manipulation of e.g. 'next' and
'previous' pointers should be hidden details in the queue
type. (For example as part of functions such as 'next()',
'prev()', 'insert()', 'remove()', 'operator==()', etc.)
'Higher level' code such as your above function should
call these functions to perform its work.
Separating your code into discrete functional units has
many advantages, such as quicker development time, ability
to later reuse code, easier testing and debugging, and
much more effective maintenance.
Also consider passing your arguments by reference rather
than by value. When your queue objects get large, you
could see a reduction in performance.
-Mike