27.Prednaska/ Cvicenie
Z Pascal
27. Cvičenie
< 27.Prednáška | riešené úlohy
Rozcvička
PVrchol = ^TVrchol; TVrchol = record Info:Integer; Next:PVrchol; end;
- dany je spajany zoznam
- funkcia Urob(Z);
- za parnu hodnotu vrcholu prida dalsi s hodnotou 0
1 -> 2 -> 3 -> 4 -> nil 1 -> 2 -> 0 -> 3 -> 4 -> 0 -> nil
Cvičenie
1. pracujeme s triedou TVrchol v Unit1
TVrchol = class Info:Integer; Next:TVrchol; constructor Create(I:Integer; N:TVrchol); function Text:String; end; function TVrchol.Text: String begin Writeln(Info); end;
2. v novom module pouzijeme Unit1
- vytvorit premennu typu TVrchol
- vlozit trojprvkovy zoznam a vypisat
Z:=TVrchol.Create(3,TVrchol.Create(2,TVrchol.Create(1,nil))); Writeln(Z.Text, Z.Next.Text, Z.Next.Next.Text);
3. trieda TZoznam obsahujuca spajany zoznam.
- Obsahuje zaciatok a koniec a proceduru PridajZ, ktora prida vrchol na zaciatok
TZoznam = class Z, K:TVrchol; end; procedure TZoznam:PridajZ(I:Integer); begin Z:=Vrchol.Create(I, Z); if Z.Next = nil then K:=Z; end;
4. metoda TZoznam.Vypis
- vypise zoznam od zaciatku po koniec
- pozor na to, aby sme namiesto pomocnej premennej P nepracovali priamo so Z, aby potom nedoslo k strate informacie o zaciatku zoznamu
procedure TZoznam:Vypis; var P:TZoznam; begin P:=Z; while P <> nil do begin Write(P.Text, ' -> '); P:=P.Next; end; Writeln; end;
- test procedury v druhom module
Z:=TZoznam.Create; for i:=1 to 8 do Z.PridajZ(i); Z.Vypis; Z.PridajZ(9); Z.Vypis;
5. metoda TZoznam.PridajK
- prida vrchol na koniec
- aby sme predisli chybam je dobre situaciu si nakreslit
procedure TZoznam:PridajK; var R:Tvrchol; begin R:=Vrchol.Create(I, nil); if K = nil then begin Z:=R; K:=R; end else K.next:=R; end;
6. metoda TZoznam.VyhodZ
- vyhodi prvy vrchol
procedure TZoznam:VyhodZ; var P:Tvrchol; begin P:=Z; if P <> nil then begin P:=Z.Next; Z.Free; Z:=P; if Z = nil then K:=P; end end;
7. konstruktor
- bez parametra aj s parametrom
class TZoznam constructor Create; constructor Create(N:integer); ...
constructor TVrchol.Create(N: integer); begin while N > 0 do begin PridajZ(N); Dec(N); end; end;
- v testovacom module mozeme for cyklus nahradit konstruktorom
Z:=TZoznam.Create(8);
8. Limitovat vypis na 100 prvkov
procedure TZoznam:Vypis; var P:TZoznam; i:integer; begin P:=Z; i:=0; while (P <> nil) and (i < 100) do begin Write(P.Text, ' -> '); P:=P.Next; Inc(i); end; if (P <> nil) Write('...'); Writeln; end;
9. metoda TZoznam.pocet
function TZoznam:Pocet :integer; var P:TZoznam; i:integer; begin P:=Z; i:=0; while (P <> nil) do begin P:=P.Next; Inc(i); end; result := i; end;
- test
Z:=TZoznam.Create(1000000); Z.Vypis; Z.Pridaj(9); Z.Vypis; Writeln('Pocet = ', Z.Pocet);
10. konstruktor s otvorenym polom ako parametrom
constructor TZoznam.Create(A:array of integer); var i:integer; begin for i := 0 to high(A) do PridajK(A[i]); end;
- ina moznost napisat for cyklus
constructor TZoznam.Create(A:array of integer); var i:integer; begin for i in A do PridajK(i); end;
11 konstruktor, ktory dostava iny zoznam ako parameter a vytvori kopiu zoznamu
constructor TZoznam.Create(A:TZoznam); var P:TVrchol; begin P:=A.Z; while P <> nil do begin PridajK(P.Text); P := P.Next; end; end;