-
Help with Stack output -- Please help
Hello, I am writing a program that uses a stack to output the prime factors of an integer input by the user. My problem is I need to reverse the output using the reversePrint method. So instead of the program reading, The prime numbers 0f 500: 5 5 5 2 2, I need it to read 500: 2 2 5 5 5. (The user input is 500)
I have the reversePrint method for a Linked List Node, but have no idea how to implement it in regards to my stack output, here is the method:
public void reversePrint()
{
LinkedListNode<T> current;
current = last;
while (current != null)
{
System.out.print(current.info + " ");
current = current.back;
}
}
I have 2 classes that I am using for this program and have no idea where to put this method or how to get it to work. Here are my classes, the first is where I attempted to put the reversePrint method, at the bottom, commented out.(it's really not that long I just spaced it out to read)
Could someone please help me, it would be greatly appreciated.
1st class:
import java.util.*;
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 isPrimeNum;
int sqrtNum;
int divisor = 3;
if (num == 2)
isPrimeNum = true;
else
if (num % 2 == 0)
isPrimeNum = false;
else
{
isPrimeNum = true;
sqrtNum = (int) (Math.pow(num, 0.5));
while (divisor <= sqrtNum)
{
if (num % divisor == 0)
{
isPrimeNum = false;
break;
}
else
divisor = divisor + 2;
}
}
return isPrimeNum;
}
/*
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;
}
}
*/
}
2nd class:
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;
}
}
-
if you truly are using only stack calls, invoke a second stack, pop elements off the top, first and push them onto the second, print out from the second stack.
if you're "fudging", pop off the end of the stack rather than the top.
Similar Threads
-
By Darkrider in forum C++
Replies: 1
Last Post: 04-24-2008, 06:03 AM
-
By Darkrider in forum Database
Replies: 0
Last Post: 11-28-2005, 12:56 PM
-
By davidsc in forum VB Classic
Replies: 4
Last Post: 11-03-2005, 08:21 AM
-
Replies: 4
Last Post: 08-14-2005, 01:03 PM
-
Replies: 3
Last Post: 03-11-2001, 08:04 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks