Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using...
-
Upload
barry-rose -
Category
Documents
-
view
226 -
download
0
Transcript of Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using...
![Page 1: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/1.jpg)
CS340 1
Lecture Objectives
• To understand how Java implements a stack• To learn how to implement a stack using an underlying array or linked list
• Implement a simple calculator
![Page 2: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/2.jpg)
2
Implementing a Stack
Click icon to add picture
CS340
![Page 3: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/3.jpg)
CS340 3
A little about information hiding• Principle:
• Hide internal details of a component from other components
• Why?• Prevent damage from wrong external code• Make components easier to understand/use• Simplify modification and repair• Facilitate re-use
![Page 4: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/4.jpg)
CS340 4
Implementing a Stack with a List Component ListStack: has a List component We can use ArrayList, Vector, or the LinkedList
classes to implement the List interface. push method:
public E push(E obj) { theData.add(obj); return obj;}
Adapter class: stack in this case is adapter class of List
Method delegation: from stack to list
![Page 5: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/5.jpg)
Implementing a Stack with a List Component (cont.)public class ListStack<E> implements Stack<E>{ private List<E> theData; public ListStack( ) { theData = new ArrayList<E>(); } @Override public E push( E obj ) { theData.add(obj);
return obj; }
CS340 5
![Page 6: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/6.jpg)
6
Implementing a Stack with a List Component (cont.)
@Override public E pop( ) { if( empty( ) ) throw new EmptyStackException( "ListStack pop" ); return theData.remove(theData.size()-1); } @Override public E peek( ) { if( empty( ) ) throw new EmptyStackException( "ListStack top" ); return theData.get(theData.size()-1); }
![Page 7: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/7.jpg)
Implementing a Stack with a List Component (cont.)@Override public boolean empty( ) { return(theData.size() == 0); }
CS340 7
![Page 8: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/8.jpg)
CS340 8
Implementing a Stack Using an Array
If we implement a stack as an array, we would need . . .
public class ArrayStack<E> implements StackInt<E> { private E[] theData; int topOfStack = -1; private static final int INITIAL_CAPACITY = 10;
@SupressWarnings("unchecked") public ArrayStack() { theData = (E[])new Object[INITIAL_CAPACITY]; }
Allocate storage for an array with a default
capacity
Keep track of the top of the stack
We do not need a size variable or method
![Page 9: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/9.jpg)
9
Implementing a Stack Using an Array (cont.)
ArrayStack
theData = topOfStack = -1
Object[]
[0] = null[1] = null[2] = null[3] = null[4] = null[5] = null[6] = null[7] = null[8] = null[9] = null
public E push(E obj) { if (topOfStack == theData.length - 1){ reallocate(); } topOfStack++; theData[topOfStack] = obj; return obj;}
0
Character
value = 'J'
1
Character
value = 'a'
Character
value = 'v'
2
Character
value = 'a'
3
![Page 10: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/10.jpg)
CS340 10
Implementing a Stack Using an Array (cont.)
@Overridepublic E pop() { if (empty()) { throw new EmptyStackException(); } return theData[topOfStack--];}
This implementation is O(1)
![Page 11: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/11.jpg)
11
Implementing a Stack as a Linked Data Structure• We can also implement a stack using a linked list of
nodes
It is easiest to insert and delete from the head of a
list
push inserts a node at the head and pop deletes the
node at the head
when the list is empty, pop returns null
![Page 12: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/12.jpg)
Implementing a Stack as a Linked Data Structure (cont.)public class LinkedStack<E> implements Stack<E>{// Data fieldsPrivate Node<E> topOfStackRef = null;// Methods: push, pop, peek, empty
}
CS340 12
![Page 13: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/13.jpg)
CS340 13
Comparison of Stack Implementations
Extending a Vector: poor choice for stack implementation
The easiest implementation uses a List component (ArrayList is the simplest) for storing data Array requires reallocation of space when the array becomes full,
and Linked data structure requires allocating storage for links
All insertions and deletions occur at one end: constant time, O(1)
![Page 14: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/14.jpg)
14
Additional Stack Applications
Click icon to add picture
CS340
![Page 15: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/15.jpg)
15
Additional Stack Applications Postfix and infix notation
Expressions normally are written in infix form, but it easier to evaluate an expression in postfix form since there is
no need to group sub-expressions in parentheses or worry about operator precedence
![Page 16: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/16.jpg)
CS340 16
Evaluating Postfix Expressions• Write a class that evaluates a postfix expression• Use the space character as a delimiter between tokens
![Page 17: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/17.jpg)
CS340 17
Evaluating Postfix Expressions (cont.)
1. create an empty stack of integers
2. while there are more tokens
3. get the next token
4. if the first character of the token is a digit
5. push the character on the stack
6. else if the token is an operator
7. pop the right operand off the stack
8. pop the left operand off the stack
9. evaluate the operation
10. push the result onto the stack
11. pop the stack and return the result
7 -20*4
44
![Page 18: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/18.jpg)
CS340 18
Evaluating Postfix Expressions (cont.)
1. create an empty stack of integers
2. while there are more tokens
3. get the next token
4. if the first character of the token is a digit
5. push the character on the stack
6. else if the token is an operator
7. pop the right operand off the stack
8. pop the left operand off the stack
9. evaluate the operation
10. push the result onto the stack
11. pop the stack and return the result
7 -20*4
44 77
4
![Page 19: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/19.jpg)
CS340 19
Evaluating Postfix Expressions (cont.)
1. create an empty stack of integers
2. while there are more tokens
3. get the next token
4. if the first character of the token is a digit
5. push the character on the stack
6. else if the token is an operator
7. pop the right operand off the stack
8. pop the left operand off the stack
9. evaluate the operation
10. push the result onto the stack
11. pop the stack and return the result
7 -20*44 77
4
4 * 7
![Page 20: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/20.jpg)
CS340 20
Evaluating Postfix Expressions (cont.)
1. create an empty stack of integers
2. while there are more tokens
3. get the next token
4. if the first character of the token is a digit
5. push the character on the stack
6. else if the token is an operator
7. pop the right operand off the stack
8. pop the left operand off the stack
9. evaluate the operation
10. push the result onto the stack
11. pop the stack and return the result
7 -20*44 72828
![Page 21: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/21.jpg)
CS340 21
Evaluating Postfix Expressions (cont.)
1. create an empty stack of integers
2. while there are more tokens
3. get the next token
4. if the first character of the token is a digit
5. push the character on the stack
6. else if the token is an operator
7. pop the right operand off the stack
8. pop the left operand off the stack
9. evaluate the operation
10. push the result onto the stack
11. pop the stack and return the result
7 -20*44 728
2020
28
![Page 22: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/22.jpg)
CS340 22
Evaluating Postfix Expressions (cont.)
1. create an empty stack of integers
2. while there are more tokens
3. get the next token
4. if the first character of the token is a digit
5. push the character on the stack
6. else if the token is an operator
7. pop the right operand off the stack
8. pop the left operand off the stack
9. evaluate the operation
10. push the result onto the stack
11. pop the stack and return the result
7 -20*44 720
28
28 - 20
![Page 23: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/23.jpg)
CS340 23
Evaluating Postfix Expressions (cont.)
1. create an empty stack of integers
2. while there are more tokens
3. get the next token
4. if the first character of the token is a digit
5. push the character on the stack
6. else if the token is an operator
7. pop the right operand off the stack
8. pop the left operand off the stack
9. evaluate the operation
10. push the result onto the stack
11. pop the stack and return the result
7 -20*44 788
![Page 24: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/24.jpg)
CS340 24
Evaluating Postfix Expressions (cont.)
1. create an empty stack of integers
2. while there are more tokens
3. get the next token
4. if the first character of the token is a digit
5. push the number on the stack
6. else if the token is an operator
7. pop the right operand off the stack
8. pop the left operand off the stack
9. evaluate the operation
10. push the result onto the stack
11. pop the stack and return the result
7 -20*44 78
![Page 25: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/25.jpg)
Evaluating Postfix Expressions (cont.)
• Listing 3.6 (PostfixEvaluator.java, pages 173 - 175)
CS340 25
![Page 26: Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.](https://reader036.fdocuments.net/reader036/viewer/2022062516/56649e4c5503460f94b413da/html5/thumbnails/26.jpg)
CS340 26
Evaluating Postfix Expressions (cont.)
Testing: write a driver which creates a PostfixEvaluator object reads one or more expressions and report the result catches PostfixEvaluator.SyntaxErrorException exercises each path by using each operator exercises each path through the method by trying different
orderings and multiple occurrences of operators tests for syntax errors:
an operator without any operandsa single operandan extra operandan extra operatora variable namethe empty string