Thread: change the head of linked list question..

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

    change the head of linked list question..

    i cant see how it puts a new head
    Code:
    typedef struct node {
        int value;
        struct node *next;
    }Node;
    
    void what1(Node ** p,int num){
       Node *elt;
       elt=(Node*)malloc(sizeof(Node));
       elt->next=*p;
       elt->value=num;
       *p=elt;
    }
    if p is our head of the list
    then we can just do this
    Code:
    void what1(Node ** p,int num){
      Node *elt;
       elt=(Node*)malloc(sizeof(Node));
       elt->next=p;
    elt->value=num;
    
    }
    i cant understand the first code

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    p is pointer to the head pointer, which gives the possibility to update the head pointer value (in the line you have removed)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    p is the head of the list
    so if we want to put one node before it
    what is the meaning of *p
    if our p is defined as node **p
    ??

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by transgalactic2 View Post
    p is the head of the list
    so if we want to put one node before it
    what is the meaning of *p
    if our p is defined as node **p
    ??
    you sure know better what this code means even when you do not understand what it does...

    I do not see a way to explain you something
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by transgalactic2 View Post
    p is the head of the list
    so if we want to put one node before it
    what is the meaning of *p
    if our p is defined as node **p
    ??
    I believe what Vart is trying to say is that the head of the list, has to be updated, so it can stay as the head of the list.

    You can't / shouldn't, put a node in front of the head of the list, unless you make that node, the new head of the list.

    Otherwise, the head of the list, will not stay the head of the list.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    is this code doing the same job as the first one

    Code:
    void what1(Node ** p,int num){
      Node *elt;
       elt=(Node*)malloc(sizeof(Node));
       elt->next=p;
    elt->value=num;
    
    }

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by transgalactic2 View Post
    is this code doing the same job as the first one
    it does not

    I have already told this.

    You have not believe the first time. I hardly think you will believe it from the second. I do not understand, why you continue to ask questions if you ignore the answers ...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    because its a very simple logic where i say that then next of some node will be the head of the list

    why its wrong??

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    you said
    "p is pointer to the head pointer, which gives the possibility to update the head pointer value "

    but you didnt say why my method is wrong

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by transgalactic2 View Post
    you said
    "p is pointer to the head pointer, which gives the possibility to update the head pointer value "

    but you didnt say why my method is wrong
    I said you have deleted the line that does that
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Use your code on a short linked list with some values. Work the list - add and delete nodes, change the values, etc. Do it all. Good practice, anyway.

    And keep printing up the new list, after you do each of these.

    See for yourself what is the difference between your code, and the school's. Sometimes it's hard to believe that something is hot, until you get close enough to feel the heat. Then you know it's hot, and your mind opens up to accepting the idea that the object is indeed, hot.

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    why i get this warning
    c|18|warning: assignment from incompatible pointer type|
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    typedef struct node {
        int value;
        struct node *next;
    }Node;
    
    void what1(Node ** p,int num);
    int main()
    {
    
        return 0;
    }
    
    void what1(Node ** p,int num){
      Node *elt;
       elt=(Node*)malloc(sizeof(Node));
       elt->next=p;                                            //i get warning here
    elt->value=num;
    
    }

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by transgalactic2 View Post
    why i get this warning
    because you know better
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    why i get this warning
    c|18|warning: assignment from incompatible pointer type|
    What is the type of elt->next and the type of p on this line?
    Code:
    elt->next=p;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i understand my error p is node**
    elt is node*

    another question is:
    you said about this code that
    Code:
    typedef struct node {
        int value;
        struct node *next;
    }Node;
    
    void what1(Node ** p,int num){
       Node *elt;
       elt=(Node*)malloc(sizeof(Node));
       elt->next=*p;
       elt->value=num;
       *p=elt;
    }
    Quote Originally Posted by laserlight View Post
    The key to the linked list is on this line:
    Code:
    elt->next=*p;
    This says that the node pointed to by elt now has a next pointer that points to the node pointed to by *p. In more graphical terms, this tongue twister is like this:

    First, we have two pointers that point to nodes:
    Code:
    *p --> [node #1]
    elt --> [node #2]
    Now, we get elt's next pointer to point to what *p is pointing to:
    Code:
    elt->next --> [node #1]
    This means that we now have two nodes linked together:
    Code:
    [node #2] --> [node #1]

    Now, here's the catch. elt points to node #2, but elt itself is a local variable. When the function ends, elt will be destroyed. What to do?

    We can choose to return elt, but in this case we choose to make *p point to what elt points to. As such, the caller now has a pointer to node #2
    .
    regarding the red marked part:
    before the last step we have elt->next->*p

    what happens to this structure after the red part
    because as i see it when you say
    "we choose to make *p point to what elt points to"
    we get a circular linked list between the old and the new heads
    ??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List, Please Help!
    By CodeMonkeyZ in forum C Programming
    Replies: 5
    Last Post: 02-17-2009, 06:23 AM
  2. question about a working linked list
    By cold_dog in forum C++ Programming
    Replies: 23
    Last Post: 09-13-2006, 01:00 AM
  3. Replies: 11
    Last Post: 01-02-2006, 04:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM