lab5soln - while (equal && !stack1.isEmpty()...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
1. See Stack.zip on the daily schedule for the ListStack class 2. public static ListStack<Integer> toListStack(int[] values) { ListStack<Integer> stack = new ListStack<Integer>(); for (int i = 0; i < values.length; i++) { stack.push(values[i]); } return stack; } 3. public static boolean areEqual(LIFOStack<Integer> stack1, LIFOStack<Integer> stack2) { // create stack for the equal elements LIFOStack<Integer> equalStack = new ListStack<Integer>(); boolean equal = true;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: while (equal && !stack1.isEmpty() && !stack2.isEmpty()) { int value1 = stack1.pop(); int value2 = stack2.pop(); if (value1 != value2) { stack1.push(value1); stack2.push(value2); equal = false; } else { equalStack.push(value1); } } equal = equal && stack1.isEmpty() && stack2.isEmpty(); // restore the stacks while (!equalStack.isEmpty()) { int value = equalStack.pop(); stack1.push(value); stack2.push(value); } return equal; }...
View Full Document

This note was uploaded on 12/19/2009 for the course CS 121 taught by Professor Reid-miller during the Spring '09 term at Carnegie Mellon.

Ask a homework question - tutors are online