LinkedList Pointer (REPOST - diff version)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • r.devaraj@gmail.com

    #1

    LinkedList Pointer (REPOST - diff version)



    Some typo and confusion in previous post
    (http://groups.google.com/group/comp....3fdc9a2d49e9ac)
    made it difficult for me to fetch correct answers.
    Here's the original problem.

    Consider Linked List

    struct node{
    int data;
    struct node * next;
    }list;


    No problem until you are dealing with a pointer variable(list).
    there, you can access[color=blue][color=green]
    >> memory where it is pointing to.
    >> own address of pointer variable.[/color][/color]

    but think in case &(list->next)[color=blue][color=green]
    >>you can just access the target.
    >> no clue about (atlest for me, now) what pointer comes to picture[/color][/color]
    or where it is stored.,


    Description:

    Consider foll. routines.

    a function to add a node in the list

    void Push(struct node** headRef, int newData);
    Given an int and a reference to the head pointer (i.e. a struct
    node** pointer to the head pointer), add a new node at the head of the
    list with the standard 3-step-link-in: create the new node, set its
    ..next to point to the current head, and finally change the head to
    point to the new node.


    and a routine using it is,


    void BasicsCaller() {
    struct node* head;
    int len;
    head = BuildOneTwoThre e(); // Start with {1, 2, 3}
    Push(&head, 13); // Push 13 on the front,
    // yielding {13, 1, 2, 3}
    // (The '&' is because head is
    //passed
    // as a reference pointer.)
    Push(&(head->next), 42); // Push 42 into the second
    //position
    // yielding {13, 42, 1, 2, 3}
    // Demonstrates a use of '&' on
    // the .next field of a node.
    // (See technique #2 below.)
    }



    First call for push(&head,13) is obvious and works fine.


    Now this is the single question:
    Just explain what happens in the call Push(&(head->next), 42)
    what is passed to Push and what it is done in push.


    if you couldn't get me completely,


    Thanks.
    Deva.

  • manoj1978@gmail.com

    #2
    Re: LinkedList Pointer (REPOST - diff version)


    r.devaraj@gmail .com wrote:[color=blue]
    > Some typo and confusion in previous post
    > (http://groups.google.com/group/comp....3fdc9a2d49e9ac)
    > made it difficult for me to fetch correct answers.
    > Here's the original problem.
    >
    > Consider Linked List
    >
    > struct node{
    > int data;
    > struct node * next;
    > }list;
    >
    >
    > No problem until you are dealing with a pointer variable(list).
    > there, you can access[color=green][color=darkred]
    > >> memory where it is pointing to.
    > >> own address of pointer variable.[/color][/color]
    >
    > but think in case &(list->next)[color=green][color=darkred]
    > >>you can just access the target.
    > >> no clue about (atlest for me, now) what pointer comes to picture[/color][/color]
    > or where it is stored.,[/color]

    It contains the address of list-> next.

    that is address of the field next,inside the structure pointed by list.
    [color=blue]
    >
    >
    > Description:
    >
    > Consider foll. routines.
    >
    > a function to add a node in the list
    >
    > void Push(struct node** headRef, int newData);
    > Given an int and a reference to the head pointer (i.e. a struct
    > node** pointer to the head pointer), add a new node at the head of the
    > list with the standard 3-step-link-in: create the new node, set its
    > .next to point to the current head, and finally change the head to
    > point to the new node.
    >
    >
    > and a routine using it is,
    >
    >
    > void BasicsCaller() {
    > struct node* head;
    > int len;
    > head = BuildOneTwoThre e(); // Start with {1, 2, 3}
    > Push(&head, 13); // Push 13 on the front,
    > // yielding {13, 1, 2, 3}
    > // (The '&' is because head is
    > //passed
    > // as a reference pointer.)
    > Push(&(head->next), 42); // Push 42 into the second
    > //position
    > // yielding {13, 42, 1, 2, 3}
    > // Demonstrates a use of '&' on
    > // the .next field of a node.
    > // (See technique #2 below.)
    > }
    >
    >
    >
    > First call for push(&head,13) is obvious and works fine.
    >
    >
    > Now this is the single question:
    > Just explain what happens in the call Push(&(head->next), 42)
    > what is passed to Push and what it is done in push.[/color]

    It will pass the address of next in the structure pointed by head.
    Push will allocate a node,put 42 inside that.and store address of that
    node in this address.

    So after returning from push,head->next will contain address of newly
    allocated node.
    [color=blue]
    >
    >
    > if you couldn't get me completely,
    > http://cslibrary.stanford.edu/105/Li...stProblems.pdf
    >
    > Thanks.
    > Deva.[/color]

    Comment

    Working...