Linked List - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Linked List

Description:

Advantages over dynamic array: incremental enlargement is possible. Start from basics: ... Similar to traversal but operation will stop immediately when some ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 24
Provided by: raym168
Category:
Tags: linked | list

less

Transcript and Presenter's Notes

Title: Linked List


1
Linked List
  • COMP103 Lab 9

2
Linked List
  • You have learnt many examples (single, circular
    and doubly) in lectures!
  • They share some common characteristics
  • Advantages over dynamic array incremental
    enlargement is possible
  • Start from basics
  • A linear sequence of nodes connected together
  • Concept of head and tail

Head
Tail
3
Basic Operations on Linked List
Operations with NO modification to the linked list
  • Traversal
  • Walk along the list from head to tail, e.g.
    printing
  • Searching
  • Similar to traversal but operation will stop
    immediately when some criteria are met
  • Different behaviors on unsorted and sorted linked
    lists
  • Unsorted linked list Stop when
  • target is found or
  • the end of the list is reached
  • Sorted linked list Stop when
  • target is found or
  • the end of the list is reached or
  • the remaining nodes must not contain such target,
    usually larger or smaller than the target

4
Basic Operations on Linked List
Operations with modification to the linked list
  • Insertion
  • Add one more node to the linked list
  • Deletion
  • Remove a node from the linked list

5
Cases to Handle in Linked List
  • With different implementation of linked list
    (single, circular, doubly, with/without dummy
    head, etc), exact cases to be handled varies
  • But there is a general rule Another case exists
    because the behavior is DIFFERENT

6
Cases to Handle in Linked List
  • Generally speaking
  • Traversal, searching
  • Empty list, non-empty list
  • Insertion
  • Empty -gt non-empty
  • Insert as new head, new tail or in-between node
  • Deletion
  • Non-empty -gt empty
  • Remove head, tail or in-between node
  • Removal item may not exist in the linked list

7
Single Linked List (SLL)
Head
Tail
Head
  • Traversal and searching
  • Start from head node, i.e. Head pointer
  • End when reaching the tail node, i.e. NULL
    pointer
  • Unless other criteria are met in searching
  • Empty and non-empty case are the same

8
Traversal and Searching in SLL
Head
Tail
prev
cur
Head
// search for proper insertion/deletion //
location in SLL NodePtr prev NULL, cur
Head while (cur!NULL item gt
cur-gtdata) prev cur cur cur-gtnext
9
Insertion in SLL
Handled all the cases?
What if the list is originally empty?
In-between
New tail
new
Head
cur
prev
New head
10
Deletion in SLL
What if the target item does not exist? Or there
are only one element originally?
Handled all the cases?
X
prev
Head
cur
In-between
X
Head
prev
cur
Tail
X
Head
cur
prev
Head
11
Summary on SLL
  • Traversal and searching
  • Empty and non-empty cases are the same
  • Insertion
  • In-between node and tail node cases are the same
  • Head node and empty list cases are the same
  • Deletion
  • In-between node and tail node cases are the same
  • Head node and single-element cases are the same
  • Special handling on non-existing target item

12
Circular Linked List (CLL)
  • Traversal and searching
  • Start from head node, i.e. Rear-gtnext
  • End when reaching the tail node, i.e. Rear
  • Empty and non-empty cases are DIFFERENT

Head
Tail
Rear
13
Traversal and Searching in CLL
Head
Tail
prev
Rear
cur
// search for proper insertion/deletion //
location in CLL if (Rear ! NULL) NodePtr prev
Rear, cur Rear-gtnext do if(item lt
Cur-gtdata) break Prev Cur Cur
Cur-gtnext while(Cur ! Rear-gtnext)
14
Insertion in CLL
Handled all the cases?
What if the list is originally empty?
new
Rear
prev
cur
In-between
new
Rear
prev
New tail
cur
Rear
cur
new
prev
New head
15
Deletion in CLL
What if the target item does not exist? Or there
are only one element originally?
Handled all the cases?
X
Rear
prev
cur
In-between
X
Rear
prev
cur
Tail
X
Rear
cur
prev
Head
16
Summary on CLL
  • Traversal and searching
  • Empty and non-empty cases are DIFFERENT
  • Insertion
  • In-between node and head node cases are the same
  • Special handling on tail node case
  • Special handling on empty list case
  • Deletion
  • In-between node and head node cases are the same
  • Special handling on tail node case
  • Special handling on single-element case
  • Special handling on non-existing target item

17
Doubly Linked List (DLL) with Dummy Head
  • Traversal and searching
  • Start from head node, i.e. Head-gtnext
  • End when reaching the tail node, i.e. Head-gtprev
  • Empty and non-empty cases are the same

Dummy
Head
Tail
Head
18
Doubly Linked List (DLL) with Dummy Head
Dummy
Head
Tail
Head
cur
// search for proper insertion/deletion //
location in DLL NodePtr Cur Head-gtnext while(Cu
r ! Head) if(Cur-gtdata item) return
Cur if(Cur-gtdata lt item) Cur
Cur-gtnext else break
19
Insertion in DLL
Handled all the cases?
What if the list is originally empty?
Dummy
new
In-between
Head
cur
Dummy
new
Head
cur
New tail
Dummy
new
Head
cur
New head
20
Deletion in DLL
What if the target item does not exist? Or there
are only one element originally?
Handled all the cases?
Dummy
X
In-between
Head
cur
Dummy
X
Head
cur
Tail
Dummy
X
Head
cur
Head
21
Summary on DLL
  • Traversal and searching
  • Empty and non-empty cases are the same
  • Insertion
  • In-between node, head node, tail node and empty
    list case are the same
  • Deletion
  • In-between node, head node, tail node and
    single-element cases are the same
  • Special handling on non-existing target item

22
Notes on Linked List
  • To avoid errors in modifying pointers, try
  • Always start with the pointers of newly added
    node first, i.e. newNode-gtnext and newNode-gtprev
  • Then modify pointers in previous and next node
  • Last but not lest, a diagram
  • Beware of NULL pointer
  • Always check if the variable is NULL or not
    before using -gt and . operators

23
Notes on Linked List
  • CLL will have NULL pointers ONLY when it is empty
Write a Comment
User Comments (0)
About PowerShow.com