Results 1 to 2 of 2
  1. #1
    Mika is offline Member
    Join Date
    Dec 2008
    Davao Oriental
    Rep Power

    Exclamation Stack problem..pls help

    Hi Everyone
    I have problem regarding my Stack. my code isnt working correctly.I dont know what to do. Can you help me??pls..This code is just like in list, I want to push and element and pop and element if necessary but before i pop something ,it will check if the node is empty and if empty it will display empty otherwise it will pop.

    Hers the code:
    /** Stack implements a stack as a linked list */
    public class Stack
    private Cell top; // marks the topmost Cell of the stack

    /** Constructor Stack creates an empty stack */
    public Stack()
    { top = null; }

    /** push inserts a new element onto the stack
    * @param ob - the element to be added */
    public void push(Object ob)
    { top = new Cell(ob, top); }

    /** pop removes the most recently added element
    * @return the element removed from the stack
    * @exception RuntimeException if stack is empty */
    public Object pop()
    { if ( top == null )
    { throw new RuntimeException("Stack error: stack empty"); }
    Object answer = top.getVal();
    top = top.getNext();
    return answer;

    /** top returns the identity of the most recently added element
    * @return the element
    * @exception RuntimeException if stack is empty */
    public Object top()
    { if ( top == null )
    { throw new RuntimeException("Stack error: stack empty"); }
    return top.getVal();

    /** isEmpty states whether the stack has 0 elements.
    * @return whether the stack has no elements */
    public boolean isEmpty()
    { return (top == null); }
    private class Cell
    // reference to the next node in the chain,
    // or null if there isn't one.
    Cell next;
    // data carried by this node.
    // could be of any type you need.
    Object ob;

    // Node constructor
    public Cell(Object ob)
    next = null;
    ob = ob;

    I have my main method here, it will ask the user to add element or pop an element. we using "import javax.swing.JOptionPane;"

    heres the code:
    import javax.swing.JOptionPane;
    public class listNode{

    public static void main(String[]args){
    System.out.println("Press 1 to push");
    System.out.println("Press 2 to pop ");
    System.out.println("Press 3 to know if empty");
    int a;
    String b;
    char ans='y';

    Stack list=new Stack();
    String data;
    Object next;
    a=Integer.parseInt(JOptionPane.showInputDialog("En ter choice:"));


    String ob=(JOptionPane.showInputDialog("Enter the element"));



    b=(JOptionPane.showInputDialog("do you want to continue?"));

    Thank you very much..

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    So, what's the problem? Compiler messages? Runtime Errors? Doing something it shouldn't? Not doing something it should?

    I'm sorry, but we are not going to play human compiler and JVM to try and figure out what, exactly, is going wrong. We need you to tell us.

    Also, please post your code again, in code tags this time, so that formatting is correctly preserved.

Similar Threads

  1. Problem in Calculator implementation using Stack
    By realahmed8 in forum New To Java
    Replies: 1
    Last Post: 12-20-2008, 12:58 AM
  2. Replies: 1
    Last Post: 11-17-2008, 10:32 AM
  3. Stack not popping
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 05:59 PM
  4. Stack Trace
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-10-2007, 06:29 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