230 likes | 241 Views
Learn how to implement a stack in Java using object-oriented programming and data structures. Explore operations like push, pop, peek, isEmpty, and isFull. Understand passing object references and the differences between user and implementer perspectives.
E N D
COMPUTER 2430Object Oriented Programming andData Structures I
public class Stack { private Object[] items; private int top; public Stack (int size) . . . public void push ( Object obj ) . . . public Object pop() . . . public boolean isEmpty() . . . public boolean isFull() . . . }
Additional Operations? • Peek? • As an implementer • As a user
User Method peek • Given a Stack with the methods isEmpty isFull push pop • How can a user have a look at the element before calling pop? Can NOT access private data! User peek method
// User method outside Stack class public Object peek ( Stack s ); Stack myStack = new Stack( 1000 ); ... Object x = peek( myStack ); if (x == null) . . . else if (x instanceof FixedPoint) . . . else if (x instanceof Golfer) . . . else . . .
public Object peek ( Stack s ) { if ( s.isEmpty() ) return null; // Don’t change the stack Object obj = s.pop(); s.push( obj ); // peek the stack return obj; }
Passing Object Reference public Object peek ( Stack s ); // Pass by value or pass by reference? • Java has only passing by value • No passing reference • Each class variable is a reference • The value of s is the address of a Stack object
Passing Object Reference Stack myStack; myStack = new Stack( 1000 ); ... Object x = peek( myStack ); // public Object peek ( Stack s ) // passing by value myStack Activation record return point s
Passing Object Reference Stack myStack; . . . Object x = peek( myStack ); • Can function peek change the object referenced by myStack? Yes! myStack stores the reference to the stack object. All objects are passed by reference in Java! • Can function peek modify the value of myStack to reference a different stack? No! Passing by value! myStack Activation record return point s
Peek by a User public Object peek ( Stack s ) { if ( s.isEmpty() ) return null; Object x = s.pop(); s.push( x ); return x; } It works. Has to pop and push! User cannot access private data.
User versus Implementer • Implementer can provide a peek method • More efficient • Implementer has access to all private data • Upgrade
public class Stack { private Object[] items; private int top = 0; ... public Object peek() { if ( top == 0 ) return null; return items[top - 1]; // Do not modify the stack (top). // return items[top --]; // top is modified! // top = top – 1; } }
What can you do with a Stack? As Implementer • Get count of items • Search • Insert item at any position • Remove item from any position • … As User Call the public methods!
Binary Arithmetic Operations • Conversion programs Infix Prefix, Postfix Prefix Infix, Postfix Postfix Infix, Prefix • Evaluation programs Using operator stack Using operand stack Using both • Easiest program Evaluate postfix expressions Operand stack
Evaluating RPN Using Stack • 4 / 5 1 3 + * - ------------ ------------ 3 5 4 * - ------------------------ 3 20 - -17 • Process tokens in order • If the token is an operand Push it • If the token is an operator Pop two operands Perform the operation Push the result • Stop if invalid at any time
Evaluating RPN 5 7 4 6 + - 8 3 / * 9 * + 7 4 6 + - 8 3 / * 9 * + 4 6 + - 8 3 / * 9 * + 6 + - 8 3 / * 9 * + + - 8 3 / * 9 * + • 8 3 / * 9 * + 8 3 / * 9 * + / * 9 * + * 9 * + 9 * + * + + -49 4 + 6 7 - 10 8 / 3 -3 * 2 -6 * 9 5 + (-54) Stack: LIFO Just what we need here!
Initialize an operand stack, s Set valid true While valid and more tokens to process read token if token is operand s.push(token) else if token is operator if s empty then valid false else op2 s.pop() if s empty then valid false else op1 s.pop() if divide by 0 then valid false else s.push( op1 <operator> op2 ) else valid false If s empty then valid false Else Answer s.pop() if s not empty then valid false
Invalid Expression • 5 + Second pop fails – check for empty before calling pop • + First pop fails – check for empty before before calling pop • 4 0 / Divide by 0 • 3 4 Stack not empty at the end – check for empty at the end • 3 4 B Bad token • Empty expression Popping answer fails – check for empty at the end
Invalid Expression What about an expression consisting of a single operand? 243 It is valid! An expression can be put on the right hand side of an assignment. X = 243; // valid! X = ; // Invalid!
Quiz 3 Monday, October 22
Lab 6 • Can assume the commands you need • Specify input and the corresponding output • No assert!
Lab 6 Submission • UserName_Lab6.doc(x) • To make corrections, UserName_Lab6_2.doc(x) Only second submission accepted • K:\Courses\CSSE\yangq\cs2430\1Dropbox