Example We would like to keep a list of inventory records but - - PDF document

example
SMART_READER_LITE
LIVE PREVIEW

Example We would like to keep a list of inventory records but - - PDF document

Example We would like to keep a list of inventory records but only as many as we need Linked Lists An array is a fixed size Instead use a linked list What are the disadvantages of using a linked list? Linked List


slide-1
SLIDE 1

1 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
  • What are the disadvantages of using a

linked list?

Linked List

  • 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

Object next Ø Object next Object next

Linked List

  • head keeps track of the head of the list
  • tail keeps track of the last node in the list

– tail not always used

Object next Ø Object next Object next head tail

Insertion at Head

Object1 next Ø head Object2 next Object3 next Insert here new_node tail

Insertion at Head

  • Create new_node

– store object in new_node

  • Point new_node next to the node head points to

Object1 next Ø head Object2 next Object3 next new_node tail

slide-2
SLIDE 2

2

Insertion at Head

  • Create new_node

– store object in new_node

  • Point new_node next to the node head points to
  • Point head to new_node

Object1 next Ø head Object2 next Object3 next new_node tail

Insertion at Head

  • Does this algorithm work for the list below?

Object1 next Ø head Object2 next Object3 next Ø head Object3 next new_node tail tail

Insertion at Head

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

  • 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

Ø head Object3 next new_node tail

Insertion at Tail

Ø head Object3 next new_node tail Object1 next Ø head Object2 next Object3 next Insert here tail new_node

Find

  • find(3)
  • find(16) - always remember to deal with special

cases

5 next Ø 3 next 12 next head tail

slide-3
SLIDE 3

3

Deletion

  • Deletion of head

– Complexity?

  • Deletion of tail

– Complexity?

Object1 next Ø head Object2 next Object3 next tail

Insertion/Deletion in Middle

  • Insert between Object1 and Object2
  • Delete Object1

Object1 next Ø head Object2 next Object3 next tail

Doubly Linked Lists

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

  • At the beginning and end of the list are sentinel

nodes

– Simplify insertion/deletion algorithm

Object3 prev next trailer Object2 prev next Object1 prev next header

Doubly Linked Lists

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

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

Doubly Linked Lists

  • Insertion

Object3 prev next trailer Object2 prev next Object1 prev next header Object4 prev next insert here new_node

Doubly Linked Lists

  • Insertion at head

1. Set next of new_node to point to what header’s next points to 2. Set prev of node that header’s next points to to point to new_node 3. Set prev of new_node to point to header 4. Set header’s next to point to new_node

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

Object3 prev next trailer Object2 prev next Object1 prev next header Object4 prev next insert here new_node