Linked Lists - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Linked Lists

Description:

Sorted or unsorted, would the performance be better if the list were doubly-linked? ... algorithm change if we use a doubly-linked list? CMSC 202, Version 4/02 ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 19
Provided by: dennis139
Category:
Tags: doubly | linked | lists

less

Transcript and Presenter's Notes

Title: Linked Lists


1
Linked Lists
2
Types of Linked Lists
  • Singly-linked list
  • Doubly-linked list
  • Singly, circularly-linked list
  • Doubly, circularly-linked list

3
Singly-linked List
data
head pointer
head node
tail
Some refer to only the last node as the tail
4
Doubly-linked List
data
head pointer
5
Singly, Circularly-linked List
data
head pointer
6
Doubly, Circularly-linked List
data
head pointer
7
Singly-linked List in OO
other private data members
data object
list object
nodeobject
May include maximum number of nodes, current
number of nodes, or other items.
8
A Look at Common Operations on a Singly-linked
List
  • Locating an item
  • Inserting an item
  • Removing an item

9
Locating an Item
Other Private Data members
42
63
99
10
next
next
next
head
10
Locating an Item (cont)
  • Design Decision
  • What do we do if the item is not in the list?
  • Performance
  • What is the best case performance?
  • What is the worst case performance?
  • What is the average performance?
  • How is the performance different in a sorted
    linked list (best, worst, average cases)?
  • Sorted or unsorted, would the performance be
    better if the list were doubly-linked?

11
Inserting an Item
Insert 17 at the head
Other Private Data members
42
63
99
10
next
next
next
head
12
Inserting an Item (cont)
Insert 17 in sorted order
Other Private Data members
42
63
99
10
next
next
next
head
13
Inserting an Item (cont)
In general, with a sorted list, there are three
cases - insert as first node - insert as
last node - insert somewhere in between
Other Private Data members
42
63
99
10
next
next
next
head
14
Inserting an Item (cont)
  • Design Decision
  • What do we do if duplicates are not allowed?
  • Performance
  • If the list is not maintained in sorted order, it
    is most efficient to insert at the head.
  • What is the asymptotic performance of this kind
    of insert?
  • If we choose to insert at the end of the list,
    what is the asymptotic performance?

15
Inserting an Item (cont)
  • If the list is maintained in sorted order, we
    must search to find the proper place in the list,
    then insert.
  • What is the asymptotic performance of this
    insert?
  • Would performance be better if the list were
    doubly-linked?

16
Removing an Item
Other Private Data members
42
63
99
10
17
next
next
next
head
next
Three cases - remove first item - remove
last item - remove an item in between
17
Removing an Item (cont)
How does the algorithm change if we use a
doubly-linked list?
Other Private Data members
42
63
99
10
17
next
next
next
head
next
prev
prev
prev
prev
18
Removing an Item (cont)
  • Design decisions
  • What do we do if the item we are asked to delete
    is not in the list?
  • What do we do if duplicates are allowed in the
    list?
  • Performance
  • Would performance be better if the list were
    doubly-linked?
Write a Comment
User Comments (0)
About PowerShow.com