CSC 1351: Advanced Java - Interdisciplinary -...
Transcript of CSC 1351: Advanced Java - Interdisciplinary -...
CSC 1351: Advanced JavaLinked lists, Stacks, Queues
Dr Frank [email protected]
Center for Computation and TechnologyLouisiana State University, Baton Rouge, LA
Mar 16 2017
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Today: Basic Data Structures (other than arrays)
Linked Lists
singly linked listsdoubly linked lists
Queues
Stacks
Reading until Tuesday: Chapters 15.1, 15.2, 15.5, 16.1, 16.3QUIZ!
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Linked Lists
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Linked Lists
Linked List (LL):
Collection of nodes containing
some datasome reference(s) (links) to other nodes
Linked list example (singly linked list):
12 99 37
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Linked lists: Advantages/Disadvantages
Pros
can grow dynamically (although arrays can too)does not need contiguous memorysome operations fast (e.g., inserting element in some cases)item data not necessarily uniform
Cons
references add memory overheadsome operations slow, e.g., random look-upmaybe only able to traverse one way
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Linked list tips
Think of special cases
list emptywhat if there is only one element?
Draw diagrams if unsure.
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
List Variations
Singly linked lists
Doubly linked lists
Circular linked lists (both singly and doubly)
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
List Operations
Creation
Insertion
Removal
Deletion
Look-up / Retrieval
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Creation
Reduces to the question: How does an empty list look like?
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Creation
Reduces to the question: How does an empty list look like?
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Insert
Inserting first element...
12 99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Insert
Inserting first element...
12
99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Insert
Inserting first element...
12
99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Insert
Inserting first element...
12
99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Insert
Inserting next element...
12 99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Insert
Inserting next element...
12 99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Insert
Inserting next element...
12 99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Removal
Removing element “99”
12 99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Removal
Removing element “99”, requires look-up of element “12”
12 99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Removal
Removing element “99”, requires look-up of element “12”
12 99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Removal
Removing last element “12”
12
99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Removal
Removing last element “12”
12
99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Removal
Removing last element “12”
12
99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Removal
Removing last element “12”
12 99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Deletion
Remove all elements until list empty, clean up
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Look-up / Retrieval
12 99 37
O(N)
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Singly LL Operations: Look-up / Retrieval
12 99 37
O(N)
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly Linked Lists
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly linked list example
12 99 37
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Creation
An empty doubly linked list:
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Insert
Inserting elements:
12
99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Insert
Inserting elements:
12
99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Insert
Inserting elements:
12
99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Insert
Inserting elements:
12 99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Insert
Inserting elements:
12 99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Insert
Inserting elements:
12 99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Removal
Removing element “99”:
12 99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Removal
Removing element “99”: does not require expensive look-up:
12 99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Removal
Removing element “99”: does not require expensive look-up:
12 99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Removal
Removing element “99”: does not require expensive look-up:
12
99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Removal
Removing element “12”:
12
99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Removal
Removing element “12”:
12
99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Removal
Removing element “12”:
12 99
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Deletion
Remove all elements until list empty, clean up
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Look-up / Retrieval
12 99 37
still O(N), but access to end faster
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Doubly LL Operations: Look-up / Retrieval
12 99 37
still O(N), but access to end faster
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Lab today
Implement a doubly linked list.
Don’t use any of the Java-provided List classes.
Use provided “Node” class
No need for full LL interface - “just” the methods mentioned in thelab description
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Queues
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Queue
Fundamental data structure
Container of elements
Elements inserted and removed according to theFirst-In First-Out FIFO principle
Insertion of elements always possible (assuming enough space)
Removal only possible for “oldest” element
Elements only enter at “rear” and only leave at the “front”
Implementation usually using arrays or linked lists
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Queue Operations / Abstract Data Type
enqueue(): insert element at rear (“push”)
dequeue(): remove element at front (“pop”), error if queue empty
front(): return (but not remove) a reference to the front elementerror if empty
size(): get number of elements
empty(): “true” if queue empty, “false” otherwise
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Deque
Similar to queue, but with two “open ends”
Pronounced “deck” (like a deck of cards, and is similar to it)
Often implemented using doubly linked list
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Deque Operations / Abstract Data Type
insertFront(): Insert element at front
insertBack(): Insert element at back
eraseFront(): Remove first element, error if empty
eraseBack(): Remove last element, error if empty
front(): Return first element, error if empty
back(): Return last element, error if empty
size(): Return number of elements in deque
empty(): “true” if deque empty, “false” otherwise
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Stacks
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Stacks
Fundamental data structure
Container of elements
Elements inserted and removed according to the Last-In First-OutLIFO principle
Insertion of elements always possible (assuming enough space)
Removal only possible for “newest” element
Elements only enter on “top” and only leave from there
Implementation usually using arrays or linked lists
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Stack Operations / Abstract Data Type
push(): Insert element at top of stack
pop(): Remove element from top of stack, error if empty
top(): Return reference to element on top, error if empty
size(): Return number of elements
empty(): “true” if stack empty, “false” otherwise
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Stack Applications
Among the most common data structures
Especially useful in partsing and translation of computer languages,mathematics, and other formal notations
Parentheses matchingCompilers
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Using Stacks in Function Calls
Collection of information representing a function call → activation
Computer systems use runtime activation stack to keep track of thatinformation (return addresses, parameters, local variables)
Each function call results in push onto stack
Each function returns results in pop from stack
Using stack can help with debugging: backtrace
Turns recursive functions into series of stack pushes
Recursive functions and require a lot of stack memory... and can run out of it → stack overflow
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise I
Stack S initially empty
In unspecified order:
25 push operations
12 top operations
10 pop operations, of which 3 generated StackEmpty exception(caught and ignored)
What is the current size of S?
25− (10− 3) = 18
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise I
Stack S initially empty
In unspecified order:
25 push operations
12 top operations
10 pop operations, of which 3 generated StackEmpty exception(caught and ignored)
What is the current size of S?
25− (10− 3) = 18
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise II
Describe the output of the following operations on a stack:
push(5)
push(3)
print pop()
push(2)
push(8)
print pop()
print pop()
push(9)
push(1)
print pop()
push(7)
print pop()
print pop()
3, 8, 2, 1, 7, 9
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise II
Describe the output of the following operations on a stack:
push(5)
push(3)
print pop()
push(2)
push(8)
print pop()
print pop()
push(9)
push(1)
print pop()
push(7)
print pop()
print pop()
3,
8, 2, 1, 7, 9
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise II
Describe the output of the following operations on a stack:
push(5)
push(3)
print pop()
push(2)
push(8)
print pop()
print pop()
push(9)
push(1)
print pop()
push(7)
print pop()
print pop()
3, 8,
2, 1, 7, 9
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise II
Describe the output of the following operations on a stack:
push(5)
push(3)
print pop()
push(2)
push(8)
print pop()
print pop()
push(9)
push(1)
print pop()
push(7)
print pop()
print pop()
3, 8, 2,
1, 7, 9
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise II
Describe the output of the following operations on a stack:
push(5)
push(3)
print pop()
push(2)
push(8)
print pop()
print pop()
push(9)
push(1)
print pop()
push(7)
print pop()
print pop()
3, 8, 2, 1,
7, 9
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise II
Describe the output of the following operations on a stack:
push(5)
push(3)
print pop()
push(2)
push(8)
print pop()
print pop()
push(9)
push(1)
print pop()
push(7)
print pop()
print pop()
3, 8, 2, 1, 7,
9
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise II
Describe the output of the following operations on a stack:
push(5)
push(3)
print pop()
push(2)
push(8)
print pop()
print pop()
push(9)
push(1)
print pop()
push(7)
print pop()
print pop()
3, 8, 2, 1, 7, 9Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise III
Describe the output of the following operations on a queue:
enqueue(5)
enqueue(3)
print dequeue()
enqueue(2)
enqueue(8)
print dequeue()
print dequeue()
enqueue(9)
print dequeue()
print dequeue()
print dequeue()
5, 3, 2, 8, 9, ERROR
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise III
Describe the output of the following operations on a queue:
enqueue(5)
enqueue(3)
print dequeue()
enqueue(2)
enqueue(8)
print dequeue()
print dequeue()
enqueue(9)
print dequeue()
print dequeue()
print dequeue()
5,
3, 2, 8, 9, ERROR
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise III
Describe the output of the following operations on a queue:
enqueue(5)
enqueue(3)
print dequeue()
enqueue(2)
enqueue(8)
print dequeue()
print dequeue()
enqueue(9)
print dequeue()
print dequeue()
print dequeue()
5, 3,
2, 8, 9, ERROR
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise III
Describe the output of the following operations on a queue:
enqueue(5)
enqueue(3)
print dequeue()
enqueue(2)
enqueue(8)
print dequeue()
print dequeue()
enqueue(9)
print dequeue()
print dequeue()
print dequeue()
5, 3, 2,
8, 9, ERROR
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise III
Describe the output of the following operations on a queue:
enqueue(5)
enqueue(3)
print dequeue()
enqueue(2)
enqueue(8)
print dequeue()
print dequeue()
enqueue(9)
print dequeue()
print dequeue()
print dequeue()
5, 3, 2, 8,
9, ERROR
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise III
Describe the output of the following operations on a queue:
enqueue(5)
enqueue(3)
print dequeue()
enqueue(2)
enqueue(8)
print dequeue()
print dequeue()
enqueue(9)
print dequeue()
print dequeue()
print dequeue()
5, 3, 2, 8, 9,
ERROR
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise III
Describe the output of the following operations on a queue:
enqueue(5)
enqueue(3)
print dequeue()
enqueue(2)
enqueue(8)
print dequeue()
print dequeue()
enqueue(9)
print dequeue()
print dequeue()
print dequeue()
5, 3, 2, 8, 9, ERROR
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise IV
Describe the output of the following operations on a deque:
insertFront(3)
insertBack(8)
insertBack(9)
insertFront(5)
print eraseFront()
print eraseBack()
print first()
insertBack(7)
print eraseFront()
print last()
print eraseBack()
5, 9, 3, 3, 7, 7
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise IV
Describe the output of the following operations on a deque:
insertFront(3)
insertBack(8)
insertBack(9)
insertFront(5)
print eraseFront()
print eraseBack()
print first()
insertBack(7)
print eraseFront()
print last()
print eraseBack()
5,
9, 3, 3, 7, 7
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise IV
Describe the output of the following operations on a deque:
insertFront(3)
insertBack(8)
insertBack(9)
insertFront(5)
print eraseFront()
print eraseBack()
print first()
insertBack(7)
print eraseFront()
print last()
print eraseBack()
5, 9,
3, 3, 7, 7
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise IV
Describe the output of the following operations on a deque:
insertFront(3)
insertBack(8)
insertBack(9)
insertFront(5)
print eraseFront()
print eraseBack()
print first()
insertBack(7)
print eraseFront()
print last()
print eraseBack()
5, 9, 3,
3, 7, 7
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise IV
Describe the output of the following operations on a deque:
insertFront(3)
insertBack(8)
insertBack(9)
insertFront(5)
print eraseFront()
print eraseBack()
print first()
insertBack(7)
print eraseFront()
print last()
print eraseBack()
5, 9, 3, 3,
7, 7
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise IV
Describe the output of the following operations on a deque:
insertFront(3)
insertBack(8)
insertBack(9)
insertFront(5)
print eraseFront()
print eraseBack()
print first()
insertBack(7)
print eraseFront()
print last()
print eraseBack()
5, 9, 3, 3, 7,
7
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Exercise IV
Describe the output of the following operations on a deque:
insertFront(3)
insertBack(8)
insertBack(9)
insertFront(5)
print eraseFront()
print eraseBack()
print first()
insertBack(7)
print eraseFront()
print last()
print eraseBack()
5, 9, 3, 3, 7, 7
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Java Collections Framework
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Java Collection Methods
constructor, e.g., new ArrayList<String>()
number of elements: size()
adding element: add()
removing element: remove()
check if collection contains element: contains(...)
string representation of collection: toString()
iterating over elements: iterator()
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Java Linked Lists
Inherit Collection
Are doubly-linked lists
Methods:
constructor: new LinkedList<String>()
add element to end: addLast(...) (same as add(...))
add element to front: addFirst(...)
get first element: getFirst()
get last element: getLast()
remove first element: removeFirst()
remove last element: removeLast()
list iterator: listIterator()
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Java Stacks
constructor: new Stack<String>()
add to top: push(...)
remove from top: pop() (returns removed element)
look at top element: peek() (does not remove element)
Frank Loffler CSC 1351: Advanced Java Mar 16 2017
Java Queues
constructor: new Queue<String>()
add to tail: add(...)
remove head: remove() (returns removed element)
look at first element: peek() (does not remove element)
Frank Loffler CSC 1351: Advanced Java Mar 16 2017