Cod sursa(job #945731)

Utilizator RusuAlexeiRusu Alexei RusuAlexei Data 2 mai 2013 19:00:27
Problema Hashuri Scor 70
Compilator fpc Status done
Runda Arhiva educationala Marime 1.41 kb
program hashuri;
  type lista=^celula;
       celula=record
                info:longint;
                next:lista;
              end;
  var n,x,i:longint;
      op:byte;
      a:array [0..100003] of lista;
      r:lista;

function hash(x:longint):longint;
  begin
    hash:=x mod 100003;
  end;

procedure insert(x:longint);
  var y:longint;
  begin
    y:=hash(x);
    new(r);
    r^.info:=x;
    r^.next:=a[y];
    a[y]:=r;
  end;

procedure delete(x:longint);
  var y:longint;
  begin
    y:=hash(x);
    r:=a[y];
    if r<>nil then begin
    while r^.next<>nil do
      begin
        if r^.next^.info=x then
          begin
            r^.next:=r^.next^.next;
          end else r:=r^.next;

      end;
    if a[y]^.info=x then a[y]:=a[y]^.next;
    end;
  end;

function search(x:longint):byte;
  var y:longint;
      s:boolean;
  begin
    y:=hash(x);
    s:=false;
    r:=a[y];
    while (r<>nil) and (not s)do
      begin
        if x=r^.info then s:=true;
        r:=r^.next;
      end;
    if s then search:=1 else search:=0;
  end;

begin
  assign(input,'hashuri.in');
  reset(input);
  assign(output,'hashuri.out');
  rewrite(output);
  readln(n);
  for i:=1 to n do
    begin
      readln(op,x);
      case op of
        1: insert(x);
        2: delete(x);
        3: writeln(search(x));
        end;
    end;
  close(input);
  close(output);
end.