Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Linked list question

0 views
Skip to first unread message

Chad

unread,
Nov 7, 2009, 12:29:41 PM11/7/09
to
What's the difference between using some like struct node* current as
a temporary variable in the following function...

int Length(struct node* head) {
struct node* current = head;
int count = 0;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}

As opposed to not using one in the same function....

int Length(struct node* head) {
int count = 0;
while (head != NULL) {
count++;
head = head->next;
}
return count;
}


Or wouldn't it matter in this case?

bartc

unread,
Nov 7, 2009, 12:40:25 PM11/7/09
to

"Chad" <cda...@gmail.com> wrote in message
news:5605a29f-f1a3-4c08...@f20g2000prn.googlegroups.com...

Probably doesn't matter, but in the second case, 'head' is a misnomer for
the variable.

--
Bart

Chad

unread,
Nov 7, 2009, 12:51:27 PM11/7/09
to
On Nov 7, 9:40 am, "bartc" <ba...@freeuk.com> wrote:
> "Chad" <cdal...@gmail.com> wrote in message


Aye, that was sloppy naming on my part.

Ben Bacarisse

unread,
Nov 7, 2009, 7:04:59 PM11/7/09
to
"bartc" <ba...@freeuk.com> writes:

> "Chad" <cda...@gmail.com> wrote in message

<snip other version>


>> int Length(struct node* head) {
>> int count = 0;
>> while (head != NULL) {
>> count++;
>> head = head->next;
>> }
>> return count;
>> }

<snip>


> Probably doesn't matter, but in the second case, 'head' is a misnomer
> for the variable.

Is it? At every stage of the loop, count is the number of list items
seen and head points to the start of a list whose length is yet to be
determined. I don't mind the name at all.

--
Ben.

Thad Smith

unread,
Nov 8, 2009, 11:24:09 AM11/8/09
to

No, head is a reasonable name for the parameter designating the head of
a list and having the parameter properly described for the caller is
high on my list. When you reuse it, though, it takes on a different
meaning, although I understand Ben's argument that it is the head of the
remaining list instead of the provided list. I consider the first
version slightly better for documentation purposes.

--
Thad

BGB / cr88192

unread,
Nov 8, 2009, 2:46:27 PM11/8/09
to

"Chad" <cda...@gmail.com> wrote in message
news:5605a29f-f1a3-4c08...@f20g2000prn.googlegroups.com...


it is mostly a stylistic difference, where using a local variable is a
little "cleaner" than using the argument (and is not as "destructive" to the
argument in question).


also, of note, is that when using languages which support references (may be
called "pass by reference" in many languages), then the semantics will
differ as well, since modifying the argument "may" also modify the value in
the variable held by the caller.

so, this issue could be an issue in languages such as Perl, VB, or AFAIK
some Pascal variants.
(apparently Perl and VB do so by default...).

it could also matter in C++, although the syntax is more explicit in this
case:
int Length(struct node* &head);
or, in Pascal and friends:
function Length(var head: ^node): integer;

(where the 'var' could be easily missed...)

so, using a local makes ones' intentions a little clearer, even though,
technically, in C it is unecessary...

0 new messages