40
Lecture objectives Collections interface Learn about stacks and their methods: push pop peek Empty Analyze stack applications and why stacks were the best choice in these cases Use the stack in an actual implementation: “Code stack” CS340 1

Lecture objectives Collections interface Learn about stacks and their methods: push pop peek Empty Analyze stack applications and why stacks

Embed Size (px)

Citation preview

Page 1: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

1

Lecture objectives

Collections interface Learn about stacks and their methods:

push pop peek Empty

Analyze stack applications and why stacks were the best choice in these cases

Use the stack in an actual implementation: “Code stack”

Page 2: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

2 The Collections Framework Design

CS340

Page 3: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

3

The Collection Interface

Specifies a subset of methods in the List interface, specifically excluding add(int, E) get(int) remove(int) set(int, E)

but including add(E) remove(E) the iterator method

Page 4: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS3404

The Collection Framework

Page 5: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

5

Common Features of Collections Collections

grow as needed hold references to objects have at least two constructors:

one to create an empty collection one to make a copy of another collection

Page 6: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

6

CS340

Common Features of Collections (cont.)

In a general Collection the order of elements is not specified

For collections implementing the List interface, the order of the elements is determined by the index

Page 7: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

7

CS340

Common Features of Collections (cont.)

In a general Collection, the position where an object is inserted is not specified

In ArrayList and LinkedList, add(E) always inserts at the end and always returns true

Page 8: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

8

AbstractCollection, AbstractList, and AbstractSequentialList

“Helper" abstract classes: help build implementations of their corresponding interfaces

Extend the AbstractCollection class and implement only the desired methods

Page 9: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

9

Implementing a Subclass of Collection<E>

Extend AbstractCollection<E>, which implements most operations

You need to implement only: add(E) size() iterator() An inner class that implements Iterator<E>

Page 10: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

10

Implementing a Subclass of List<E>

Extend AbstractList<E> You need to implement only:

add(int, E) get(int) remove(int) set(int, E) size()

AbstractList implements Iterator<E> using the index

Page 11: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

11

AbstractCollection, AbstractList, and AbstractSequentialList

Another more complete way to declare CS340ArrayList is:

public class CS340ArrayList<E> extends AbstractList<E> implements List<E>

Another more complete, way to declare CS340LinkedList is:

public class CS340LinkedList<E> extends AbstractSequentialList<E> implements List<E>

Page 12: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

12

CS340

Stack Abstract Data Type

Page 13: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

13

Stack Abstract Data Type

A stack is one of the most commonly used data structures in computer science

A stack example: dirty plates Only the top item can be accessed You can extract only one item at a

time The stack’s storage policy is

Last-In, First-Out, or LIFO

Page 14: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

14

Specification of the Stack Abstract Data Type

Only the top element of a stack is visible; therefore the number of operations performed by a stack are few

We need the ability to test for an empty stack (empty) inspect the top element (peek) retrieve the top element (pop) put a new element on the stack (push)

Page 15: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

15

A Stack of Strings

String last = cities.peek() String temp = cities.pop() cities.push(“Tampa”) cities.push(“Boston”) String temp = cities.pop()

Atlanta

Raleigh

Norfolk

Wichita

Raleigh

Norfolk

Wichita

Raleigh

Norfolk

Wichita

Tampa

Raleigh

Norfolk

Wichita

Tampa

Boston

Page 16: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

16

A Stack of Strings

String last = cities.peek() String temp = cities.pop() cities.push(“Tampa”) cities.push(“Boston”) String temp = cities.pop()

Raleigh

Norfolk

Wichita

Tampa

Page 17: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

17 Stack Applications

Click icon to add picture

CS340

Page 18: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

18

Finding Palindromes

Palindrome: a string that reads identically in either direction, letter by letter (ignoring case) kayak "I saw I was I" “Able was I ere I saw Elba” "Level, madam, level"

Problem: Write a program that reads a string and determines whether it is a palindrome

Page 19: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

19

Finding Palindromes (cont.)

Page 20: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

20

Finding Palindromes (cont.)import java.util.*;

public class PalindromeFinder {

private String inputString;

private Stack<Character> charStack = new Stack<Character>();

public PalindromeFinder(String str) {

inputString = str;

fillStack();

}

...

Page 21: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

21

Finding Palindromes (cont.)

Solving using a stack: Push each string character, from left to

right, onto a stack

ka

k

y

ka

ka

a

aya y a kk

k

y

a

ka y a kk

private void fillStack() { for(int i = 0; i < inputString.length(); i++) { charStack.push(inputString.charAt(i)); }}

Page 22: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

22

kaykaykaa

Finding Palindromes (cont.)

Solving using a stack: Pop each character off the stack, appending

each to the StringBuilder result

k

k

a

a

k

y

a

k

y

a

ka y akk

private String buildReverse(){ StringBuilder result = new StringBuilder(); while(!charStack.empty()) { result.append(charStack.pop()); } return result.toString();}

Page 23: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

23

Finding Palindromes (cont.) ...

public boolean isPalindrome() {

return inputString.equalsIgnoreCase(buildReverse());

}

}

Page 24: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

24

Testing

To test this class using the following inputs: a single character (always a palindrome) multiple characters in a word multiple words different cases even-length strings odd-length strings the empty string (considered a palindrome)

Page 25: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

25

Balanced Parentheses

When analyzing arithmetic expressions, it is important to determine whether an expression is balanced with respect to parentheses

( a + b * ( c / ( d – e ) ) ) + ( d / e )

The problem is further complicated if braces or brackets are used in conjunction with parentheses

The solution is to use stacks!

Page 26: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

26

Balanced Parentheses (cont.)

Page 27: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

27

Balanced Parentheses (cont.)

Page 28: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

28

Balanced Parentheses (cont.)

Expression:

balanced : trueindex : 0

(w * [x + y] / z)

1 4 5 6 7 8 9 10320

w x + y ] / z )[*(( (

Page 29: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

29

Balanced Parentheses (cont.)

Expression:

balanced : trueindex : 1

(w * [x + y] / z)

1 4 5 6 7 8 9 10320

w x + y ] / z )[*(( (

Page 30: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

30

Balanced Parentheses (cont.)

Expression:

balanced : trueindex : 2

(w * [x + y] / z)

1 4 5 6 7 8 9 10320

w x + y ] / z )[*(( (

Page 31: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

31

Balanced Parentheses (cont.)

Expression:

balanced : trueindex : 3

(w * [x + y] / z)

1 4 5 6 7 8 9 10320

w x + y ] / z )[*(

(

( [[(

Page 32: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

32

Balanced Parentheses (cont.)

Expression:

balanced : trueindex : 4

(w * [x + y] / z)

1 4 5 6 7 8 9 10320

w x + y ] / z )[*(

(

([

Page 33: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

33

Balanced Parentheses (cont.)

Expression:

balanced : trueindex : 5

(w * [x + y] / z)

1 4 5 6 7 8 9 10320

w x + y ] / z )[*(

(

([

Page 34: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

34

Balanced Parentheses (cont.)

Expression:

balanced : trueindex : 6

(w * [x + y] / z)

1 4 5 6 7 8 9 10320

w x + y ] / z )[*(

(

([

Page 35: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

35

Balanced Parentheses (cont.)

Expression:

balanced : trueindex : 7

(w * [x + y] / z)

1 4 5 6 7 8 9 10320

w x + y ] / z )[*(

(

([(

Matches! Balanced still true

Page 36: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

36

Balanced Parentheses (cont.)

Expression:

balanced : trueindex : 8

(w * [x + y] / z)

1 4 5 6 7 8 9 10320

w x + y ] / z )[*(((

Page 37: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

37

Balanced Parentheses (cont.)

Expression:

balanced : trueindex : 9

(w * [x + y] / z)

1 4 5 6 7 8 9 10320

w x + y ] / z )[*(((

Page 38: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

38

Balanced Parentheses (cont.)

Expression:

balanced : trueindex : 10

(w * [x + y] / z)

1 4 5 6 7 8 9 10320

w x + y ] / z )[*(((

Matches! Balanced still true

Page 39: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

39

Testing

Provide a variety of input expressions displaying the result true or false

Try several levels of nested parentheses Try nested parentheses where corresponding

parentheses are not of the same type Try unbalanced parentheses

PITFALL: attempting to pop an empty stack will throw an EmptyStackException. You can guard against this by either testing for an empty stack or catching the exception

Page 40: Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks

CS340

40

Code Stack

When analyzing code, we execute methods, code stack changes

public static void main(String[] args) {

//create a new myLibrary

MyLibrary testLibrary = new MyLibrary("Test Drive Library");

… How do we implement the debugger

calls? The solution is to use stacks!