Linked list question

Discussion in 'C Programming' started by Chad, Nov 7, 2009.

  1. Chad

    Chad Guest

    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?
    Chad, Nov 7, 2009
    #1
    1. Advertising

  2. Chad

    bartc Guest

    "Chad" <> wrote in message
    news:...
    > 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?


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

    --
    Bart
    bartc, Nov 7, 2009
    #2
    1. Advertising

  3. Chad

    Chad Guest

    On Nov 7, 9:40 am, "bartc" <> wrote:
    > "Chad" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > 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?

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



    Aye, that was sloppy naming on my part.
    Chad, Nov 7, 2009
    #3
  4. Chad

    Seebs Guest

    On 2009-11-07, Chad <> 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 /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Nov 7, 2009
    #4
  5. "bartc" <> writes:

    > "Chad" <> 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.
    Ben Bacarisse, Nov 8, 2009
    #5
  6. Chad

    Thad Smith Guest

    Chad wrote:
    > On Nov 7, 9:40 am, "bartc" <> wrote:
    >> "Chad" <> wrote in message
    >>
    >> news:...
    >>> 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?

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

    > Aye, that was sloppy naming on my part.


    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
    Thad Smith, Nov 8, 2009
    #6
  7. "Chad" <> wrote in message
    news:...
    > 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?



    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...
    BGB / cr88192, Nov 8, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    479
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    467
    emerth
    Jul 10, 2003
  3. fool
    Replies:
    14
    Views:
    506
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    668
    John Carson
    Oct 2, 2006
  5. jawdoc
    Replies:
    9
    Views:
    752
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page