![]() | ![]() |
|
|
Esercitazione ottava
In questa esercitazione è presentata la codifica in Pascal di un programma che classifica un insieme di parole secondo una struttura dati di tipo Hash Table .
Programma HashTable ( VIII )
Program Hashtable ;
type elemento = array [ 1..10 ] of char ;
type link = ^ cell 545c24f a ;
cella = record
data : elemento ;
next : link ;
end;
const k = 5;
type HashTable = array [ 0..k-1 ] of link ;
var x : elemento ;
var val, i : integer;
var f : char ;
var l : link ;
var Table : HashTable ;
function class ( x : elemento ): integer ;
var a, i :integer ;
begin
a := 0 ;
for i := 1 to 10 do a := a + ord ( x [ i ] ) ;
class := a mod k ;
end ;
function insert ( a : elemento ; var l : link ) : link ;
If l = nil then begin
new ( l ) ;
l ^.data := a ;
l ^.next := nil ;
end
else if l ^.data < > a then insert := insert ( a, l ^.next )
else writeln ( ' La stringa è già presente ' ) ;
end;
Begin
writeln ( ' Inserire la frase che si vuole classificare di lunghezza uguale a 10 ' ) ;
for i:=1 to 10 do begin
read ( f ) ;
x [ i ] :=f ;
val :=class ( x ) ;
end ;
Table [ val ] := insert ( x, l );
writeln ( 'Il bucket corrispondente alla stringa è ', val ) ;
readln ;
end .
Esempi pratici
Inserire la frase che si vuole classificare di lunghezza uguale a 10 :
Input : cantami
Output : Il bucket corrispondente alla stringa è 4
Inserire la frase che si vuole classificare di lunghezza uguale a 10 :
Input : o diva del
Output : Il bucket corrispondente alla stringa è 4
Inserire la frase che si vuole classificare di lunghezza uguale a 10 :
Input : Pelide
Output : Il bucket corrispondente alla stringa è 3
Inserire la frase che si vuole classificare di lunghezza uguale a 10 :
Input : Achille
Output : Il bucket corrispondente alla stringa è 1
Inserire la frase che si vuole classificare di lunghezza uguale a 10 :
Input : la ira
Output : Il bucket corrispondente alla stringa è 1
Inserire la frase che si vuole classificare di lunghezza uguale a 10 :
Input : funesta
Output : Il bucket corrispondente alla stringa è 4
Privacy |
Articolo informazione
Commentare questo articolo:Non sei registratoDevi essere registrato per commentare ISCRIVITI |
Copiare il codice nella pagina web del tuo sito. |
Copyright InfTub.com 2025