
- •Модули для лабораторных работ.
- •Var I:byte;
- •Var I:Index;
- •Var I:byte;
- •Interface
- •V:BaseType;
- •Var TablError:byte;
- •Implementation
- •Var I:index;
- •Interface
- •Var tablerror:byte;
- •Implementation
- •Interface
- •Var TablError:byte;
- •Implementation
- •Var I:index;
- •Interface
- •Var TablError:byte;
- •Implementation
- •Var I:index;
- •Interface
- •Implementation
- •Var I:index;
- •Interface
- •Implementation
- •Interface
- •Implementation
- •Interface
- •Implementation
- •Interface
- •Implementation
- •Interface
- •Implementation
- •Interface
- •Implementation
- •Interface
- •Implementation
- •Interface
- •Implementation
- •Interface
- •Implementation
- •Interface
- •Implementation
- •Interface
- •Implementation
Interface
uses UBTree;
Const tablok=ok; {успешное завершение операции}
tnotfound=notfound; {элемент не найден}
tablnotmem=notmem; {не хватает памяти}
tkeyexist=keyexist; {элемент с таким ключом уже существует}
Type tabl=elptr;
Var tablerror:byte;
Procedure InitTabl(var T:tabl); {конструктор}
Procedure PutTabl(var T:tabl;e:basetype;k:integer);{помещение элемента в таблицу}
Procedure GetTabl(var T:tabl; var e:basetype; k:integer);{удаление элемента из таблицы}
Procedure DoneTabl(var T:tabl); {деструктор}
Implementation
Procedure InitTabl;
Begin
InitTree(T);
tablerror:=error
End;
Procedure PutTabl;
Begin
PutTree(T,e,k);
tablerror:=error
End;
Procedure GetTabl;
Begin
GetTree(T,e,k);
tablerror:=error
End;
Procedure DoneTabl;
Begin
DoneTree(T)
End;
End.
{---------------------------------------------------------------------------}
Unit UHeshTD; {метод двойного хеширования}
Interface
Const TablSize=149;
TablOk=0; {успешное завершение операции}
TablOver=1; {таблица переполнена}
TablUnder=2; {таблица пуста}
NotFound=3; {элемент не найден}
c1=3;
c2=5;
c3=11;
Type Index=1..TablSize;
BaseType=byte;
Element=record
key:longint;
Data:BaseType;
end;
ElTabl=record
flag:-1..1;{flag=-1 позиция в таблице свободна, но ранее
использовалась}
{flag=0 позиция в таблице свободна}
{flag=1 позиция в таблице занята}
E:Element;
end;
Tabl=record
Buf:array [index] of ElTabl;
n:0..TablSize; {количество занятых позиций в рассеянной таблице}
end;
Var TablError:byte;
Function Hesh(b:longint):index; {вычисление адреса элемента}
Function ReHesh(b:longint):index; {рехеширование}
Procedure InitTabl(var T:Tabl); {конструктор}
Function EmptyTabl(T:Tabl):boolean; {таблица пуста?}
Function FullTabl(T:Tabl):boolean; {таблица полна?}
Function Search(T:Tabl;var E:Element;key:longint):boolean; {поиск элемента в
таблице по его ключу}
Procedure Put(var T:Tabl;var E:Element); {помещение элемента в таблицу}
Function Del(var T:Tabl;key:longint):boolean; {удаление элемента из таблицы}
Implementation
Function Hesh;
Begin
Hesh:=(c1*b+c2) mod TablSize;
TablError:=TablOk
End;
Function ReHesh;
Begin
ReHesh:=(c2*b+c3) mod TablSize;
TablError:=TablOk
End;
Procedure InitTabl;
Var I:index;
Begin
for i:=1 to TablSize do with T do Buf[i].flag:=0;
T.n:=0;
TablError:=TablOk
End;
Function EmptyTabl;
Begin
EmptyTabl:=T.n=0;
TablError:=TablOk
End;
Function FullTabl;
Begin
FullTabl:=T.n=TablSize;
TablError:=TablOk
End;
Function Search;
var a:index;
Begin
if not EmptyTabl(T) then
begin
a:=Hesh(key);
while (T.Buf[a].E.key<>key) and (T.Buf[a].flag=0) do a:=ReHesh(a);
if T.Buf[a].E.key=key then
begin
E:=T.Buf[a].E;
Search:=true
end
else Search:=false;
TablError:=TablOk
end
else begin
TablError:=TablUnder;
Search:=false
end
End;
Procedure Put;
var a,j:index;
Begin
if not FullTabl(T) then
begin
a:=Hesh(E.key);
while T.Buf[a].flag=1 do a:=ReHesh(a);
T.Buf[a].e:=E;
T.Buf[a].flag:=1;
inc(T.n);
TablError:=TablOk
end
else TablError:=TablOver
End;
Function Del;
var a:index;
E:Element;
Begin
if not EmptyTabl(T) then
if Search(T,E,key) then
begin
a:=Hesh(key);
while (T.Buf[a].flag=-1) and (key=T.Buf[a].E.key) or
(T.Buf[a].E.key<>key) and (T.Buf[a].flag=0) do a:=ReHesh(a);
if T.Buf[a].flag=0 then Del:=false
else if T.Buf[a].E.key=key then
begin
T.Buf[a].flag:=-1;
dec(T.n);
Del:=true;
TablError:=TablOk
end
end
else begin
Del:=False;
TablError:=NotFound
end
else begin
Del:=false;
TablError:=TablUnder
end
End;
End.
{---------------------------------------------------------------------------}
Unit UHeshTP;{метод последовательного опробования}