Sie sind auf Seite 1von 3

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 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

Node one element of the linked list


Object data stored in the node examples? next a reference to the next node in the list
last node points to NULL

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

Point new_node next to the node head points to

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

Does this algorithm work for the list below?


new_node head Object3 next tail

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

Insert here head

new_node

find(3)
new_node head Object3 next tail

find(16) - always remember to deal with special cases

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?

Insert between Object1 and Object2 Delete Object1

Deletion of tail
Complexity?

Doubly Linked Lists


Object1 header prev next Object2 prev next Object3 prev next trailer header

Doubly Linked Lists


Object1 prev next Object2 prev next Object3 prev next trailer

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?

Insertion and deletion at beginning/end Insertion and deletion in middle


header trailer

At the beginning and end of the list are sentinel nodes


Simplify insertion/deletion algorithm

new_node Object4 prev next header

new_node Object4 prev next Object2 prev next Object3 prev next trailer header

Doubly Linked Lists


insert here Object1 prev next

Doubly Linked Lists


insert here Object1 prev next Object2 prev next Object3 prev next trailer

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

Number 1 must come before number 4 Insertion at trailer? Deletion?

Das könnte Ihnen auch gefallen