Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 27.12.2013, 12:04   #1
MariaD
Пользователь
 
Аватар для MariaD
 
Регистрация: 10.01.2013
Сообщений: 56
Вопрос Удаление элемента из хеш таблицы

Удаление записано в button3

Код:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,Math, Grids;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    StringGrid1: TStringGrid;
    Label1: TLabel;
    //Label7: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  pElem=^Elem;
  Elem=record
  key:integer;
  next:pElem;
end;

var
  m:integer;
  HT:array [0.. 100] of pElem;
  T:array of integer;
  Cl:array of Integer;
  k:array of integer;

function h(k:integer):integer;
var
  A:Real;
begin
  a:=(sqrt(5)-1)/2;
  h:=Floor(m*Abs(Floor(k*A)-k*A));
  h:=Trunc(m*frac(k*A));
end;

procedure TForm1.FormActivate(Sender: TObject);
var
  i,j:integer;
begin
  m:=10;
  StringGrid1.ColWidths[0]:=20;
  StringGrid1.ColWidths[1]:=110;
  SetLength(T,m);
  SetLength(Cl,m);
  StringGrid1.Cells[0,0]:='¹';
  StringGrid1.Cells[1,0]:='          Ýëåìåíò';
  for i:=1 to 10 do
    StringGrid1.Cells[0,i]:=IntToStr(i);
  for i:=1 to m do
    T[i-1]:=0;
  for j:=1 to m do
    begin
      Cl[j-1]:=0;
      HT[j-1]:=Nil;
    end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  position,k:integer;
  p,cur:pElem;
  find:boolean;
begin
  k:=StrToInt(Edit2.Text);
  position:=h(k);
  cur:=HT[position];
  while (not find) and (cur<>nil) do
    begin
      if cur^.key=k then
        find:=true;
      cur:=cur^.next;
    end;
  if not find then
    Label1.Caption:='Äîáàâëåíî'
  else
    begin
      Label1.Caption:='Òàêîå çíà÷åíèå óæå åñòü('+ inttostr(position+1)+ '-ÿ ñòðîêà)';
      Exit;
    end;
  GetMem(p,sizeof(Elem));
  p^.key:=k;
  p^.next:=nil;
  cur:=HT[position];
  if cur=nil then HT[position]:=p
  else
    begin
      while cur^.next<>nil do
        begin
          cur:=cur^.next;
        end;
      cur^.next:=p;
    end;
  if(StringGrid1.Cells[1,position+1]<>'') then
  StringGrid1.Cells[1,position+1]:=StringGrid1.Cells[1,position+1]+','+inttostr(k)
  else StringGrid1.Cells[1,position+1]:=inttostr(k);
end;



procedure TForm1.Button1Click(Sender: TObject);
var
  i,j:integer;
begin
  m:=StrToInt(Edit1.Text);
  if (m+1)>=10000 then
    begin
      ShowMessage('Ñëèøêîì áîëüøîé ðàçìåð òàáëèöû');
      Exit;
    end;
  StringGrid1.ColWidths[0]:=20;
  StringGrid1.ColWidths[1]:=110;
  SetLength(T,m);
  SetLength(Cl,m);
  StringGrid1.RowCount:=m+1;
  if (StringGrid1.RowCount>=100) then
    begin
      StringGrid1.ColWidths[0]:=25;
      StringGrid1.ColWidths[1]:=105;
    end;
  if (StringGrid1.RowCount>=1000) then
    begin
      StringGrid1.ColWidths[0]:=30;
      StringGrid1.ColWidths[1]:=100;
    end;
  for i:=1 to m do
    T[i-1]:=0;
  for j:=1 to m do
    Cl[j-1]:=0;
  for i:=1 to m do
    begin
      StringGrid1.Cells[0,i]:=IntToStr(i);
      StringGrid1.Cells[1,i]:='';
    end;
end;



procedure TForm1.Button6Click(Sender: TObject);
var
  i,j:integer;
begin
  for i:=0 to StringGrid1.RowCount do
    StringGrid1.Rows[i].Clear;
  m:=10;
  StringGrid1.ColWidths[0]:=20;
  StringGrid1.ColWidths[1]:=110;
  SetLength(T,m);
  SetLength(Cl,m);
  StringGrid1.Cells[0,0]:='¹';
  StringGrid1.Cells[1,0]:='          Ýëåìåíò';
  for i:=1 to 10 do
    StringGrid1.Cells[0,i]:=IntToStr(i);
  for i:=1 to m do
    T[i-1]:=0;
  for j:=1 to m do
    Cl[j-1]:=0;
  StringGrid1.RowCount:=11;
end;




procedure TForm1.Button4Click(Sender: TObject);
begin
  Label1.Caption:='';
  Edit1.Clear;
  Edit2.Clear;
  StringGrid1.Cols[1].Clear;
  StringGrid1.Cells[1,0]:='          Ýëåìåíò';

end;

procedure TForm1.Button3Click(Sender: TObject);
 Var P, cur: pElem; position,k:integer;
Begin
  k:=StrToInt(Edit2.Text);
	  position:=h(k);
If T[position]<>nil then
Begin
	If T[position]^.key=k then
	Begin cur:=T[position]; T[position]:=T[position]^.next; Dispose(cur) end
		Else
		Begin
				P:=T[i]; cur:=P^.next;
While (cur<>nil) and (cur^.key<>k) do
Begin P:=cur; cur:=cur^.next end;
If cur <>nil then
Begin P^.next:= cur ^.next; Dispose(cur) end
 			End;
	End;

end;

procedure TForm1.Button5Click(Sender: TObject);
begin
 close;
end;




end.
MariaD вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Написать программу в любой распространенной среде для создания радужной таблицы использующей любую хеш-функцию(((( плиз Джессика Помощь студентам 4 05.12.2013 20:33
Пример построения Хеш таблицы Jirogirg Помощь студентам 6 14.12.2012 12:59
Даны три открытых хеш-таблицы... nicklifs Помощь студентам 0 11.12.2011 16:29
Паскаль: хеш-таблицы DanielDredd Помощь студентам 0 26.11.2011 14:11
Хеш-таблицы Johnson Общие вопросы Delphi 2 19.08.2011 19:49