change the head of linked list question..

This is a discussion on change the head of linked list question.. within the C Programming forums, part of the General Programming Boards category; i cant see how it puts a new head Code: typedef struct node { int value; struct node *next; }Node; ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    p is pointer to the head pointer, which gives the possibility to update the head pointer value (in the line you have removed)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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 ...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by transgalactic2 View Post
    why i get this warning
    because you know better
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    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;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    ??

Page 1 of 2 12 LastLast
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, 05: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, 03:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21