- •Модули для лабораторных работ.
- •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
Const TablSize=117;{разменр таблицы}
TablOk=0;{успешное завершение операции}
TablOver=1;{таблица переполнена}
TablUnder=2;{таблица пуста}
NotFound=3;{элемент не найден}
c1=11;
c2=3;
Type Index=1..TablSize;
BaseType=byte;
Element=record
key:longint;
Data:BaseType;
end;
ElTabl=record
flag:0..1;{flag=1 позиция в таблице занята}
{flag=0 позиция в таблице свободна}
E:Element;
end;
Tabl=record
Buf:array [index] of ElTabl;
n:0..TablSize;{количество занятых позиций в рассеянной таблице}
end;
Var TablError:byte;
Function Hesh(b:longint):index; {вычисление адреса элемента}
Procedure InitTabl(var T:Tabl); {конструктор}
Function EmptyTabl(T:Tabl):boolean;{таблица пуста?}
Function FullTabl(T:Tabl):boolean; {таблица полна?}
Procedure Put(var T:Tabl;E:Element); {помещение элемента в таблицу}
Function Search(T:Tabl;var E:Element;key:longint):boolean; {поиск элемента в
таблице по его ключу}
Function Del(var T:Tabl;key:longint):boolean; {удаление элемента из таблицы}
Implementation
Function Hesh;
Begin
Hesh:=(c1*b+c2) 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+1;
TablError:=TablOk
End;
Procedure Put;
var a:index;
Begin
if not FullTabl(T) then
begin
a:=Hesh(E.key);
while T.Buf[a].flag=1 do
begin
inc(a);
if a>TablSize then a:=1
end;
T.Buf[a].flag:=1;
T.Buf[a].E:=E;
inc(T.n);
TablError:=TablOk
end
else TablError:=TablOver
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
begin
dec(a);
if a<=0 then a:=a+TablSize
end;
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;
Function Del;
var a,j,r:index;
bool:boolean;
Begin
if not EmptyTabl(T) then
begin
a:=Hesh(key);
while (T.Buf[a].E.key<>key)and(T.Buf[a].flag=0) do
begin
inc(a);
if a>TablSize then a:=1
end;
if T.Buf[a].E.key=key then
begin
T.Buf[a].flag:=0;{процесс удаления}
j:=a;
dec(a);
if a<=0 then a:=a+TablSize;
while T.Buf[a].flag<>0 do
begin
r:=Hesh(T.Buf[a].E.key);
if ((j<=r)and(r<a)) or ((r<a)and(a<j)) or ((a<j)and(j<=r)) then
begin
dec(a);
if a<=0 then a:=a+TablSize;
end
else
begin
T.Buf[j].E:=T.Buf[a].E;
T.Buf[a].flag:=0;{процесс удаления}
j:=a;
dec(a);
if a<=0 then a:=a+TablSize;
end
end;
dec(T.n);
Del:=true
end
else begin
TablError:=NotFound;
Del:=false
end
end
else begin
Del:=false;
TablError:=TablUnder
end
End;
End.
{---------------------------------------------------------------------------}
Unit UHeshTS;{метод разделённых цепочек}
