Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Recall last lecture: Java array
nullnull
34657-232-82323
119200
I have drawn each of these as array lists.
arrayof Shapeobjects
array of int
2
array(unspecified
type)
Java ArrayList class
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
It uses an array as the underlying data structure
It grows the array (by 50%, not 100%) when the array is fulland a new element is added.
You don’t use the usual array notation a[ ]. Instead, useget() and set() and other methods.
3
Java generic type
An array of what? ArrayList<T>
Example:
ArrayList< Shape > shape = new ArrayList< Shape >();
// initializes the array length (capacity) to 10
ArrayList< Shape > shape = new ArrayList< Shape >( 23 );
// initializes the array length to 23
4
Java ArrayList object
nullnull
Has private field thatholds the number ofelements in the list(size).
Has a private field thatreferences an arrayobject.
6
These Shape objectsdo not belong to theArrayList object.
Rather they arereferenced by it.
5
a(private)
8
array list linked list
null
null
null
null
Array slots are inconsecutive locations(addresses) in memory,but objects can beanywhere.
Linked list “nodes” andobjects can be anywherein memory.
9
Singly linked list node (“S” for singly)
class SNode<E> {
SNode<E> next;E element;:
}
e.g. E might be Shape
next element
10
A linked list consists of a sequence of nodes, along witha reference to the first (head) and last (tail) node.
head
tail
11
class SLinkedList<E> {
SNode<E> head;SNode<E> tail;int size;
:
private class SNode<E> { // inner class
SNode<E> next;E element;:
}}
Linked list operations
• addFirst ( e )
• removeFirst( )
• addLast ( e )
• removeLast( )
• ……. many other list operations
12
14
construct newNodenewNode.element = enewNode.next = head
addFirst ( e ) pseudocode
head
newNode
next element e
etc
15
construct newNodenewNode.element = enewNode.next = head
// edge caseif head == null
tail = newNode
head = newNodesize = size+1
addFirst ( e ) pseudocode
head
newNode
next element e
etc
head
newNodenext element e
etc
BEFOREAFTER
18
tmp = headhead = head.nexttmp.next = nullsize = size – 1
removeFirst ( ) pseudocode
headtmp
next element
head
tmpnext element
null
BEFORE
AFTER
19
tmp = head
if (size == 0)throw exception
head = head.nexttmp.next = nullsize = size – 1
if (size == 0) // size was 1tail = null
removeFirst() edge cases (size is 0 or 1)
headtmp
next element
head
tmpnext element
null
BEFORE
AFTER
null
Worse Case Time Complexity (N = size)
20
array list linked list
addFirst O( N ) O( 1 )
removeFirst O( N ) O( 1 )
Worse Case Time Complexity (N = size)
21
array list linked list
addFirst O( N ) O( 1 )
removeFirst O( N ) O( 1 )
addLast O( 1 )* ?
removeLast O( 1 ) ?
*if array is not full
23
newNode = construct a new nodenewNode.element = the new list elementtail.next = newNode
// … and then after what// figure shows we do:
tail = tail.nextsize = size+1
addLast ( )
newNode
next element
tail
:
24
removeLast ( )
BEFORE AFTERhead
tail
head
tail
Problem: we have no direct way to access the node before tail.
25
removeLast ( )
if (head == tail){head = nulltail = null
}else {
tmp = headwhile (tmp.next != tail)
tmp = tmp.next
tail = tmptail.next = null
}size = size - 1// to return the element, you need to do a bit more
head
tmp
tail
next element
Time Complexity (N = list size)
26
array list linked list
addFirst O( N ) O( 1 )
removeFirst O( N ) O( 1 )
addLast O( 1 )* O( 1 )
removeLast O( 1 ) O( N )
*if array is not full
27
class SLinkedList<E> {
SNode<E> head;SNode<E> tail;int size;
: // various methods
private class SNode<E> { // inner class
SNode<E> next;E element;:
}}
28
class SLinkedList<E> {
SNode<E> head;SNode<E> tail;int size;
:}
head
size4
tail
SLinkedListobject
Announcements
• When I make mistakes on slides/lecture notes/exercises,please email me rather posting on discussion board.
(However, compare the date on your version with the one on thepublic web page. I may have already corrected it.)
• Assignment 1 should be posted tomorrow (due in 2 weeks)
• Quiz 1 on Monday, Sept 25 (lectures 1-2, 4-6). Online.
• Coding tutorials on lists (coming soon)
31