CSC 1351: Advanced Java - Interdisciplinary -...

85
CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L¨ offler [email protected] Center for Computation and Technology Louisiana State University, Baton Rouge, LA Mar 16 2017 Frank L¨ offler CSC 1351: Advanced Java Mar 16 2017

Transcript of CSC 1351: Advanced Java - Interdisciplinary -...

Page 1: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 2: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 3: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Linked Lists

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 4: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 5: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 6: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 7: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

List Variations

Singly linked lists

Doubly linked lists

Circular linked lists (both singly and doubly)

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 8: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

List Operations

Creation

Insertion

Removal

Deletion

Look-up / Retrieval

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 9: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Creation

Reduces to the question: How does an empty list look like?

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 10: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Creation

Reduces to the question: How does an empty list look like?

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 11: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Insert

Inserting first element...

12 99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 12: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Insert

Inserting first element...

12

99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 13: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Insert

Inserting first element...

12

99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 14: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Insert

Inserting first element...

12

99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 15: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Insert

Inserting next element...

12 99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 16: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Insert

Inserting next element...

12 99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 17: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Insert

Inserting next element...

12 99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 18: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Removal

Removing element “99”

12 99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 19: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Removal

Removing element “99”, requires look-up of element “12”

12 99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 20: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Removal

Removing element “99”, requires look-up of element “12”

12 99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 21: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Removal

Removing last element “12”

12

99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 22: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Removal

Removing last element “12”

12

99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 23: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Removal

Removing last element “12”

12

99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 24: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Removal

Removing last element “12”

12 99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 25: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Deletion

Remove all elements until list empty, clean up

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 26: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Look-up / Retrieval

12 99 37

O(N)

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 27: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Singly LL Operations: Look-up / Retrieval

12 99 37

O(N)

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 28: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly Linked Lists

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 29: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly linked list example

12 99 37

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 30: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly LL Operations: Creation

An empty doubly linked list:

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 31: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly LL Operations: Insert

Inserting elements:

12

99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 32: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly LL Operations: Insert

Inserting elements:

12

99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 33: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly LL Operations: Insert

Inserting elements:

12

99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 34: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly LL Operations: Insert

Inserting elements:

12 99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 35: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly LL Operations: Insert

Inserting elements:

12 99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 36: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly LL Operations: Insert

Inserting elements:

12 99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 37: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly LL Operations: Removal

Removing element “99”:

12 99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 38: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly LL Operations: Removal

Removing element “99”: does not require expensive look-up:

12 99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 39: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly LL Operations: Removal

Removing element “99”: does not require expensive look-up:

12 99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 40: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly LL Operations: Removal

Removing element “99”: does not require expensive look-up:

12

99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 41: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly LL Operations: Removal

Removing element “12”:

12

99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 42: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly LL Operations: Removal

Removing element “12”:

12

99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 43: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly LL Operations: Removal

Removing element “12”:

12 99

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 44: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Doubly LL Operations: Deletion

Remove all elements until list empty, clean up

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 45: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 46: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 47: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 48: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Queues

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 49: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 50: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 51: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 52: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 53: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Stacks

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 54: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 55: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 56: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 57: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 58: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 59: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 60: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 61: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 62: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 63: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 64: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 65: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 66: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 67: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 68: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 69: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 70: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 71: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 72: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 73: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 74: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 75: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 76: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 77: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 78: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 79: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 80: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 81: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

Java Collections Framework

Frank Loffler CSC 1351: Advanced Java Mar 16 2017

Page 82: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 83: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 84: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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

Page 85: CSC 1351: Advanced Java - Interdisciplinary - Inventivesbrandt/csc1351/examples/stacks_queues.pdf · CSC 1351: Advanced Java Linked lists, Stacks, Queues Dr Frank L o er knarf@cct.lsu.edu

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