Thread: Pointing to nodes in a linked list.

  1. #1
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181

    Pointing to nodes in a linked list.

    A simple singly linked list needs nodes with each node having a data and next field. Further more, it needs a "head" pointer to point to the first node. Now if we want to run along this list say to get data from each node or even to count the nodes, we could make a node pointer, lets call it "current".

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node{
    int data;
    struct node* next;
    };
    
    void Push(struct node** headRef, int data) {
    struct node* newNode = malloc(sizeof (struct node));
    newNode->data = data;
    newNode->next = *headRef;
    *headRef = newNode;
    }
    
    int main ()
    {
        int n;
        struct node* head= NULL;
        Push(&head, 3);
        Push(&head, 2);
        Push(&head, 1);
        struct node* current= head;
        current= current->next;
        printf("%d", current->data);
        return 0;
    }
    I assure you all, it compiles and runs well as it should.
    The code basically builds a list by adding to the head of the list the numbers 1, 2 and 3 through the function "Push". Note that head points to the beginning of the list. Now I made a "current" pointer that was initialized to point to whatever head was pointing to. And head, was initially pointing to NULL but after the list got built, it pointed to the beginning node of a 3 node list.

    Now the point of my question.

    When I was initially building this code, my line 23:
    Code:
    struct node* current= head;
    was in line 20 and as a result the programme kept crashing when run.

    So my question is: How does by changing the line at which current pointer points to head, effect this?

    It doesnt make sense to me. No matter where I write the code that stipulates current points to head shouldnt matter. Because head is constantly pointing to the beginning of the list.
    Last edited by Vespasian; 10-05-2011 at 12:16 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    So let's say you wrote this:
    Code:
        struct node* head= NULL;
        struct node* current= head;
        Push(&head, 3);
        Push(&head, 2);
        Push(&head, 1);
    What could be wrong with this picture. Well you change the pointer variable head with the Push function, but you never update current. Since current is a NULL pointer, it is not dereferenceable.

    More generally, say you have 2 pointers which point to the same object. Then you change one by reassignment. The other pointer doesn't change, and isn't supposed to change; it still points to the original object.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list, Head pointing to second node
    By nkbxwb in forum C Programming
    Replies: 3
    Last Post: 10-01-2011, 06:38 PM
  2. Linked List of nodes with operators
    By antonsavelyev in forum C Programming
    Replies: 3
    Last Post: 04-14-2011, 05:30 PM
  3. adding new nodes to linked list
    By roaan in forum C Programming
    Replies: 8
    Last Post: 07-15-2009, 12:55 PM
  4. Adding nodes into linked list
    By Suchy in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2007, 04:03 PM
  5. Linked List and Nodes
    By paperbox005 in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2004, 09:12 AM