Download presentation
Presentation is loading. Please wait.
Published byΝικόλας Σκλαβούνος Modified over 6 years ago
1
COMPUTER 2430 Object Oriented Programming and Data Structures I
3
Quiz 3 1. Evaluate the following expressions a) Postfix Answer: -1
b) Prefix Answer: 29
4
Quiz 3 3. Convert the following to prefix and postfix a) Prefix + A - * B / C D E b) Postfix A B C D / * E - +
5
Quiz 3: As a User public Object removeAndReturnBottomElement( Stack s, int size ) { Stack temp = new Stack(size); // Hint: use another stack! if (s.isEmpty()) return null; while (!s.isEmpty()) temp.push( s.pop() ); Object obj = temp.pop(); while (!temp.isEmpty()) s.push) temp.pop() ); return obj; }
6
!s.isEmpty() public Object removeAndReturnBottomElement( Stack s, int size ) { if (s.isEmpty()) return null; // while (!s.isEmpty()) while (s.isEmpty() == false) temp.push( s.pop() ); Object obj = temp.pop(); while (temp.isEmpty() == false) s.push) temp.pop() ); return obj; }
7
Quiz 3: As a User public Object removeAndReturnBottomElement( Stack s, int size ) { Stack temp = new Stack(size); // Hint: use another stack! if (s.isEmpty()) return null; while (!s.isEmpty()) temp.push( s.pop() ); Object obj = temp.pop(); s = temp; // will it work? // NO! return obj; }
8
Quiz 3: As an Implementer
public class Stack { private Object[] items; private int top; public Object getBottomElement() if (top == 0) return null; Object obj = items[0]; top --; for (int i = 0; i < top; i ++) items[i] = items[i + 1]; return obj; }
9
Quiz 3: As an Implementer
public class Stack { public Object getBottomElement() if (top == 0) return null; Object obj = items[0]; for (int i = 0; i < top - 1; i ++) items[i] = items[i + 1]; top --; return obj; }
10
public class Stack { private Object[] items; private int top = 0; public Stack( int size ) ... public void push( Object obj ) public Object pop() public boolean isEmpty() public boolean isFull() }
11
Example Stack personList = new Stack(100); Person p = new Person(); personList.push( p ); // Valid? // Yes. // public void push( Object obj ) // Can use child when parent is expected. ... p = personList.pop(); // Valid? // NO! // public Object pop() // Cannot use parent when child is expected. p = (Person) personList.pop(); // Must cast!
12
Stack of Person public class PersonStack { private Person[] items; private int top = 0; public Stack( int size ) ... public void push( Person obj ) public Person pop() public boolean isEmpty() public boolean isFull() }
13
Stack of Person public class PersonStack { private Person[] items; ... } PersonStack personList = new PersonStack(100); Person p; p = personList.pop(); // Valid? // Yes! No cast needed. Object obj = new Object(); personList.push( obj ); // Valid? // NO! // Cannot use parent when child is expected.
14
Stack for Each Class public class PersonStack { private Person[] items; ... } public class DateStack private Date[] items; public class TVectorStack private TVector[] items; The code is almost the same!
15
Generic Stack public class Stack<E> { private E[] items; ... }
E represents a class The class Stack can be instantiated with a particular class E is a parameter E must be a class and cannot be a primitive type
16
public class Stack<E>
{ private E[] items; private int top = 0; public Stack( int size ) items = (E[])new Object[size]; // Java Warning possible } public boolean isEmpty() return top == 0; public boolean isFull() return top >= items.length; ...
17
public class Stack<E>
{ private E[] items; private int top = 0; . . . public void push ( E x ) items[top ++] = x; } public E pop() return items[-- top];
18
Example Stack<Person> personStack = new Stack<Person>(100); Stack<Date> dateStack = new Stack<Date>(100); Person p; Date d; ... p = personStack.pop(); d = dateStack.pop(); // No cast needed! d = new Date(); dateStack.push( d ); // Valid? // Yes personStack.push( d ); // Valid? // NO!
19
Generic Stack RPN Evaluation
Prog 4 Generic Stack RPN Evaluation
20
Prog 4 Class TVector Java class Vector 4-dimensional
Example: (-7,21,5,-33) Java class Vector Generic class Methods: size() get(i) add(x)
21
Other Generic Classes Generic Queue More in CS 2630
22
Prog 3 & Prog 4
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.