Ho un altro problema....io ho creato la classe coda:
E poi la classe Stackconcoda, Ho un NullpointerException nel toString di Stackconcoda:
codice:
public class Queue<E>
{
private int size;
private Node<E> front;
private Node<E> rear;
public Queue()
{
front=null;
rear=null;
size=0;
}
public boolean isEmpty(){return(rear==null);}
public int getSize(){return size;}
public E front(){
if(isEmpty()) throw new EmptyQueueException("Coda vuota.");
return front.getElement();
}
public Node<E> NextFront() {
return front.getNext();
}
public E dequeue() throws EmptyQueueException
{
if(isEmpty()) throw new EmptyQueueException("Coda vuoto.");
E object=front.getElement();
front=front.getNext();
size--;
if(size==0)
rear=null;
return object;
}
public void enqueue(E element)
{
Node <E> node=new Node<E>(element, null);
if(isEmpty())
rear=node;
else rear.setNext(node);
rear=node;
size++;
}
}
codice:
public class Stackconcoda<E> implements Stack<E>
{
private int size;
private Queue <E> coda;
private Node <E> top;
public Stackconcoda()
{
top=null;
size=0;
}
public boolean isEmpty(){return(top==null);}
public int getSize(){return size;}
public E top() throws EmptyStackException
{
if(isEmpty()) throw new EmptyStackException("Stack vuoto.");
return top.getElement();
}
public E pop() throws EmptyStackException
{
if(isEmpty()) throw new EmptyStackException("Stack vuoto.");
E object=coda.front();
Queue<E> appoggio=new Queue<E>();
if(coda.getSize()==1) coda=null;
Node<E>node=new Node<E>(coda.front(),coda.NextFront());
while(node.getNext()!=null)
{
appoggio.enqueue(node.getElement());
node=node.getNext();
}
coda=appoggio;
return object;
/*
E object=front.getElement();
front=front.getNext();
size--;
if(size==0)
rear=null;
return object;*/
}
public void push(E element)
{
Node <E> node=new Node<E>(element, top);
if(isEmpty())
top=node;
else top.setNext(node);
top=node;
size++;
}
public String toString()
{
String Stack="[";
Node<E> node;
for(node=new Node<E>(coda.front(),coda.NextFront());node.getNext()!=null; node=node.getNext())
Stack+=node.getElement()+", ";
Stack+=node.getElement()+"]";
return Stack;
}
}
.