Thread: Traversing a linked list

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Heraklion, Greece, Greece
    Posts
    26

    Traversing a linked list

    Hello,

    Let's say i have a struct
    Code:
    struct node
    {
        int item;
        struct node *next
    };




    The code for traversing a linked list is this



    Code:
      while(current != NULL)
        {   
             current->item=5;
             current = current->next;
        }
    Here comes my stupid question.Since current is a node,wouldn't
    Code:
    current = current->next;
    replace the node current with the next one and lose all the information we know about the current node?
    Last edited by Konstantinos; 11-10-2013 at 07:32 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > replace the node current with the next one and lose all the information we know about the current node?
    You're not replacing the node, you're just updating a pointer to one node to point to another node.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Heraklion, Greece, Greece
    Posts
    26
    Thanks for your time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-12-2010, 06:00 PM
  2. Traversing linked list help
    By cool_guy in forum C++ Programming
    Replies: 2
    Last Post: 10-23-2010, 10:03 PM
  3. Traversing a linked list
    By JFonseka in forum C Programming
    Replies: 1
    Last Post: 10-25-2007, 04:24 AM
  4. Traversing Link List Problem
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 05-15-2007, 02:13 AM
  5. traversing a linked list with a node and list class
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 11:57 AM