Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package FOCS;
- /**
- *
- * @author jxe953
- */
- class IntStack // Alternative implementation 2.
- {
- private int[] stack;
- private int sp;
- public IntStack(int maximumCapacity) {
- // A stack with zero maximum capacity is allowed.
- // But the maximum capacity cannot be negative.
- assert (maximumCapacity >= 0);
- // Allocate an array for the stack.
- stack = new int[maximumCapacity];
- // Initialize stack pointer.
- sp = stack.length - 1;
- }
- public void push(int element) {
- // We refuse to overflow the stack.
- assert (sp > 0);
- stack[sp] = element;
- sp = sp - 1;
- }
- public int pop() {
- // We refuse to pop from an empty stack.
- assert (sp + 1 < stack.length);
- sp = sp + 1;
- return (stack[sp]);
- }
- public boolean isEmpty() {
- return (sp == stack.length - 1);
- }
- public boolean isFull() {
- return (sp == 0);
- }
- public static void main(String[] args) {
- IntStack s = new IntStack(25);
- int[] rpn = {6, 5, -1, 4, -2, 3, -1, 2, -2, 1, -1};
- int x = 0;
- int y = 0;
- int i;
- //System.out.println(rpn[0]);
- for (i = 0; i < rpn.length; i++) {
- System.out.println(rpn [i]);
- if (rpn[i] >= 0) {
- // System.out.println(rpn[i]);
- s.push(rpn[i]);
- } else if (rpn[i] == -1) {
- while (s.isEmpty() == false) {
- x = s.pop() + x;
- //System.out.println(x);
- s.push(x);
- }
- } else if (rpn[i] == -2) {
- while (s.isEmpty() == false) {
- x = s.pop() * x;
- System.out.println(x);
- s.push(x);
- }
- }
- }
- System.out.println(x);
- }
- }
Add Comment
Please, Sign In to add comment