Chapter 21 Implementing lists : Linked implementations - PowerPoint PPT Presentation

1 / 59
About This Presentation
Title:

Chapter 21 Implementing lists : Linked implementations

Description:

Dynamic memory allocation Recall memory space for method variables parameters and local variables is allocated when the method is invoked, ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 60
Provided by: JNi96
Category:

less

Transcript and Presenter's Notes

Title: Chapter 21 Implementing lists : Linked implementations


1
Chapter 21 Implementing lists Linked
implementations
2
Linked implementations
  • Structure built from collection of nodes that
    reference each other.
  • A node
  • contains reference to a list element,
  • one or more variables referencing other structure
    nodes.

3
First linked implementation
  • LinkedList Each node references node containing
    next list element.

4
Class Node
  • Class Node, local to LinkedList, models nodes.
  • Node structure
  • must know an element of the List,
  • must know the next Node in sequence.
  • As local class of LinkedList has no methods.

5
Class Node implementation
public class LinkedListltElementgt extends
AbstractListltElementgt private class Node
private Element element private Node
next // Create a Node containing specified
element. public Node (Element element)
this.element element this.next null
// end of class Node // end of class
LinkedList
6
LinkedList implementation
public class LinkedListltElementgt extends
AbstractListltElementgt private int
size private Node first // Create an empty
LinkedListltElementgt. public LinkedList ()
size 0 first null
7
An empty linked list
8
Linked list with three entries
9
Method to get the i-th node
/ The i-th node of this LinkedList. The
LinkedList must be non-empty. require 0 lt i
i lt this.size() / private Node getNode (int
i) Node p first int pos 0 // p is
pos-th Node while (pos ! i) p
p.next pos pos 1 return p
10
Method to get the i-th node i 2
LinkedList
size
4
first
p
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
11
Method to get the i-th node i 2
LinkedList
size
4
first
p
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
12
Method to get the i-th node i 2
LinkedList
size
4
first
p
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
13
Implementing LinkedList methods
public Element get (int index) Node p
getNode(index) return p.element
  • Note method get
  • uses getNode
  • get(i) will take i1 steps to get the element.

14
Method remove(int)
  • Given an index referencing a node to be removed
  • Find previous node referencing it.
  • Update that nodes next.

15
Method remove(int)
  • Find previous node.

LinkedList
Element to be removed
size
4
first
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
16
Method remove(int)
LinkedList
Element to be removed
size
4
first
p
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
17
Method remove(int)
18
Method remove(int)
19
Method remove(int)
LinkedList
Element to be removed
size
4
first
p
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
20
Method remove(int)
LinkedList
Element to be removed
size
3
first
p
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
21
Method remove(int), special case
  • Recall getNode requires argument gt 0.
  • If index is 0, its not legal to write
  • Node p getNode(index-1)
  • Special case index 0 removes first linked
    element.
  • instance variable first of LinkedList must be
    modified.

22
Method remove(int)
LinkedList
size
4
node to be removed
first
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
23
Method remove(int)
LinkedList
size
4
node to be removed
first
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
copy
24
Method remove(int)
node to be removed
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
25
Method remove(int)
node to be removed
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
26
Implementation of remove(int)
public void remove (int index) if (index
0) first first.next else Node p
getNode(index-1) p.next p.next.next size
size - 1
27
Method add (int, Element)
  • To add an element at the i-th position
  • must find the element with index i-1
  • special case to consider when i is 0.

28
Method add(int, Element)
  • Add an element at index 2.
  • Must find previous node.

29
Method add(int, Element)
30
Method add(int, Element)
  • New node next is ps next

31
Method add(int, Element)
  • p node next is new node

Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
32
Method add(int, Element)
  • LinkedList size is incremented.

Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
33
Method add(int, Element)
  • add at index 0

Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
34
Method add(int, Element)
  • add at index 0

Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
35
Method add(int, Element)
  • add at index 0

Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
36
Method add (int, Element)
  • public void add (int index, Element element)
  • Node newNode new Node(element)
  • if (index 0)
  • newNode.next first
  • first newNode
  • else
  • Node p getNode(index-1)
  • newNode.next p.next
  • p.next newNode
  • size size 1

37
Linked list variations
  • Adding a reference to last node makes adding to
    end of list constant.

38
LinkedList adding a last reference
  • Creates special cases in other methods. For
    instance, remove

public void remove (int index) if (size 1)
// remove only element first null last
null else if (index 0) //remove first
element first first.next else Node p
getNode(index-1) p.next p.next.next
if (index size-1) //last element removed
last p size size - 1
39
Header nodes
  • dummy node
  • Always present at front of list,
  • contains no element,
  • first always references header node,
  • first is never null.
  • Reduces special cases in the implementation of
    some methods.

40
Header nodes
41
Circular lists
  • Last node references the first.

42
Doubly-linked lists
43
Doubly-linked list constructor
  • Assuming Header extends Node DoublyLinkedList
    constructor creates header Node, and links it to
    itself.

public DoublyLinkedList () size 0 header
new Header() header.next header header.previ
ous header
44
Doubly-linked list constructor
45
Doubly-linked lists add(Element)
Header
A
B
C
New
46
Doubly-linked lists add(Element)
  • new node previous is node C

Header
A
B
C
New
47
Doubly-linked lists add(Element)
  • new node next is header

Header
A
B
C
New
48
Doubly-linked lists add(Element)
  • Header previous is new node

Header
A
B
C
New
49
Doubly-linked lists add(Element)
  • C node next is new node

Header
A
B
C
New
50
Doubly-linked lists remove(1)
51
Doubly-linked lists remove(1)
Header
A
B
C
52
Doubly-linked lists remove(1)
Header
A
B
C
53
Linked Lists limitations
  • Element access by index is linear.
  • Would not use a linked implementation if primary
    operation is to access randomly by index.
  • Method get uses getNode requires i steps to get
    Node with index i.

54
Linked Lists limitations indexOf(Element)
  • indexOf uses method get.
  • If element not found, get executes n times, with
    index values 0 through n-1
  • value of i 0 1 2 n-1
  • steps of get(i) 0 1 2 n-1
  • Steps require 0 1 2 (n-1) (n2-n)/2.
  • Method is quadratic.

55
Re-implementation of indexOf
public int indexOf (Element element) Node p
first int pos 0 while (p ! null
!element.equals(p.element)) p p.next pos
pos1 if (p null) return
-1 else return pos
  • Method is linear.
  • But uses private members of the class LinkedList.

56
Dynamic memory allocation
  • Recall memory space for method variables
    parameters and local variables is allocated
    when the method is invoked, and reclaimed
    (deallocated) when the method completes.
  • This is called automatic allocation and
    deallocation.
  • Space for an objects instance variables is
    allocated when the object is created. This is
    sometimes called dynamic allocation.

57
Dynamic memory allocation
  • In an array-based list implementation, memory
    space for the array elements is allocated when
    the array is created.
  • In a linked implementation, space required for a
    node is allocated when the node is created, i.e.
    when an item is added to the list.
  • But when is this space deallocated?

58
Dynamic memory allocation
  • The Java run-time system implements a facility
    called garbage collection.
  • Dynamically allocated space that can no longer be
    accessed is termed garbage.
  • If we create an object and then lose all
    references to the object, the memory space
    occupied by the object becomes garbage.
  • The run-time system continuously looks for
    garbage and reclaims the space collects the
    garbage.

59
Dynamic memory allocation
  • Many programming languages do not include garbage
    collection as part of their run-time systems.
  • These languages require programs to explicitly
    deallocate dynamically allocated space.
  • Problem difficult to know when an object is no
    longer accessible and can be safely deallocated.
  • Subtle errors
  • Memory leaks garbage not being recognized and
    reclaimed
  • Dangling reference space still accessible being
    mistakenly reclaimed as garbage.
Write a Comment
User Comments (0)
About PowerShow.com