Question

The following code;

typedef struct chainCell{
   int data;
   struct chainCell* next;
} chainCell;

bool sameValues (chainCell *x, chainCell *y)
{
   if ((x == NULL) & (y == NULL)) return true;
   if ((x == NULL) | (y == NULL)) return false;
   bool same = true;
   chainCell *xp = x, *yp = y; // scan pointers
   while ((xp != NULL) & (same == true)) // point A
   {
      if (xp->data != yp->data) same = false;
      xp = xp->next;
      yp = yp->next;
      if (((xp == NULL) & (yp != NULL)) // point B
           | ((xp != NULL) & (yp == NULL)))
           same = false;
         };
   return same;
};

I am very confused as to why the loop control contains (same == true) ?

Also what is the purpose of the if statement at Point B? I'm unsure of what the Boolean expression is checking for?

Any help for further understanding would be appreciated!

Was it helpful?

Solution

It checks that two linked lists contain the same values.

Obviously, if one list is shorter, they are not identical (point B).

Note: I think using here break/return would be a better choice, it makes the code more readable.

Note2: as noted on the comments, those should be logical operators. It works as it is, but it's a bit confusing.

Note3: You could move the test before the loop inside the loop (while(1)), this would eliminate the need for the test at the end of the loop.

This is just an ugly code, it should be about 5 lines of code, not a dozen...

bool sameValues (chainCell *x, chainCell *y)
{
   while(1) {
      if (!x && !y) return true;
      if (!x || !y) return false;
      if (x->data != y->data) return false;
      x = x->next;
      y = y->next;
   }
   return false; //this is just to suppress compiler warning.
};

OTHER TIPS

The function bool sameValues (chainCell *x, chainCell *y) checks if the two struct chainCell has the same value, that is to say, every field of chainCell type between x and y are equal.

"why the loop control contains (same == true) ?"

The boolean value same is an indicator whether the previously checked field are equal. If same == false, it is concluded that x and y are not the same in value, and the while loop could be break.

what is the purpose of the if statement at Point B?

The if statement set a condition that two fields can be equal, only if the two fields are both null or both not null.

Compact form of @Karoly Horvath's answer:

bool sameValues (chainCell *x, chainCell *y)
{

   for( ; x || y; x = x->next, y = y->next) {
      if (!x || !y) return false;
      if (x->data != y->data) return false;
   }
   return true;
}

As I understand it that code is a loop to check whether two chainCell objects are the same.

(same == true) is included to cause the while loop to exit if a position in the cellChains is found where they don't match.

Point B is checking that if either of the chainCells being compared is null and the other is not null that the comparison returns false.

The exit from the loop would be caused by xp reaching a point where it is null.

I would also agree with Shafiks comment, the operators are bitwise and they should be logical.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top