Thread: linked list

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    linked list

    i have the two linked lists if we apply the following statement to these two lists wtih temp being a temporary pointer, what would happen. im pulling my hair out over this, could anyone please help



    temp = list1;
    while(temp -> link != NULL)
    temp = temp -> link;
    temp -> link = list2;

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: linked list

    Looks about right, Salem. Here's the code in the an acceptable form:
    Code:
    temp = list1;
    while (temp->link != NULL)
    {
    	temp = temp->link;
    }
    temp->link = list2;
    Note how the [code] tag make it readable? And don't forget the closing tag either.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    I'm working on linked lists too, and came across a symbol I just cant understand. Its this: " -> " what does it mean?

    Here is the peice of code its in, that I'm trying to understand.

    Code:
    new = (person*)malloc(sizeof(struct person));
    new->next = head;
    head = new
    And also, can anyone translate that first line? I still don't under stand how the * sign can be after person.
    Thanks for any help.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by daveS
    I'm working on linked lists too, and came across a symbol I just cant understand. Its this: " -> " what does it mean?

    Here is the peice of code its in, that I'm trying to understand.

    Code:
    new = (person*)malloc(sizeof(struct person));
    new->next = head;
    head = new
    And also, can anyone translate that first line? I still don't under stand how the * sign can be after person.
    Thanks for any help.
    The first line allocates memory. malloc returns a void * so they cast it to a person *. The -> is used to dereference a pointer AND access a member. Another way to write it is:

    *new.next = head;

    Note: If you don't have a really good grasp on pointers I would suggest reading up on them before jumping into linked lists.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by daveS
    Code:
    new = (person*)malloc(sizeof(struct person));
    new->next = head;
    Another way of saying what MrWizard said:

    First:
    malloc() returns a pointer (*) but it is of type void. You need it as a pointer to a type 'person' therefore (person *) is used to redefine the pointer to the proper type.

    Second:
    If you have defined:
    person new;
    new is a structure, you reference it's members as new.member

    But you have new as a pointer to a structure person. You therefore need to use new->member to reference thru the pointer to the structure.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    28
    I always thought the . (dot) operator had prescedence over the * (derefernce) operator so wouldnt you need to do itl ike this?

    Code:
    (*person).member
    Dat
    http://hoteden.com <-- not an porn site damnit!

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    So...

    For referencing a member of a structure I would use the standard (.)

    And the (->) if Im referencing via pointer?


    Note: If you don't have a really good grasp on pointers I would suggest reading up on them before jumping into linked lists.
    I understand pointers when I study them, but apparently not when it comes time to actually use them. This tutorial I'm using lacks a little in the area of practice. Alot of the practice problems they give have no answers included, so I never know if I'm doing the right thing. Any free C-programming links would be much appreciated.

    Salem,

    That method seems alot easier to me. And it says that most experienced programmers use it. It seems there are some really experienced programmers here; what do you guys use?

    Thanks alot for the help. This is a great service you are providing!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM