Data Structures in Java Lecture 2: Array and Linked Lists. 9/9/2015 - - PowerPoint PPT Presentation

data structures in java
SMART_READER_LITE
LIVE PREVIEW

Data Structures in Java Lecture 2: Array and Linked Lists. 9/9/2015 - - PowerPoint PPT Presentation

Data Structures in Java Lecture 2: Array and Linked Lists. 9/9/2015 Daniel Bauer 1 The List ADT A 0 A 1 A 2 A 3 A 4 A 5 A 6 The List ADT A list L is a sequence of N objects A 0 , A 1, A 2, , A N-1 A 0 A 1 A 2 A 3 A 4 A 5 A 6 The List


slide-1
SLIDE 1

Data Structures in Java

Lecture 2: Array and Linked Lists.

9/9/2015

1

Daniel Bauer

slide-2
SLIDE 2

The List ADT

A0 A2 A3 A4 A5 A6 A1

slide-3
SLIDE 3

The List ADT

  • A list L is a sequence of N objects A0, A1, A2, …, AN-1

A0 A2 A3 A4 A5 A6 A1

slide-4
SLIDE 4

The List ADT

  • A list L is a sequence of N objects A0, A1, A2, …, AN-1
  • N is the length/size of the list. List with length N=0

is called the empty list. A0 A2 A3 A4 A5 A6 A1

slide-5
SLIDE 5

The List ADT

  • A list L is a sequence of N objects A0, A1, A2, …, AN-1
  • N is the length/size of the list. List with length N=0

is called the empty list.

  • Ai follows/succeeds Ai-1 for i > 0.

A0 A2 A3 A4 A5 A6 A1

slide-6
SLIDE 6

The List ADT

  • A list L is a sequence of N objects A0, A1, A2, …, AN-1
  • N is the length/size of the list. List with length N=0

is called the empty list.

  • Ai follows/succeeds Ai-1 for i > 0.
  • Ai precedes Ai+1 for i < N.

A0 A2 A3 A4 A5 A6 A1

slide-7
SLIDE 7

Typical List Operations

A0 A2 A3 A4 A5 A6 A1

slide-8
SLIDE 8

Typical List Operations

  • void printList()

A0 A2 A3 A4 A5 A6 A1

slide-9
SLIDE 9

Typical List Operations

  • void printList()
  • void makeEmpty()

A0 A2 A3 A4 A5 A6 A1

slide-10
SLIDE 10

Typical List Operations

  • void printList()
  • void makeEmpty()
  • int size()

A0 A2 A3 A4 A5 A6 A1

slide-11
SLIDE 11

Typical List Operations

  • void printList()
  • void makeEmpty()
  • int size()
  • Object findKth(k) / get(k)

A0 A2 A3 A4 A5 A6 A1

slide-12
SLIDE 12

Typical List Operations

  • void printList()
  • void makeEmpty()
  • int size()
  • Object findKth(k) / get(k)
  • boolean insert(x, k), append(x)

A0 A2 A3 A4 A5 A6 A1

slide-13
SLIDE 13

Typical List Operations

  • void printList()
  • void makeEmpty()
  • int size()
  • Object findKth(k) / get(k)
  • boolean insert(x, k), append(x)
  • boolean remove(k)

A0 A2 A3 A4 A5 A6 A1

slide-14
SLIDE 14

Typical List Operations

  • void printList()
  • void makeEmpty()
  • int size()
  • Object findKth(k) / get(k)
  • boolean insert(x, k), append(x)
  • boolean remove(k)
  • int find(x) / indexOf(x)

A0 A2 A3 A4 A5 A6 A1

slide-15
SLIDE 15

Typical List Operations

  • void printList()
  • void makeEmpty()
  • int size()
  • Object findKth(k) / get(k)
  • boolean insert(x, k), append(x)
  • boolean remove(k)
  • int find(x) / indexOf(x)
  • Object next()

A0 A2 A3 A4 A5 A6 A1

slide-16
SLIDE 16

Typical List Operations

  • void printList()
  • void makeEmpty()
  • int size()
  • Object findKth(k) / get(k)
  • boolean insert(x, k), append(x)
  • boolean remove(k)
  • int find(x) / indexOf(x)
  • Object next()
  • Object previous()

A0 A2 A3 A4 A5 A6 A1

slide-17
SLIDE 17

Typical List Operations

  • void printList()
  • void makeEmpty()
  • int size()
  • Object findKth(k) / get(k)
  • boolean insert(x, k), append(x)
  • boolean remove(k)
  • int find(x) / indexOf(x)
  • Object next()
  • Object previous()
  • void removeCurrent()

A0 A2 A3 A4 A5 A6 A1

slide-18
SLIDE 18

Array Lists

1 7 3 5 2 1 3

  • Just a thin layer wrapping an array.

public class SimpleArrayList implements List{ public static final int DEFAULT_CAPACITY = 10; private int theSize; private Integer[] theItems; public SimpleArrayList() { theItems = new Integer[DEFAULT_CAPACITY]; } }

slide-19
SLIDE 19

Running Time for Array List Operations

1 7 3 5 2 1 3

Operation Number of Steps printList find(x) findKth(k) insert(x,k) remove(x)

1 2 3 4 5 6 7 8 9 N=7

slide-20
SLIDE 20

Running Time for Array List Operations

1 7 3 5 2 1 3

Operation Number of Steps printList find(x) findKth(k) insert(x,k) remove(x)

1 2 3 4 5 6 7 8 9 N=7 N N

slide-21
SLIDE 21

Running Time for Array List Operations

1 7 3 5 2 1 3

Operation Number of Steps printList find(x) findKth(k) insert(x,k) remove(x)

1 2 3 4 5 6 7 8 9 N=7 1 N N

slide-22
SLIDE 22

Array List: Insert/Remove

1 7 3 5 2 1 3

insert(x,k) remove(x)

1 2 3 4 5 6 7 8 9 N=7

slide-23
SLIDE 23

Array List: Insert/Remove

1 7 3 5 2 1 3

insert(x,k) remove(x)

1 2 3 4 5 6 7 8 9 insert(5,7): 1 step 5 N=7

slide-24
SLIDE 24

Array List: Insert/Remove

1 7 3 5 2 1 3

insert(x,k) remove(x)

1 2 3 4 5 6 7 8 9 insert(5,7): 1 step best case remove(7): 1 step N=7

slide-25
SLIDE 25

Array List: Insert/Remove

1 7 3 5 2 1 3

insert(x,k) remove(x)

1 2 3 4 5 6 7 8 9 insert(5,7): 1 step best case remove(7): 1 step insert(5,0): 7 steps worst case 5 7 moves N=7

slide-26
SLIDE 26

Array List: Insert/Remove

1 7 3 5 2 1 3

insert(x,k) remove(x)

1 2 3 4 5 6 7 8 9 insert(5,7): 1 step best case remove(7): 1 step insert(5,0): 7 steps remove(0): O(N) worst case 5 N N 7 moves N=7

slide-27
SLIDE 27

Expanding Array Lists

1 7 3 5 2 1 7 3 5 2 1 2 3 4 5 6 7 8 9

newCapacity = arr.length * 2; Integer[ ] old = theItems; theItems = new Integer[newCapacity]; for( int i = 0; i < size( ); i++ ) theItems[ i ] = old[ i ];

  • What if we are running out of space during append/

insert

  • first copy all elements into a new array of sufficient size
slide-28
SLIDE 28

Simple Linked Lists

  • Series of Nodes. Each Node contains:
  • A reference to the data object it contains.
  • A reference to the next node in the List.

A0 A1 A2 A3

public class Node { public Integer data; public Node next; public Node(Integer d, Node n) { data = d; next = n; } }

null

slide-29
SLIDE 29

Running Time for Simple Linked List Operations

42 23 5 9 null

printList find(x) findKth(k) next()

slide-30
SLIDE 30

Running Time for Simple Linked List Operations

42 23 5 9 null

printList find(x) findKth(k) next()

N

slide-31
SLIDE 31

Running Time for Simple Linked List Operations

42 23 5 9 null

printList find(x) findKth(k) next()

N N

slide-32
SLIDE 32

Running Time for Simple Linked List Operations

42 23 5 9 null

printList find(x) findKth(k) next()

N N k

slide-33
SLIDE 33

Running Time for Simple Linked List Operations

42 23 5 9 null

printList find(x) findKth(k) next()

In many applications we can use next() instead of findKth(k). (for every element in the list do… / filter the list … )

N N k 1

slide-34
SLIDE 34

Simple Linked List Removal

42 23 5 9 null

findKth(k) k next() 1 insert(x,k) remove(k)

slide-35
SLIDE 35

Simple Linked List Removal

42 23 5 9 null

findKth(k) k next() 1 insert(x,k) remove(k)

remove(2)

search time + 1

slide-36
SLIDE 36

Simple Linked List Removal

42 23 9 null

findKth(k) k next() 1 insert(x,k) remove(k)

remove(2)

search time + 1

slide-37
SLIDE 37

Simple Linked List Insertion

42 23 9 null

insert(x,k)

slide-38
SLIDE 38

Simple Linked List Insertion

42 23 9 null

insert(x,k)

insert(5,2)

slide-39
SLIDE 39

Simple Linked List Insertion

42 23 9 null

insert(x,k)

insert(5,2)

slide-40
SLIDE 40

Simple Linked List Insertion

42 23 9 null

insert(x,k)

insert(5,2) 5

slide-41
SLIDE 41

Simple Linked List Insertion

42 23 9 null

insert(x,k)

insert(5,2) 5

slide-42
SLIDE 42

Simple Linked List Insertion

42 23 9 null

insert(x,k)

insert(5,2)

search time +1

5

slide-43
SLIDE 43

Simple Linked List Insertion

42 23 9 null

insert(x,k)

insert(5,2)

search time +1

5 Inserting in position 0? Inserting in position N-1?

slide-44
SLIDE 44

Simple Linked List Insertion

42 23 9 null

insert(x,k)

insert(5,2)

search time +1

5 Inserting in position 0? Inserting in position N-1? Linked list should remember the first and last object.

slide-45
SLIDE 45

Doubly Linked Lists

  • Also maintain reference to previous node in the list.
  • Speeds up append at end of list.

A0 A1 A2 A3

private class Node { public Integer data; public Node next; public Node prev; public Node(Integer d, Node n, Node p) { data = d; next = n; prev = n; } }

slide-46
SLIDE 46

Doubly Linked List with Sentinel Nodes

  • header node, tail node
  • make implementation of next / previous easier.
  • Remove other special cases 


(e.g. removing first node/last node) A0 A1 A2 A3 head tail

slide-47
SLIDE 47

Empty Doubly Linked List with Sentinel Nodes

head tail