P
P
Porto_b2019-11-30 18:50:11
Delphi
Porto_b, 2019-11-30 18:50:11

I can not finish the program normally?

Algorithm hash tables by chaining, after executing the following commands:

...
Init;
ADD('abc');
ADD('abs');
ADD('bcd');
ADD('cde');
...

You should get something like this: 5de28ef758e9d764727658.jpeg
The code itself, I have somewhere an error in the ADD () function of adding or searching for S (), I can not understand. Who can help?
I guess the error is in the result of the S() function when it should return an empty reference to the next element of the list (Result := b) . If instead of w := b; write zn := b; then the data is filled in, but if you add more data, then the second element of the list is overwritten destroying the previous one, which means I do not correctly get a link to the next element from the search function S ()
program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

const
    m = 3;

type
    l = ^p;
    p = record
          k : string;
          n : l;
        end;
var
    a : array [1..m-1] of l;
    j : integer;
    t : integer;
    q : l;


procedure Init;
var i : integer;
begin for i := 0 to m-1 do a[i] := nil; end;


function H(s : string) : integer; begin Result := ord(s[1]) mod 97;  end;

function S(k : string) : l;
var b : l; i : integer;
begin
    i := H(k);
    b := a[i];
      while (b <> nil) do
                      if b.k = k then break
                                 else b := b.n;
    Result := b;
end;

function ADD(k : string) : l;
var  w, b, z : l; i : integer;
begin
     i := H(k);
     w := S(k);
     z := a[i];

     if w = nil then
                 begin
                      new(b);
                      b^.k := k;
                      b^.n := nil;
                     if z = nil
                                then a[i] := b
                                else w := b;
                end;
     Result := b;
end;


begin
    Init;
    ADD('abc');
    ADD('abs');
    ADD('bcd');
    ADD('cde');
   if S('abs') <> nil then writeln('find') else writeln('not find');
   for j := 0 to m-1 do
      begin
        q := a[j];
        writeln(q.k);
      end;
    readln;

end.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question