Data Structures in Java
Lecture 2: Array and Linked Lists.
9/9/2015
1
Daniel Bauer
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
Lecture 2: Array and Linked Lists.
9/9/2015
1
Daniel Bauer
A0 A2 A3 A4 A5 A6 A1
A0 A2 A3 A4 A5 A6 A1
is called the empty list. A0 A2 A3 A4 A5 A6 A1
is called the empty list.
A0 A2 A3 A4 A5 A6 A1
is called the empty list.
A0 A2 A3 A4 A5 A6 A1
A0 A2 A3 A4 A5 A6 A1
A0 A2 A3 A4 A5 A6 A1
A0 A2 A3 A4 A5 A6 A1
A0 A2 A3 A4 A5 A6 A1
A0 A2 A3 A4 A5 A6 A1
A0 A2 A3 A4 A5 A6 A1
A0 A2 A3 A4 A5 A6 A1
A0 A2 A3 A4 A5 A6 A1
A0 A2 A3 A4 A5 A6 A1
A0 A2 A3 A4 A5 A6 A1
A0 A2 A3 A4 A5 A6 A1
1 7 3 5 2 1 3
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]; } }
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 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
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
1 7 3 5 2 1 3
insert(x,k) remove(x)
1 2 3 4 5 6 7 8 9 N=7
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
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
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
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
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 ];
insert
A0 A1 A2 A3
public class Node { public Integer data; public Node next; public Node(Integer d, Node n) { data = d; next = n; } }
null
42 23 5 9 null
printList find(x) findKth(k) next()
42 23 5 9 null
printList find(x) findKth(k) next()
N
42 23 5 9 null
printList find(x) findKth(k) next()
N N
42 23 5 9 null
printList find(x) findKth(k) next()
N N k
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
42 23 5 9 null
findKth(k) k next() 1 insert(x,k) remove(k)
42 23 5 9 null
findKth(k) k next() 1 insert(x,k) remove(k)
remove(2)
search time + 1
42 23 9 null
findKth(k) k next() 1 insert(x,k) remove(k)
remove(2)
search time + 1
42 23 9 null
insert(x,k)
42 23 9 null
insert(x,k)
insert(5,2)
42 23 9 null
insert(x,k)
insert(5,2)
42 23 9 null
insert(x,k)
insert(5,2) 5
42 23 9 null
insert(x,k)
insert(5,2) 5
42 23 9 null
insert(x,k)
insert(5,2)
search time +1
5
42 23 9 null
insert(x,k)
insert(5,2)
search time +1
5 Inserting in position 0? Inserting in position N-1?
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.
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; } }
(e.g. removing first node/last node) A0 A1 A2 A3 head tail
head tail