How To

How to Create a Linked List in C

Contributor
By eHow Contributing Writer
(43 Ratings)

In C, a linked list allows you to create a list without deciding ahead of time how long it might be, and without wasting memory by allocating elements you don't have yet. The downside is that you have to do all the work of organizing and managing the list in memory.

Difficulty: Easy
Instructions

    Create the Data Structure

  1. Step 1

    Choose a name, then use typedef to define it. Every linked list will need a structure, even if it only has one variable:

    typedef struct product_data PRODUCT_DATA;

  2. Step 2

    Define the structure. The last element should be a pointer to the type you just defined, and named "next":

    struct product_data {
    int product_code;
    int product_size;
    PRODUCT_DATA *next;
    };

  3. Step 3

    Allocate two pointers to this data structure, initializing them to NULL, to be the list "head" and "tail":

    PRODUCT_DATA *products_head = NULL;
    PRODUCT_DATA *products_tail = NULL;

  4. Add to the List

  5. Step 1

    Allocate a temporary variable that's a pointer to the data structure:

    PRODUCT_DATA *newproduct;

  6. Step 2

    Use malloc() to create a new element, always checking for an error:

    if ((newproduct = malloc(sizeof(PRODUCT_DATA))) == NULL) { abort(); }

  7. Step 3

    Populate the new element's fields. Set its "next" field to NULL:

    newproduct->product_code = newcode;
    newproduct->product_size = newsize;
    newproduct->next = NULL;

  8. Step 4

    Set the head variable. If the head variable is NULL, this is the first element added to the list, so set the head variable to point to it:

    if (!products_head) products_head = newproduct;

  9. Step 5

    Prepare for a different variable. In other cases, the tail variable points to the last item on the list, so set its next value to point to the new item:

    else products_tail->next = newproduct;

  10. Step 6

    Update the tail to point to the new last element, in either case:

    products_tail = newproduct;

  11. Access the List

  12. Step 1

    Create another temporary variable pointing to the data structure:

    PRODUCT_DATA *product;

  13. Step 2

    Set your temporary variable to the head variable:

    product = products_head;

  14. Step 3

    Loop through the elements, checking each one and then setting the temporary variable to the next pointer to traverse to the next one:

    while (product) { if (product->product_code != 15) { product = product->next; } }

  15. Step 4

    Check if the variable is NULL. If so, you never found the item:

    if (!product) return 0;
    . Otherwise, it points to the item you were looking for:
    return product->product_size;

  16. Clean Up Your Work

  17. Step 1

    Deallocate the list when your program ends, as not all operating systems will handle this automatically.

  18. Step 2

    Loop as long as the head variable is not NULL:

    while (products_head) {

  19. Step 3

    Store its next pointer in the tail variable temporarily:

     products_tail = products_head->next;

  20. Step 4

    Deallocate the element:

     free(products_head);

  21. Step 5

    Set the head pointer to the pointer you saved in step 4:

     products_head = products_tail;
    }

Tips & Warnings
  • It's a good idea to create a C function to add to the linked list, then always use it instead of doing this directly.
  • "Doubly linked lists" make deletes and some searches more efficient by using a "previous" pointer along with the "next" pointer, but they make adds less efficient.
  • Deleting from the list is similar to adding to it, but you'll need to find its predecessor and change its next pointer to the deleted element's next pointer, bypassing it, before deallocating it. Deleting the first or last elements will require updating the head or tail variables instead.
  • It's possible to sort a C linked list, and it can be very efficient to do so, since it's only pointers, not actual data, that you must move and copy. However, it's a complicated algorithm.

Comments  

zainka said

Flag This Comment

on 5/4/2009 I believe there is an error in Step3 under "Access the list". The if should have an else with the break; statement. Else you will never leave the loop...Else the tut. is simple to understand1

eskiya said

Flag This Comment

on 1/7/2009 thanks for this fluently explation

satnaay said

Flag This Comment

on 11/20/2008 NICE ARTICLE..

Post a Comment

Post a Comment

Have you done this? Click here to let us know.

I Did This

Related Ads

Internet
Virginia DeBolt,

Meet Virginia DeBolt eHow’s Internet Expert.

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US

Demand Media
eHow_eHow Technology and Electronics