licky79
07-11-2004, 15:02
Dato il seguente codice che crea uno stack con una lista linkata:
public class LinkedStack implements stack{
public LinkedStack(){
top = null;
size = 0;
}
public LinkedStack(Object[] A){
}
public int size(){
return size;
}
public boolean isEmpty(){
if(top==null)
return true;
return false;
}
public Object push(Object elem){
Node v = new Node();
v.setElement(elem);
v.setNext(top);
top = v;
size++;
return elem;
}
public Object top() throws StackEmptyException{
if(isEmpty())
throw new StackEmptyException ("Stack vuoto");
return top.getElement();
}
public Object pop() throws StackEmptyException{
if(isEmpty())
throw new StackEmptyException("Stack vuoto");
Object temp = top.getElement();
top = top.getNext();
size--;
return temp;
}
private Node top;
private int size;
}
devo creare un costruttore che mi prende in input un array di Object e mi inizializzi lo stack con gli elementi dell'array (il primo elemento da inserire nello stack è A[0], il secondo A[1], ...).
Chi mi aiuta a creare questo costruttore???
public LinkedStack(Object[] A){
}
GRAZIE.
public class LinkedStack implements stack{
public LinkedStack(){
top = null;
size = 0;
}
public LinkedStack(Object[] A){
}
public int size(){
return size;
}
public boolean isEmpty(){
if(top==null)
return true;
return false;
}
public Object push(Object elem){
Node v = new Node();
v.setElement(elem);
v.setNext(top);
top = v;
size++;
return elem;
}
public Object top() throws StackEmptyException{
if(isEmpty())
throw new StackEmptyException ("Stack vuoto");
return top.getElement();
}
public Object pop() throws StackEmptyException{
if(isEmpty())
throw new StackEmptyException("Stack vuoto");
Object temp = top.getElement();
top = top.getNext();
size--;
return temp;
}
private Node top;
private int size;
}
devo creare un costruttore che mi prende in input un array di Object e mi inizializzi lo stack con gli elementi dell'array (il primo elemento da inserire nello stack è A[0], il secondo A[1], ...).
Chi mi aiuta a creare questo costruttore???
public LinkedStack(Object[] A){
}
GRAZIE.