473,385 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Having problem with Queues

Vmz
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. Suggestions are wellcome :-)

Nov 23 '05 #1
8 1626
Vmz
Here is how queue :: add function works :

==================================================
void queue :: add(node state) {
node * temp;
temp = new node(state);
if(First == NULL) {
temp->next = NULL;
temp->previous = NULL;
First = temp;
Last = temp;
}
else {
if(temp->heuristics <= First->heuristics) {
temp->next = First;
First->previous = temp;
First = temp;
}
else {
Last->next = temp;
temp->previous = Last;
Last = temp;
temp->next = NULL;
}
}
}

================================================== =========

Nov 23 '05 #2

"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
Nov 23 '05 #3

"Vmz" <al**************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Here is how queue :: add function works :

==================================================
void queue :: add(node state) {
node * temp;
temp = new node(state);
if(First == NULL) {
temp->next = NULL;
temp->previous = NULL;
First = temp;
Last = temp;
}
else {
if(temp->heuristics <= First->heuristics) {
temp->next = First;
First->previous = temp;
First = temp;
}
else {
Last->next = temp;
temp->previous = Last;
Last = temp;
temp->next = NULL;
}
}
}

================================================== =========


Presumably you also have the counterpart to 'add()' --
'remove()' (or whatever name you gave it). Don't you
think it would make sense to have 'check()' call 'remove()'
instead of essentially writing that logic over again (with
the very real possibility of not getting it right?)

The same principle applies to other things 'check()' is
doing, e.g. traversing your queue. You should have
member functions for doing these 'general' operations,
and call them from higher level code (i.e. don't keep
writing the logic over and over all over the place).
That is the recipe for 'spaghetti', a.k.a. 'maintenance
nightmare'.

Another basic operation you'll want to include (if you
haven't already) in your queue type to support 'check()',
is a 'find()' function.

-Mike
Nov 23 '05 #4
"vmz" wrote
if(que1.First = tempp->next)


I do not see where tempp is defined (could you mean temp?) Also,
shouldn't you be using "==" instead of "="?

Nov 23 '05 #5
Vmz
argh, sorry, for bit messing up. when i was translating to english made
some mistakes, it should be:

void check(queue que1, queue que2) {
node temp
temp->next = que1.First;
//Remembering first element
for(;que1.First != NULL;) {
for(;que2.First != NULL; ) {
if(equal(que1.First->State, que2.First->State)) { //checking if
nodes of queues are

//equal, if yes then ..
if(que1.First == tempp->next) {
//checking if its the first node
que1.First = que1.First->next; //Changing que1.First
pointer to second node
}
else { //if the node which is equal is somewhere in the
middle of queue
//remove it from middle of queue
que1.First->previous->next = que1.First->next;
}
que2.First = que2.First->next;
}
que1.First = que1.First->next;
}
}
}
=======================================
I did some little explanation here. I think the problem is in the
second part when I have to remove node from the middle of queue. Could
any1 supply my with algorithm that I could implement it having
queue.First and queue.Last pointers ?

Nov 23 '05 #6
Vmz
> 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.


It does compile, the program works, one of my tasks is to check for
repeated states, so i make to queues, one with present states(nodes)
and second with those which already happened. The program gives me
wrong results after ~10 checks, and the two states start repeat. for
example:
-------
|1|5|6|
-------
|0|2|3|
-------
|7|8|4|
-------
|
\/
-------
|1|5|6|
-------
|2|0|3|
-------
|7|8|4|
-------
|
\/
-------
|1|5|6|
-------
|0|2|3|
-------
|7|8|4|

======================================
I'm making 8-puzzle game.

Nov 23 '05 #7

"Vmz" <al**************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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.


It does compile, the program works, one of my tasks is to check for
repeated states, so i make to queues, one with present states(nodes)
and second with those which already happened. The program gives me
wrong results after ~10 checks, and the two states start repeat. for
example:


The occurrence of exactly these kinds of problems is why
I strongly recommend a much more modular design: small
discrete functions which each doing only a single task;
each fully tested and debugged. That makes the higher
level stuff much easier to implement, debug and maintain.

And even without redesigning your code, you've still not
given nearly enough information (code) to do any kind
of analysis.

-Mike
Nov 23 '05 #8
Vmz wrote:
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. Suggestions are wellcome :-)


I'll second the suggestions of Mike Wahler, he gave a lot of good advice
which you should not ignore, even though it wasn't what you wanted to hear.

However there is one error I can see even from what you've posted.

void check(queue que1, queue que2) {
node *temp
temp->next = Galimos.Pirmas;

'temp' is an uninitialised pointer, but you are using it as if it
pointed somewhere meaningful.

john
Nov 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: AlanR | last post by:
Hi, I have to implement(or possibly acquire) a queue that manages priorities. Low priority elements cannot get lost in the queue indefinitely if there is a sustained, constant flow of high...
0
by: Aki Niimura | last post by:
Hello everyone. I'm trying to control a program from a Python program using IPC. Although using a socket is a common choice for such applications, I would like to use SysV message queues...
1
by: steve | last post by:
hi, during a full export of my export of a (Release 9.2.0.4.0 - Production ) database i'm getting the following: I have been thru metalink, and can find nothing that is exactly the same, even...
1
by: Aravind | last post by:
Want to know how robust UNIX queues are? How many queues can be created at a time in AIX platform ? What is the maxsize that we allocate for a queue in AIX platform?
4
by: Tingo | last post by:
Hi all, Is it possible to create a message queue with a specific ID? I want to do this because I'm trying to write a piece of software which restores communicating processes (which communicate...
4
by: aba | last post by:
I'm trying to write a c application that calculates the average time it takes for someone in line at an ice cream stand to get an ice cream cone. But, I don't really know where to start. Does...
1
by: Carl Griffin | last post by:
I am working an an application to enumerate users and permissions they have to read certain fax queues on a fax server. The problem I am having is that I can't figure out a way to let the admin...
6
by: rh0dium | last post by:
Hi Experts!! I am trying to get the following little snippet to push my data to the function func(). What I would expect to happen is it to print out the contents of a and loglevel. But it's...
5
by: ravi | last post by:
Can any body tell me How to implement a stack using two queues Thax in advance
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.