Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils;
- type
- plista=^Tlista;
- Tlista=record
- x:integer;
- n:plista;
- end;
- procedure dodajelem(var head:plista; wartosc:integer);
- function ileelem(head:plista):integer;
- procedure wyswietlliste(head:plista);
- procedure posortuj(var lista:plista);
- implementation
- procedure dodajelem(var head:plista; wartosc:integer);
- var nowy:plista;
- begin
- new(nowy);
- nowy^.x:=wartosc;
- if head<>nil then begin
- nowy^.n:=head;
- head:=nowy;
- end
- else
- begin
- head:=nowy;
- head^.n:=nil;
- end;
- end;
- function ileelem(head:plista):integer;
- begin
- if head=nil then
- ileelem:=0
- else
- begin
- ileelem:=1;
- repeat
- ileelem:=ileelem+1;
- head:=head^.n;
- until head^.n=nil;
- end;
- end;
- procedure wyswietlliste(head:plista);
- begin
- if head=nil then
- writeln('Sorry gosciu, nie masz zadnej listy');
- if head<>nil then begin
- writeln('');
- repeat
- writeln(head^.x);
- head:=head^.n;
- until head^.n=nil;
- writeln(head^.x);
- end;
- end;
- procedure posortuj(var lista:plista);
- var z:integer; nowy:plista; i:integer; j:integer; b:integer;
- begin
- new(nowy);
- if lista=nil then
- writeln('Nie masz czego sortowac');
- if (lista<>nil) and (lista^.n=nil) then
- writeln('Lista ma tylko jeden element');
- if (lista<>nil) and (lista^.n<>nil) then
- begin
- nowy:=lista;
- z:=1;
- repeat
- z:=z+1;
- nowy:=nowy^.n;
- until nowy^.n=nil;
- dispose(nowy);
- new(nowy);
- nowy:=lista;
- for i:=1 to z-1 do begin
- for j:=1 to z-1 do begin
- if nowy^.x>nowy^.n^.x then
- begin
- b:=nowy^.n^.x;
- nowy^.n^.x:=nowy^.x;
- nowy^.x:=b;
- end;
- end;
- end;
- lista:=nowy;
- end;
- end;
- end.
- --------------------------------------------------------
- program project1;
- uses Unit1,crt;
- var head:plista; i,il,wartoscc:integer; wybor:byte;
- begin
- repeat
- clrscr;
- writeln('menu - listy jednokierunkowe');
- writeln('1-dodaj 5 elementow na sztywno do tablicy - na poczatek');
- writeln('2-dodaj wlasna kombinacje elementow do tablicy - na poczatek');
- writeln('3-wyswietl wartosci elementow tablicy w kolejnosci dodania');
- writeln('4-wyswietl ilosc elementow listy');
- writeln('5-posortuj');
- writeln('');
- writeln('0 - narka');
- readln(wybor);
- case wybor of
- 1:begin
- dodajelem(head,2); // pierwszy dodany // ostatni element
- dodajelem(head,1);
- dodajelem(head,3);
- dodajelem(head,4);
- dodajelem(head,2); // ostatni dodany // pierwszy element
- end;
- 2:begin
- writeln('ile chcesz dodac elementow do listy?');
- readln(il);
- for i:=1 to il do begin
- writeln('Podaj ',i,' element listy');
- readln(wartoscc);
- dodajelem(head,wartoscc);
- end;
- end;
- 3:begin
- wyswietlliste(head);
- readln();
- end;
- 4:begin
- writeln(ileelem(head));
- readln();
- end;
- 5:begin
- posortuj(head);
- end;
- end;
- until wybor=0;;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement