Download presentation
Presentation is loading. Please wait.
Published byAndra Fox Modified over 9 years ago
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” CS340 1
2
The Collections Framework Design CS340 2
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 CS340 3
4
The Collection Framework 4 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 CS340 5
6
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 CS340 6
7
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 7
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 8
9
Implementing a Subclass of Collection Extend AbstractCollection, which implements most operations You need to implement only: add(E) size() iterator() An inner class that implements Iterator CS340 9
10
Implementing a Subclass of List Extend AbstractList You need to implement only: add(int, E) get(int) remove(int) set(int, E) size() AbstractList implements Iterator using the index CS340 10
11
AbstractCollection, AbstractList, and AbstractSequentialList Another more complete way to declare CS340ArrayList is: public class CS340ArrayList extends AbstractList implements List Another more complete, way to declare CS340LinkedList is: public class CS340LinkedList extends AbstractSequentialList implements List CS340 11
12
Stack Abstract Data Type CS340 12
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 13
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 14
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 15
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 CS340 16
17
Stack Applications CS340 17
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 18
19
Finding Palindromes (cont.) CS340 19
20
Finding Palindromes (cont.) import java.util.*; public class PalindromeFinder { private String inputString; private Stack charStack = new Stack (); public PalindromeFinder(String str) { inputString = str; fillStack(); }... CS340 20
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 ay ayakk k y a k ayakk private void fillStack() { for(int i = 0; i < inputString.length(); i++) { charStack.push(inputString.charAt(i)); } CS340 21
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 kayak k private String buildReverse(){ StringBuilder result = new StringBuilder(); while(!charStack.empty()) { result.append(charStack.pop()); } return result.toString(); } CS340 22
23
Finding Palindromes (cont.)... public boolean isPalindrome() { return inputString.equalsIgnoreCase(buildReverse()); } CS340 23
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 24
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 25
26
Balanced Parentheses (cont.) CS340 26
27
Balanced Parentheses (cont.) CS340 27
28
Balanced Parentheses (cont.) Expression: balanced : true index : 0 (w * [x + y] / z) 145678910320 wx+y]/z)[*((( CS340 28
29
Balanced Parentheses (cont.) Expression: balanced : true index : 1 (w * [x + y] / z) 145678910320 wx+y]/z)[*((( CS340 29
30
Balanced Parentheses (cont.) Expression: balanced : true index : 2 (w * [x + y] / z) 145678910320 wx+y]/z)[*((( CS340 30
31
Balanced Parentheses (cont.) Expression: balanced : true index : 3 (w * [x + y] / z) 145678910320 wx+y]/z)[*( ( ( [ [( CS340 31
32
Balanced Parentheses (cont.) Expression: balanced : true index : 4 (w * [x + y] / z) 145678910320 wx+y]/z)[*( ( ([ CS340 32
33
Balanced Parentheses (cont.) Expression: balanced : true index : 5 (w * [x + y] / z) 145678910320 wx+y]/z)[*( ( ([ CS340 33
34
Balanced Parentheses (cont.) Expression: balanced : true index : 6 (w * [x + y] / z) 145678910320 wx+y]/z)[*( ( ([ CS340 34
35
Balanced Parentheses (cont.) Expression: balanced : true index : 7 (w * [x + y] / z) 145678910320 wx+y]/z)[*( ( ([( Matches! Balanced still true CS340 35
36
Balanced Parentheses (cont.) Expression: balanced : true index : 8 (w * [x + y] / z) 145678910320 wx+y]/z)[*((( CS340 36
37
Balanced Parentheses (cont.) Expression: balanced : true index : 9 (w * [x + y] / z) 145678910320 wx+y]/z)[*((( CS340 37
38
Balanced Parentheses (cont.) Expression: balanced : true index : 10 (w * [x + y] / z) 145678910320 wx+y]/z)[*((( Matches! Balanced still true CS340 38
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 39
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! CS340 40
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.