Results 1 to 2 of 2
Thread: Stack problem..pls help
- 02-16-2009, 02:41 AM #1
Member
- Join Date
- Dec 2008
- Location
- Davao Oriental
- Posts
- 29
- Rep Power
- 0
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..
- 02-16-2009, 09:10 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
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
-
Problem in Calculator implementation using Stack
By realahmed8 in forum New To JavaReplies: 1Last Post: 12-20-2008, 12:58 AM -
Stack class/problem in stackSize Initialization and usage of pop() function
By Mazharul in forum New To JavaReplies: 1Last Post: 11-17-2008, 10:32 AM -
Stack not popping
By bugger in forum New To JavaReplies: 2Last Post: 01-28-2008, 05:59 PM -
Stack Trace
By Java Tip in forum Java TipReplies: 0Last Post: 12-10-2007, 06:29 PM
Bookmarks