Lecture objectives Collections interface Learn about stacks and their methods: push pop peek ...

Preview:

Citation preview

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”

2 The Collections Framework Design

CS340

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

CS3404

The Collection Framework

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

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

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

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

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>

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

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>

12

CS340

Stack Abstract Data Type

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

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)

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

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

17 Stack Applications

Click icon to add picture

CS340

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

CS340

19

Finding Palindromes (cont.)

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();

}

...

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)); }}

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();}

CS340

23

Finding Palindromes (cont.) ...

public boolean isPalindrome() {

return inputString.equalsIgnoreCase(buildReverse());

}

}

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)

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!

CS340

26

Balanced Parentheses (cont.)

CS340

27

Balanced Parentheses (cont.)

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 )[*(( (

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 )[*(( (

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 )[*(( (

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 )[*(

(

( [[(

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 )[*(

(

([

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 )[*(

(

([

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 )[*(

(

([

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

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 )[*(((

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 )[*(((

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

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

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!

Recommended