double while loop in linked list

P

picknicker187

hi,

the following while function(it´s supposed to find word synonyms out of a
list) always quits after running through the list in the inner loop once.
it´s like the aktuell = aktuell->next on the bottom would not be there.
can anyone tell me what the problem might be?

greetz,

bastian


int checksyn(Relations *head)
{
char rel = 'S';
Relations *aktuell = head;
Relations *aktuell2 = head;

while (aktuell != NULL)
{
if(aktuell->relType == rel)
{
while (aktuell2 != NULL)
{
if(aktuell2->relType == rel && aktuell2->word == aktuell->dest &&
aktuell->word == aktuell2->dest)

printf("match found for: %c %s %s\n",aktuell->relType, aktuell->word,
aktuell->dest);
else
printf("no match found for: %c %s %s\n", aktuell->relType, aktuell->word,
aktuell->dest);
aktuell2 = aktuell2->next;
}

}
aktuell = aktuell->next;
}
return 0;
}
 
A

Artie Gold

picknicker187 said:
hi,

the following while function(it´s supposed to find word synonyms out of a
list) always quits after running through the list in the inner loop once.
it´s like the aktuell = aktuell->next on the bottom would not be there.
can anyone tell me what the problem might be?

greetz,

bastian


int checksyn(Relations *head)
{
char rel = 'S';
Relations *aktuell = head;
Relations *aktuell2 = head;

while (aktuell != NULL)
{
if(aktuell->relType == rel)
{

Once you've run througgh the inner loop, `aktuell2' is NULL.
while (aktuell2 != NULL)
{
if(aktuell2->relType == rel && aktuell2->word == aktuell->dest &&
aktuell->word == aktuell2->dest)

printf("match found for: %c %s %s\n",aktuell->relType, aktuell->word,
aktuell->dest);
else
printf("no match found for: %c %s %s\n", aktuell->relType, aktuell->word,
aktuell->dest);
aktuell2 = aktuell2->next;
}

}
aktuell = aktuell->next;
}
return 0;
}
HTH,
--ag

BTW - please post appropriately indented code (spaces, not tabs).
 
P

Pedro Graca

picknicker187 said:
the following while function(it´s supposed to find word synonyms out of a
list) always quits after running through the list in the inner loop once.
it´s like the aktuell = aktuell->next on the bottom would not be there.
can anyone tell me what the problem might be?

You need to restart aktuell2 before the inner while() loop.

'aktuell = aktuell->next;' is working fine.


The problem is that

while (aktuell2 != NULL) { /* ... */ }

never goes inside the loop the second (or later) time around.



[un-indented code snipped]

Do a favor to us and, especially, to yourself: indent your code.
 
K

Karthik Kumar

picknicker187 said:
hi,

the following while function(it´s supposed to find word synonyms out of a
list) always quits after running through the list in the inner loop once.
it´s like the aktuell = aktuell->next on the bottom would not be there.
can anyone tell me what the problem might be?

greetz,

bastian


int checksyn(Relations *head)

Please post the prototype of Relations.
or better, a compilable code.
{
char rel = 'S';
Relations *aktuell = head;
Relations *aktuell2 = head;

while (aktuell != NULL)
{
if(aktuell->relType == rel)
{
while (aktuell2 != NULL)
{
if(aktuell2->relType == rel && aktuell2->word == aktuell->dest &&
aktuell->word == aktuell2->dest)

Well - I am guessing that the members word and dest are
pointers to characters, from the next line .

Assuming that, aktuell->word == aktuell2->dest may not be what you
want in the previous line. It is comparing pointers, instead of
two C-strings. You may want 'strcmp' .
 

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,768
Messages
2,569,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top