Help with Stack output -- Please help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Help with Stack output -- Please help

  1. #1
    Join Date
    Mar 2007
    Posts
    1

    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;
    }




    }

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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

  1. Replies: 1
    Last Post: 04-24-2008, 07:03 AM
  2. Replies: 0
    Last Post: 11-28-2005, 01:56 PM
  3. Capture DOS output after CreateProcess
    By davidsc in forum VB Classic
    Replies: 4
    Last Post: 11-03-2005, 09:21 AM
  4. Queue Stack Question
    By trixma in forum Java
    Replies: 4
    Last Post: 08-14-2005, 02:03 PM
  5. Replies: 3
    Last Post: 03-11-2001, 09: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center