Beruflich Dokumente
Kultur Dokumente
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 What are the disadvantages of using a linked list?
Linked Lists
Linked List
Object next Object next Object next Object next
Linked List
tail Object next Object next
head
head keeps track of the head of the list tail keeps track of the last node in the list
tail not always used
Insertion at Head
new_node head Object3 next Insert here tail Object1 next Object2 next Object3 next new_node
Insertion at Head
tail head Object1 next Object2 next
Create new_node
store object in new_node
Insertion at Head
new_node head Object3 next tail Object1 next Object2 next
Insertion at Head
tail head Object3 next Object1 next Object2 next
Create new_node
store object in new_node
Point new_node next to the node head points to Point head to new_node
Insertion at Head
Create new_node
store object in new_node
Insertion at Head
new_node head Object3 next tail
Point new_node next to the node head points to Point head to new_node If tail points to NULL
point tail to new_node
Create new_node
store object in new_node
Point new_node next to the node head points to Point head to new_node If tail points to NULL
point tail to new_node
Insertion at Tail
head Object1 next Object3 next Object2 next
tail
Find
tail 5 next 12 next 3 next
new_node
find(3)
new_node head Object3 next tail
Deletion
tail head Object3 next Object1 next Object2 next
Insertion/Deletion in Middle
tail head Object3 next Object1 next Object2 next
Deletion of head
Complexity?
Deletion of tail
Complexity?
Each node keeps a pointer to the next node and to the previous node
Makes some operations (such as insertion at end) more efficient Costs?
new_node Object4 prev next Object2 prev next Object3 prev next trailer header
Insertion
Insertion at head
1. Set next of new_node to point to what headers next points to 2. Set prev of node that headers next points to to point to new_node 3. Set prev of new_node to point to header 4. Set headers next to point to new_node