• Non ci sono risultati.

Lista <Elemento&gt

N/A
N/A
Protected

Academic year: 2021

Condividi "Lista <Elemento&gt"

Copied!
2
0
0

Testo completo

(1)

Generics

Java 5 introduced generic (class) types.

This means that it is possible to define parametric (class) types.

An algorithm can be defined once, parametrically on the (class) type of data to which can be applied.

class Lista <Elemento>{

/** Rappresentazione del TDA lista semplice generico */

private Elemento elem;

Lista <Elemento> next;

public Lista (){next = null; } // costruttore public void elimina()

{if (next!=null) {next = next.next;} // recupero memoria (automatico) }

public Elemento leggi(){

if (next!=null) return next.elem;

else return(null);}

public void inserisci(Elemento x) // inserisce in testa {Lista<Elemento> p = new Lista<Elemento>();

p.elem = x;

p.next = next;

next = p;

}

public void stampa() //stampa l'intera lista { Lista<Elemento> temp = next;

while (temp!=null) {System.out.println("Elem. = " + temp.elem);

temp = temp.next;}

} }

(2)

public class ListaGenerica {

public static void main(String args[]) { // programma di prova

Lista<String> q = new Lista<String>();

String x="1";

q.inserisci(x);

q.inserisci("2");

q.inserisci("4");

q.stampa();

q.elimina();

q.stampa();

Lista<Integer> r = new Lista<Integer>();

int y=1;

r.inserisci(y);

r.inserisci(2);

r.inserisci(4);

r.stampa();

r.elimina();

r.stampa();

} }

ListaGenerica.main({ });

4

Elem. = 4 Elem. = 2 Elem. = 1 Elem. = 2 Elem. = 1 Elem. = 4 Elem. = 2 Elem. = 1 Elem. = 2 Elem. = 1

Riferimenti

Documenti correlati

Experimental tests show that the eigenvalues of P are all grouped in a circle with center in the origin and radius about 0.3 except one which is near 1.. Moreover, they show that

õôõôõôõôõô õôõô õô õô

[r]

BOOL LockFileEx ( HANDLE hFile, DWORD dwFlags, DWORD dwReserved,. DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh,

[r]

The relation between the pressure drop and flow of a uniform segment of blood vessel is given by Womersley’s oscillatory flow theory and called longitudinal impedance [3]..

the Commissioner General's Office for Italy's participation in EXPO 2020 Dubai within the 2020 edition of the Sustainable

Si consideri una lista di numeri interi Lista implementata come lista doppiamente puntata non circolare implementata utilizzando la seguente struttura.. struct