1 / 26

Lecture Objectives

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. Implementing a Stack. A little about information hiding. Principle:

santa
Download Presentation

Lecture Objectives

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS340 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

  2. CS340 Implementing a Stack

  3. CS340 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

  4. CS340 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

  5. CS340 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( Eobj) { theData.add(obj); return obj; }

  6. Implementing a Stack with a List Component (cont.) @Override public Epop( ) { 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); }

  7. CS340 Implementing a Stack with a List Component (cont.) @Override public boolean empty( ) { return(theData.size() == 0); }

  8. CS340 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; inttopOfStack = -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

  9. Implementing a Stack Using an Array (cont.) Character Character Character Character value = 'a' value = 'J' value = 'v' value = 'a' ArrayStack Object[] theData = topOfStack = -1 [0] = null [1] = null [2] = null [3] = null [4] = null [5] = null [6] = null [7] = null [8] = null [9] = null 3 0 2 1 public E push(E obj) { if (topOfStack == theData.length - 1){ reallocate(); } topOfStack++; theData[topOfStack] = obj; return obj; }

  10. CS340 Implementing a Stack Using an Array (cont.) @Override public E pop() { if (empty()) { throw new EmptyStackException(); } return theData[topOfStack--]; } • This implementation is O(1)

  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

  12. CS340 Implementing a Stack as a Linked Data Structure (cont.) public class LinkedStack<E> implements Stack<E>{ // Data fields Private Node<E> topOfStackRef = null; // Methods: push, pop, peek, empty }

  13. CS340 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)

  14. CS340 Additional Stack Applications

  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

  16. CS340 Evaluating Postfix Expressions • Write a class that evaluates a postfix expression • Use the space character as a delimiter between tokens

  17. CS340 Evaluating Postfix Expressions (cont.) 4 4 7 * 20 - 4 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

  18. CS340 Evaluating Postfix Expressions (cont.) 4 4 7 7 * 20 - 4 7 4 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

  19. CS340 Evaluating Postfix Expressions (cont.) 4 * 7 4 4 7 7 * 20 - 7 4 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

  20. CS340 Evaluating Postfix Expressions (cont.) 28 4 4 7 7 * 20 - 28 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

  21. CS340 Evaluating Postfix Expressions (cont.) 4 4 7 7 * 20 20 - 28 20 28 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

  22. CS340 Evaluating Postfix Expressions (cont.) 28 - 20 4 4 7 7 * 20 - 20 28 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

  23. CS340 Evaluating Postfix Expressions (cont.) 8 4 4 7 7 * 20 - 8 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

  24. CS340 Evaluating Postfix Expressions (cont.) 4 4 7 7 * 20 - 8 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

  25. CS340 Evaluating Postfix Expressions (cont.) • Listing 3.6 (PostfixEvaluator.java, pages 173 - 175)

  26. CS340 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 operands • a single operand • an extra operand • an extra operator • a variable name • the empty string

More Related