Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

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. 1Choose 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. 2Define 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. 3Allocate 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;

Add to the List

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

PRODUCT_DATA *newproduct;

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

if ((newproduct = malloc(sizeof(PRODUCT_DATA))) == NULL)


{ abort(); }

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

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

7. 4Set 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;

8. 5Prepare 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;

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

products_tail = newproduct;

Access the List

10. 1Create another temporary variable pointing to the data structure:

PRODUCT_DATA *product;

11. 2Set your temporary variable to the head variable:

product = products_head;

12. 3Loop 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; } }

13. 4Check 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;

Clean Up Your Work

14. 1Deallocate the list when your program ends, as not all operating systems will
handle this automatically.
15. 2Loop as long as the head variable is not NULL:

while (products_head) {

16. 3Store its next pointer in the tail variable temporarily:

products_tail = products_head->next;

17. 4Deallocate the element:

free(products_head);

18. 5Set the head pointer to the pointer you saved in step 4:

products_head = products_tail;
}

Read more: How to Create a Linked List in C | eHow.com


http://www.ehow.com/how_2056292_create-linked-list-c.html#ixzz15ddMtfuv

You might also like