Вы находитесь на странице: 1из 15

Linked Lists

Example
We would like to keep a list of inventory
records but only as many as we need

An array is a fixed size

Instead use a linked list


Linked List

name = shoes name = shirts name = pants


number = 1234 number = 1235 number = 1236
cost = 20.00 cost = 20.00 cost = 35.00
num_in_inventory = 10 num_in_inventory = 5 num_in_inventory = 12
Linked List

name = shoes name = shirts name = pants


number = 1234 number = 1235 number = 1236
cost = 20.00 cost = 20.00 cost = 35.00
num_in_inventory = 10 num_in_inventory = 5 num_in_inventory = 12
Linked List

name = shoes name = shirts name = pants


number = 1234 number = 1235 number = 1236
cost = 20.00 cost = 20.00 cost = 35.00
num_in_inventory = 10 num_in_inventory = 5 num_in_inventory = 12
next_ptr = next_ptr = next_ptr = NULL
Defining the Structure
typdef struct inventory_item_s {
char name[80];
int number;
double cost;
int num_in_inventory;

} inventory_item_t;
Defining the Structure
typdef struct inventory_item_s {
char name[80];
int number;
double cost;
int num_in_inventory;
struct inventory_item_s *next;
} inventory_item_t;
Typical Linked List
head: Item 1 Item 2 Item 3 Item N
NULL
Traversing a List
head: Item 1 Item 2 Item 3 Item N
NULL

//assume head points to the first element of the list


//traverse the list and print the name of each item (in the inventory)
inventory_item_t *cur_node;
cur_node = head;
while(cur_node != NULL) {
printf(Name: %s\n, cur_node->name);
cur_node=cur_node->next;
}
Building a List
head: Item N
NULL

inventory_item_t *head = NULL;


inventory_item_t *tmp = NULL;
tmp = (inventory_item_t *)malloc(sizeof (inventory_item_t));
strcpy(tmp->name, Shirts);
tmp->cost = 20;
tmp->number=1234;
tmp->num_in_inventory=10;
tmp->next=NULL;
head = tmp;
Insert an Element
head: Item N
NULL

//we have head and tmp


tmp = (inventory_item_t *)malloc(sizeof (inventory_item_t));
strcpy(tmp->name, Pants);
tmp->cost = 70;
tmp->number=1235;
tmp->num_in_inventory=17;
//insert at beginning
Insert an Element
head: Item N-1 Item N
NULL

//we have head and tmp


tmp = (inventory_item_t *)malloc(sizeof (inventory_item_t));
strcpy(tmp->name, Pants);
tmp->cost = 70;
tmp->number=1235;
tmp->num_in_inventory=17;
//insert at beginning
// point new element next to current head
// point current head to new element
tmp->next=head;
head=tmp;
Insert an Element
head: Item N-1 Item N
NULL

tmp = (inventory_item_t *)malloc(sizeof (inventory_item_t));


strcpy(tmp->name, Shoes);
tmp->cost = 40;
tmp->number=1236;
tmp->num_in_inventory=7;
//insert at end
Insert an Element
head: Item N-1 Item N
NULL

tmp = (inventory_item_t *)malloc(sizeof (inventory_item_t));


strcpy(tmp->name, Shoes);
tmp->cost = 40;
tmp->number=1236;
tmp->num_in_inventory=7;
//insert at end
// point new element next to NULL
// point last element next to new element
Insert an Element
head: Item N-1 Item N Item N+1
NULL

tmp = (inventory_item_t *)malloc(sizeof (inventory_item_t));


strcpy(tmp->name, Shoes);
tmp->cost = 40;
tmp->number=1236;
tmp->num_in_inventory=7;
//insert at end
// point new element next to NULL
// point last element next to new element
tmp->next=NULL;
//assume cur_node pointer was declared
cur_node=head;
while(cur_node != NULL && cur_node->next != NULL) {
cur_node=cur_node->next;
}
cur_node->next=tmp;

Вам также может понравиться