before anything else, i have a question. How can you have a multiple stack in one vector using node/linked list? i don't get the concept of linked list yet. Anyways, this is my problem:
Create a Java class definition of a multiple-stack in one dimensional vector. Implement the basic operations on stack (push, pop, etc) to make them applicable on multiple-stack.
i have this so far:
import javax.swing.*;
public class MStack implements Stack{
private Node top;
private int numElements = 0;
class Node{
Object info;
Node link;
}
public int size(){
return (numElements);
}
public boolean isEmpty(){
return (top == null);
}
public Object top(){
if (isEmpty()) throw new StackException("Stack empty.");
return top.info;
}
public Object pop(){
Node temp;
if (isEmpty())throw new StackException("Stack underflow.");
temp = top;
top = top.link;
return temp.info;
}
public void push(Object item){
Node newNode = new Node();
newNode.info = item;
newNode.link = top;
top = newNode;
}
public static void main ( String [] args){
new MStack();
}
public MStack(){
String item= JOptionPane.showInputDialog("hi:");
push(item);
JOptionPane.showMessageDialog(null, top);
}
}
public interface Stack {
public int size();
public boolean isEmpty();
public Object top() throws StackException;
public Object pop() throws StackException;
public void push(Object item) throws StackException;
class StackException extends RuntimeException{
public StackException(String err){
super(err);
}
}
}
i don't know if i did a multiple stack.. please help? thanks