|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help with Printing a Stack
Hello, I am relatively new to Java and I am writing a program that uses a stack to print prime factors of a positive integer. I need some help getting the program to output the data in reverse order using the reversePrint method. For example the program output for 500 reads 5 5 5 2 2 and it needs to read 2 2 5 5 5.
I am a bit confused as to where to place this method (which class) and its correct structure. I have 2 classes and will list them below, the first one is where I believe the method should go, and you will see a couple of my attempts at the bottom of this class, commented out of course. (As you see I'm totally lost!) Any help or information would be greatly appreciated, Thanks Ex8 class: public class Ex8 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { int num; int temp; int factor; Integer intObject; LinkedStackClass<Integer> stack = new LinkedStackClass<Integer>(); System.out.print("Enter a positive integer greater than 1: "); num = console.nextInt(); System.out.println(); while (num <= 1) { System.out.print("\nYou must enter a positive " + "integer greater than 1: "); num = console.nextInt(); System.out.println(); } System.out.print("The prime factorization of " + num + ": "); if (isPrime(num)) { System.out.println(num); return; } temp = num; factor = 2; while (temp > 1) if (temp % 2 == 0) { intObject = 2; stack.push(intObject); temp = temp / 2; } else break; factor = 3; while (temp > 1 && (factor <= num / 2)) { if (!isPrime(factor)) factor += 2; else if (temp % factor == 0) { intObject = factor; stack.push(intObject); temp = temp / factor; } else factor += 2; } while (!stack.isEmptyStack()) { System.out.print(stack.peek() + " "); stack.pop(); } System.out.println(); return ; } public static boolean isPrime(int num) { boolean isPrime = true; for (int i = 3; i <= Math.sqrt(num); i += 2) if (num % i == 0) { isPrime = false; break; } if (( num % 2 !=0 && isPrime && num > 2) || num == 2) { return true; } else { return false; } } /* public void reversePrint() { LinkedStackClass<Integer> current; current = stackTop; while (current != null) { System.out.print(current.info + " "); current = current.link; } } public void reversePrint() { LinkedStackClass<Integer> current; current = temp; while (current != null) { System.out.print(current.info + " "); current = current.temp; } } */ } LinkedStackClass: public class LinkedStackClass<T> implements StackADT<T> { private class StackNode<T> { public T info; public StackNode<T> link; public StackNode() { info = null; link = null; } public StackNode(T elem, StackNode<T> ptr) { info = elem; link = ptr; } public String toString() { return info.toString(); } } private StackNode<T> stackTop; public LinkedStackClass() { stackTop = null; } public void initializeStack() { stackTop = null; } public boolean isEmptyStack() { return (stackTop == null); } public boolean isFullStack() { return false; } public void push(T newElement) { StackNode<T> newNode; newNode = new StackNode<T>(newElement, stackTop); stackTop = newNode; } public T peek() throws StackUnderflowException { if (stackTop == null) throw new StackUnderflowException(); return stackTop.info; } public void pop() throws StackUnderflowException { if (stackTop == null) throw new StackUnderflowException(); stackTop = stackTop.link; } } |
|
#2
|
|||
|
|||
|
Not gonna bother to read and try to understand what your code is doing, but basically the elements 2, 2, 5, 5, 5 where pushed into the stack, so when you pop the stack, the first number to come out is going to be the last one in, therefore you get 5 5 5 2 2, right?
If so, there are a couple of ways in which you can print the output in the correct order: 1- Push the numbers into a new stack, then print. 2- Use a StringBuffer and it's reverse method: http://java.sun.com/j2se/1.4.2/docs....html#reverse() So far you have: Code:
while (!stack.isEmptyStack()){
System.out.print(stack.peek() + " ");
stack.pop();
}
Instead you might want something like: Code:
StringBuffer buffer = new StringBuffer();
while (!stack.isEmptyStack()){
buffer.append(stack.pop() + " ") //can't remember right now if this is possible
}
System.out.println(buffer.reverse()); //or something like this.
// It's almost 4 AM so can't think clearly, but I hope you get the idea. Last edited by daniel_g : March 22nd, 2007 at 08:38 PM. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Help with Printing a Stack |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|