1 / 16

CSC 205 – Java Programming II

CSC 205 – Java Programming II. Lecture 30 April 3, 2002. Stack. A stack is an abstract data type (ADT) that contains a sequence of elements and honors the last-in-first-out (LIFO) rule.

shelby
Download Presentation

CSC 205 – Java Programming II

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. CSC 205 – Java Programming II Lecture 30 April 3, 2002

  2. Stack • A stack is an abstract data type (ADT) that contains a sequence of elements and honors the last-in-first-out (LIFO) rule. • Conceptually, the only element that can be removed, accessed, modified is the one which was most recently inserted. • Pop and push are the two basic stack operations

  3. Stack PUSH top POP last-in-first-out

  4. Examples Green Red Red Yellow Red Yellow Green Red Yellow pop pop push green

  5. Java Stack Class • The Java Stack class represents a stack of objects. • It outdated the Java Collections Framework • Available since JDK1.0 • It extends class Vector with 5 additional operations. • With its top at the index size()-1 top

  6. Java Stack API booleanempty()Tests if this stack is empty. Objectpeek()Looks at the object at the top of this stack without removing it from the stack. Objectpop()Removes the object at the top of this stack and returns that object as the value of this function. Objectpush(Object item)Pushes an item onto the top of this stack. intsearch(Object o)Returns the 1-based position where an object is on this stack.

  7. Example • Checking for balanced braces • abc{defg{ijk}{l{mn}}op}qr balanced • abc{def}}ghij{kl}m not balanced • The braces are balanced if • Each time you encounter a }, it matches an already encountered { • When you reach the end of the string, you have matched each {

  8. Pseudocode Solution • A simplified solution in pseudocode while (not at the end of the string) { if (the next character is a ‘{’) { stack.push(‘{’) } else if (the character is a ‘}’) openBrace = stack.pop() } }

  9. Examples • push “}” • push “}” • pop • pop • Stack empty balanced { { { { {a{b}c} { { { { • push “}” • push “}” • pop • Stack not empty not balanced {a{bc} • push “}” • push “}” • pop • pop • Stack empty balanced { {ab}c}

  10. Problems w/ The Stack Class • All the Vector methods can be used • For examples myBookStack.get(7); myBookStack.remove(0); //the element right at the “bottom” can be removed!

  11. Convenient or Risky? myBookStack.get(7); myBookStack.remove(0);

  12. Alternative Designs • Implement a RealStack class • Use a LinkedList object as its only field • Provide push and pop methods public Object push(Object o) { list.add(o); return o; } public Object pop(Object o) { return list.removeLast(o); }

  13. Activation Stack • Stacks are used almost exclusively by computer systems • An activation stack is used by a compiler /interpreter to save the method information when a method is called • The stack is part of the main memory. • Other parts include a heap to hold data

  14. Activation Records • Each activation record includes • The return address • The value of each argument • The values of the method’s other local variables

  15. Decimal To Binary public void processInput(String s) { writeBinary(Integer.parseInt(s)); //RA1 } public void writeBinary(int n) { if (n<=1) throw new IllegalArgumentException(); if (n<=1) gui.println(n); else { writeBinary(n/2); //RA2 gui.println(n%2); } }

  16. Example RA2 1 RA2 RA2 RA2 3 3 3 RA1 RA1 RA1 RA1 RA1 6 6 6 6 6

More Related