Thread: List of nodes

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    37

    List of nodes

    I have a struct as follows

    Code:
    struct node{
      int x;
      int y;
      node *next_nodex;
    }
    
    node r_table[360];
    These are delcared globally. I know this is bad but it does initilise everything if declared globally.

    I have a function which decides which element in the array.

    How would I get to the last element in one of the lists. Is it something like this.

    Code:
    node *tmp_ptr;
    
            tmp_ptr = &(r_table[table_indx]);
    
            // Goto to end of list
            while (tmp_ptr->next_node != NULL)
              tmp_ptr = tmp_ptr->next_node;
    
            // Add new node
            tmp_ptr->next_node = new(node);
    
            // Step into new node
            tmp_ptr = tmp_ptr->next_node;
    
            tmp_ptr->x = x_anchor - col;
            tmp_ptr->y = y_anchor - row;
            tmp_ptr->next_node = NULL;
    Thanks

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Looks good - but here is an optimization:
    Code:
    typedef struct _node
    {
        int x;
        int y;
        _node *next_nodex;
    
        _node() : next_nodex(NULL) {}
    } node_t;
    
    typedef struct _node_array_elem
    {
        node_t *first,*last;
    
        _node_array_elem() : first(NULL),last(NULL) {}
    } node_array_elem_t;
    
    node_array_elem_t r_table[360];
    
    ...
    
        node_array_elem_t *tmp_ptr;
        tmp_ptr = r_table + table_indx;
    
        node_t *new_node = new(node_t);
    
        // Add new node
        if (tmp_ptr->last != NULL)
        {
            tmp_ptr->last->next_nodex = new_node;
            tmp_ptr->last = new_node;
        }
        else
        {
            tmp_ptr->last = new_node;
            tmp_ptr->first = new_node;
        }
    
        new_node->x = x_anchor - col;
        new_node->y = y_anchor - row;
    If you are using "new", then you must be using C++, so you can also use constructors to perform initialization (which also means the typedef's are really necessary, but I like em). And then the optimization part: why traverse to the end of the list every time if you can just remember where the end is?

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  4. freeing list that gets nodes from block
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-01-2002, 10:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM