Thread: Linked list

  1. #1
    bishopcl
    Guest

    Linked list

    After reading a linked list - what is an easy way to extract a particular part of the record, modify it, then save it back to the list? For example: I have a structure that looks like this:

    struct playerinfo {
    char name[15];
    int current_rate;
    int id_number;
    struct playerinfo *next;
    };

    struct playerinfo *first,*current,*new;

    Now, I would input the names of members, with their respective rates and identification numbers.
    Then, I want to take the current_rate from a record, manipulate it, and return it. Is this an easy thing to do?

    Ex: (THIS IS AFTER INPUT)

    ID: 1
    NAME: BISHOP
    RATE: 1571

    ID:2
    NAME: JESTER
    RATE: 1728

    ID: 3
    NAME: VIPER
    RATE: 1400

    Now - pull the RATE from BISHOP (1571) and the rate from JESTER (1728) and throw those into a function that will return different rates for each person.

    Am I making this too hard for myself?

    Thanks!

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    The most easy way is to use a pointer to the list. Using this pointer you can traverse to the list and adapt records in the list. If your pointer current is used to traverse the list, then you could use this pointer to get access to the records you want to adapt.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    1
    That is exactly what I would like to do, but I'm not sure how to code it. If it's not too complexed, can you give me a small example?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct node *ptr;
    for( ptr = first_node; ptr != NULL; ptr = ptr->next )
        if( ptr->data == something )
        {
            do whatever
        }
    Presto!

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Hope this also explains a bit about traversing a linked list.

    Code:
    /* A pointer to the list */
    struct playerinfo *list_ptr;
    
    /* Let the pointer point to the start of the list */
    list_ptr = first;
    
    /* Traverse the list until you found a record you want to adapt */
    bool found = false;
    while (found == false)
    {
        if (list_ptr->id_number == id_to_be_found)
        {
           /* Set found to true to quit loop. Don't change list pointer, it
              points to the node you searched for. */
            found = true;
        }
        else
        {
            /* Let list pointer point to the next node */
            list_ptr = list_ptr->next;
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM