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

    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;
    while(ans=='y'){
    a=Integer.parseInt(JOptionPane.showInputDialog("En ter choice:"));

    if(a==1){


    String ob=(JOptionPane.showInputDialog("Enter the element"));
    list.push(ob);
    System.out.print(list);
    }
    if(a==3){

    list.isEmpty();
    System.out.println(list);
    }

    System.out.println(list);

    b=(JOptionPane.showInputDialog("do you want to continue?"));
    ans=b.charAt(0);
    }
    }
    }

    Thank you very much..

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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-19-2008, 11:58 PM
  2. Replies: 1
    Last Post: 11-17-2008, 09:32 AM
  3. Stack not popping
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 04:59 PM
  4. Stack Trace
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-10-2007, 05: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
  •