Download presentation
Presentation is loading. Please wait.
1
Linked Lists
2
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
3
Linked List name = shoes number = 1234 cost = 20.00 num_in_inventory = 10 name = shirts number = 1235 cost = 20.00 num_in_inventory = 5 name = pants number = 1236 cost = 35.00 num_in_inventory = 12
4
Linked List name = shoes number = 1234 cost = 20.00 num_in_inventory = 10 name = shirts number = 1235 cost = 20.00 num_in_inventory = 5 name = pants number = 1236 cost = 35.00 num_in_inventory = 12
5
Linked List name = shoes number = 1234 cost = 20.00 num_in_inventory = 10 name = shirts number = 1235 cost = 20.00 num_in_inventory = 5 name = pants number = 1236 cost = 35.00 num_in_inventory = 12 next_ptr = next_ptr = NULL
6
Defining the Structure typdef struct inventory_item_s { char name[80]; int number; double cost; int num_in_inventory; } inventory_item_t;
7
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;
8
Typical Linked List Item 1Item 2Item 3Item N NULL head:
9
Traversing a List //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; } Item 1Item 2Item 3Item N NULL head:
10
Building a List 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; Item N NULL head:
11
Insert an Element //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 Item N NULL head:
12
Insert an Element //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; Item N-1 head: Item N NULL
13
Insert an Element 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 Item N-1 head: Item N NULL
14
Insert an Element 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 Item N-1 head: Item N NULL
15
Insert an Element 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; Item N-1 head: Item NItem N+1 NULL
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.