michelexeno
20-12-2013, 20:46
Ciao a tutti sto progettando il dato astratto coda doppia in java, (consente di inserire, cancellare, esaminare elementi in entrambe le estremità) implementandolo con una lista doppiamente collegata. Ci sto impiegando troppo tempo e mi sto confondendo parecchio..mi servirebbe una mano, grazie.
package coda;
public class CodaDoppiaCollegata implements CodaDoppia {
private Record inizio = null; //Punta all'elemento in testa
private Record fine = null; ////Punta all'elemento in coda
private class Record {
public Object elem;
public Record next; //Punta all'elemento successivo
public Record prev; //Punta all'elemento precedente
public Record(Object e) {
elem = e;
next = prev = null;
}
}
@Override
/**
* Inserisce un elemento in testa.
*/
public void enqueueStart(Object e) {
Record p = new Record(e);
if(isEmpty()) {
inizio = fine = p;
} else {
inizio.next = new Record(e);
inizio = inizio.next;
}
}
@Override
/**
* Inserisce un elemento in coda.
*/
public void enqueueEnd(Object e) {
Record p = new Record(e);
if(isEmpty()) {
inizio = fine = p;
} else {
fine.next = p;
fine = fine.next;
}
}
@Override
/**
* Rimuove l'elemento in testa.
*/
public void dequeueStart() {
if(isEmpty())
throw new EccezioneStrutturaVuota("CODA VUOTA!");
inizio = inizio.next;
}
@Override
/**
* Rimuove l'elemento in coda.
*/
public void dequeueEnd() {
if(isEmpty())
throw new EccezioneStrutturaVuota("CODA VUOTA!");
fine = fine.next;
}
@Override
/**
* Verifica se la coda e' vuota.
*/
public boolean isEmpty() {
return (inizio == null) && (fine == null);
}
@Override
/**
* Restituisce l'elemento in testa.
*/
public Object first() {
if(isEmpty())
throw new EccezioneStrutturaVuota("CODA VUOTA!");
return inizio.elem;
}
@Override
/**
* Restituisce l'elemento in coda.
*/
public Object last() {
if(isEmpty())
throw new EccezioneStrutturaVuota("CODA VUOTA!");
return fine.elem;
}
private static void inizializza(CodaDoppia doubleQueue) {
doubleQueue.enqueueStart("secondo");
doubleQueue.enqueueStart("primo");
doubleQueue.enqueueEnd("terzo");
doubleQueue.enqueueEnd("quarto");
}
public static void main(String args[]) {
CodaDoppia test = new CodaDoppiaCollegata();
inizializza(test);
System.out.println(test.first());
System.out.println(test.last());
test.dequeueStart();
System.out.println(test.first());
test.dequeueEnd();
System.out.println(test.last());
}
}
package coda;
public class CodaDoppiaCollegata implements CodaDoppia {
private Record inizio = null; //Punta all'elemento in testa
private Record fine = null; ////Punta all'elemento in coda
private class Record {
public Object elem;
public Record next; //Punta all'elemento successivo
public Record prev; //Punta all'elemento precedente
public Record(Object e) {
elem = e;
next = prev = null;
}
}
@Override
/**
* Inserisce un elemento in testa.
*/
public void enqueueStart(Object e) {
Record p = new Record(e);
if(isEmpty()) {
inizio = fine = p;
} else {
inizio.next = new Record(e);
inizio = inizio.next;
}
}
@Override
/**
* Inserisce un elemento in coda.
*/
public void enqueueEnd(Object e) {
Record p = new Record(e);
if(isEmpty()) {
inizio = fine = p;
} else {
fine.next = p;
fine = fine.next;
}
}
@Override
/**
* Rimuove l'elemento in testa.
*/
public void dequeueStart() {
if(isEmpty())
throw new EccezioneStrutturaVuota("CODA VUOTA!");
inizio = inizio.next;
}
@Override
/**
* Rimuove l'elemento in coda.
*/
public void dequeueEnd() {
if(isEmpty())
throw new EccezioneStrutturaVuota("CODA VUOTA!");
fine = fine.next;
}
@Override
/**
* Verifica se la coda e' vuota.
*/
public boolean isEmpty() {
return (inizio == null) && (fine == null);
}
@Override
/**
* Restituisce l'elemento in testa.
*/
public Object first() {
if(isEmpty())
throw new EccezioneStrutturaVuota("CODA VUOTA!");
return inizio.elem;
}
@Override
/**
* Restituisce l'elemento in coda.
*/
public Object last() {
if(isEmpty())
throw new EccezioneStrutturaVuota("CODA VUOTA!");
return fine.elem;
}
private static void inizializza(CodaDoppia doubleQueue) {
doubleQueue.enqueueStart("secondo");
doubleQueue.enqueueStart("primo");
doubleQueue.enqueueEnd("terzo");
doubleQueue.enqueueEnd("quarto");
}
public static void main(String args[]) {
CodaDoppia test = new CodaDoppiaCollegata();
inizializza(test);
System.out.println(test.first());
System.out.println(test.last());
test.dequeueStart();
System.out.println(test.first());
test.dequeueEnd();
System.out.println(test.last());
}
}