Thread: a link list code question..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    a link list code question..

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    struct list_el {                         //type list_el
       int val;
       struct list_el * next;
    };
    
    typedef struct list_el item;         //item is alias for list_el
    
    void main() {
       item * curr, * head;             
       int i;
    
       head = NULL;                   //head is an item with two variables 
                                               //does it put null on every sub variable of head??
       for(i=1;i<=10;i++) {
          curr = (item *)malloc(sizeof(item)); 
          curr->val = i;
          curr->next  = head;          //*(curr).next is a value, head is an address of a pointer
                                                  //putting one into another makes no sense??
          head = curr;
       }
    
       curr = head;
    
       while(curr) {
          printf("%d\n", curr->val);
          curr = curr->next ;
       }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. head is a pointer, not an item with two variables.
    2. curr->next is a pointer (it's the subfield of the struct); head is also a pointer (not the address of a pointer).

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    struct list_el {                         //type struct list_el
    typedef struct list_el item;         //item is alias for struct list_el
    void main() { http://faq.cprogramming.com/cgi-bin/...&id=1043284376
          curr = (item *)malloc(sizeof(item)); http://faq.cprogramming.com/cgi-bin/...&id=1043284351

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    head is a pointer for a complex type variable
    so there should be two addresses one for each variable

    *head is the value
    head stores the address of the this it points too

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by transgalactic2 View Post
    head is a pointer for a complex type variable
    so there should be two addresses one for each variable
    This is of course incorrect.

    *head is the value
    head stores the address of the this it points too
    That is correct. head stores an address. It does not store an address of an address.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    head is a pointer for a complex type variable
    so there should be two addresses one for each variable
    No there aren't two addresses. head points to an object of type struct el_list and is treated as a unit by itself even though it has member elements.
    *head is the value
    head stores the address of the this it points too
    head is the pointer to an object of type struct el_list and *head is the object itself not the value.
    Code:
    curr->next  = head;
    should actually be
    Code:
    curr->next  = NULL; /* next element should always be null no matter how many objects of type struct el_lst are linked together */
    Last edited by itCbitC; 10-26-2008 at 04:23 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by itCbitC View Post
    Code:
    curr->next  = head;
    should actually be
    Code:
    curr->next  = NULL; /* next element should always be null no matter how many objects of type struct el_lst are linked together */
    Never seen people add elements to the front of a linked list before?

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Sure won't mark the head but the end of the linked list.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by itCbitC View Post
    Sure won't mark the head but the end of the linked list.
    That's why head is changed on the next line, you see. You make this new node point at what used to be the head, and then you reset the head to be the new node.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    itCbitC, you need to understand, the only things wrong in the code have been pointed out by robwhit.
    The question isn't whether the code works or not; it's basically fine, and simply prepends onto the head of the list not the tail.

    transgalactic2, you're asking how existing code works. Before answering I'd rather know where the code comes from.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i have seen but the C way is not easy
    i'll try to read some more

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by tabstop View Post
    That's why head is changed on the next line, you see. You make this new node point at what used to be the head, and then you reset the head to be the new node.
    gotcha a bit of an oversight on my part.

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant understand this line in the code

    Code:
    struct node {
      int x;
      node *next;
    };
    
    int main()
    {
      node *root;      // This will be the unchanging first node
    
      root = new node; // Now root points to a node struct
      root->next = 0;  // The node root points to has its next pointer
                       //  set equal to a null pointer
      root->x = 5;     // By using the -> operator, you can modify the node
                       //  a pointer (root in this case) points to.
    }
    first they create a node type pointer called root

    after that i thought they will put some address inside "root"
    but instead they are doing
    Code:
    root = new node;
    this makes no sense
    i am familiar with this command
    it for creating a new object
    but we need to write it like this
    Code:
    node root = new node();
    and it doest put any address to root

    ??

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The C++ operator "new" creates a block of memory (and when relevant, calls the constructor for the object that has been created). It returns an address.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The C++ operator "new" creates a block of memory (and when relevant, calls the constructor for the object that has been created). It returns an address.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Tutorial sample code question
    By Kalleos in forum C Programming
    Replies: 2
    Last Post: 01-16-2009, 12:20 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM