Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Linked list question
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
Chad  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 17:29
Newsgroups: comp.lang.c, comp.programming
From: Chad <cdal...@gmail.com>
Date: Sat, 7 Nov 2009 09:29:41 -0800 (PST)
Local: Sat 7 Nov 2009 17:29
Subject: Linked list question
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?

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bartc  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 17:40
Newsgroups: comp.lang.c, comp.programming
From: "bartc" <ba...@freeuk.com>
Date: Sat, 07 Nov 2009 17:40:25 GMT
Local: Sat 7 Nov 2009 17:40
Subject: Re: Linked list question

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

news:5605a29f-f1a3-4c08-89a9-ecb1c6421fc2@f20g2000prn.googlegroups.com...

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

--
Bart


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chad  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 17:51
Newsgroups: comp.lang.c, comp.programming
From: Chad <cdal...@gmail.com>
Date: Sat, 7 Nov 2009 09:51:27 -0800 (PST)
Local: Sat 7 Nov 2009 17:51
Subject: Re: Linked list question
On Nov 7, 9:40 am, "bartc" <ba...@freeuk.com> wrote:

Aye, that was sloppy naming on my part.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Seebs  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 18:22
Newsgroups: comp.lang.c
From: Seebs <usenet-nos...@seebs.net>
Date: 07 Nov 2009 18:22:56 GMT
Local: Sat 7 Nov 2009 18:22
Subject: Re: Linked list question
On 2009-11-07, Chad <cdal...@gmail.com> wrote:

> What's the difference between using some like struct node* current as
> a temporary variable in the following function...
>    struct node* current = head;
>    while (current != NULL) {
>       current = current->next;
>    }
>    while (head != NULL) {
>       head = head->next;
>    }
> Or wouldn't it matter in this case?

Wouldn't matter in this case.  In general, it doesn't matter what you do
to arguments, as they are passed by value.  (Arrays are sort of an exception,
but not really; what is actually passed is a pointer, because the array
decays into a pointer, and the pointer is passed by value.)

Often people do this when they expect to need to refer to the original
value again later; for instance, iterating over a list with intent to delete
members.  Sometimes someone will have such a function, then copy and paste
it and modify it to serve a new purpose, and the new one no longer needs
the stashed copy.

Harmless; I doubt you'll see many optimizers where the code's different
these days.

-s
--
Copyright 2009, all wrongs reversed.  Peter Seebach / usenet-nos...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Bacarisse  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 00:04
Newsgroups: comp.lang.c, comp.programming
From: Ben Bacarisse <ben.use...@bsb.me.uk>
Date: Sun, 08 Nov 2009 00:04:59 +0000
Local: Sun 8 Nov 2009 00:04
Subject: Re: Linked list question

"bartc" <ba...@freeuk.com> writes:
> "Chad" <cdal...@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.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thad Smith  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 16:24
Newsgroups: comp.lang.c, comp.programming
From: Thad Smith <ThadSm...@acm.org>
Date: Sun, 08 Nov 2009 09:24:09 -0700
Local: Sun 8 Nov 2009 16:24
Subject: Re: Linked list question

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
BGB / cr88192  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 19:46
Newsgroups: comp.lang.c, comp.programming
From: "BGB / cr88192" <cr88...@hotmail.com>
Date: Sun, 8 Nov 2009 12:46:27 -0700
Local: Sun 8 Nov 2009 19:46
Subject: Re: Linked list question

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

news:5605a29f-f1a3-4c08-89a9-ecb1c6421fc2@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...


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google