Advertisement
Guest User

Untitled

a guest
Jan 29th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 3.05 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9.  
  10. type
  11.     plista=^Tlista;
  12.   Tlista=record
  13.     x:integer;
  14.     n:plista;
  15.   end;
  16.  
  17.  
  18. procedure dodajelem(var head:plista; wartosc:integer);
  19. function ileelem(head:plista):integer;
  20. procedure wyswietlliste(head:plista);
  21. procedure posortuj(var lista:plista);
  22.  
  23. implementation
  24.  
  25. procedure dodajelem(var head:plista; wartosc:integer);
  26. var nowy:plista;
  27. begin
  28.   new(nowy);
  29.   nowy^.x:=wartosc;
  30.   if head<>nil then begin
  31.     nowy^.n:=head;
  32.     head:=nowy;
  33.   end
  34.   else
  35.   begin
  36.     head:=nowy;
  37.     head^.n:=nil;
  38.   end;
  39. end;
  40. function ileelem(head:plista):integer;
  41. begin
  42.   if head=nil then
  43.   ileelem:=0
  44.   else
  45.     begin
  46.       ileelem:=1;
  47.       repeat
  48.         ileelem:=ileelem+1;
  49.         head:=head^.n;
  50.         until head^.n=nil;
  51.  
  52.       end;
  53. end;
  54. procedure wyswietlliste(head:plista);
  55. begin
  56.   if head=nil then
  57.   writeln('Sorry gosciu, nie masz zadnej listy');
  58.   if head<>nil then begin
  59.     writeln('');
  60.     repeat
  61.       writeln(head^.x);
  62.       head:=head^.n;
  63.       until head^.n=nil;
  64.       writeln(head^.x);
  65.   end;
  66. end;
  67.  
  68. procedure posortuj(var lista:plista);
  69. var z:integer; nowy:plista; i:integer; j:integer; b:integer;
  70. begin
  71.   new(nowy);
  72.   if lista=nil then
  73.   writeln('Nie masz czego sortowac');
  74.  
  75.   if (lista<>nil) and (lista^.n=nil) then
  76.   writeln('Lista ma tylko jeden element');
  77.  
  78.   if (lista<>nil) and (lista^.n<>nil) then
  79.   begin
  80.     nowy:=lista;
  81.     z:=1;
  82.     repeat
  83.       z:=z+1;
  84.       nowy:=nowy^.n;
  85.     until nowy^.n=nil;
  86.     dispose(nowy);
  87.  
  88.     new(nowy);
  89.     nowy:=lista;
  90.     for i:=1 to z-1 do begin
  91.       for j:=1 to z-1 do begin
  92.         if nowy^.x>nowy^.n^.x then
  93.         begin
  94.           b:=nowy^.n^.x;
  95.           nowy^.n^.x:=nowy^.x;
  96.           nowy^.x:=b;
  97.           end;
  98.  
  99.     end;
  100.       end;
  101.     lista:=nowy;
  102.   end;
  103.  
  104. end;
  105.  
  106. end.
  107. --------------------------------------------------------
  108. program project1;
  109.  
  110. uses Unit1,crt;
  111.  
  112.  
  113. var head:plista; i,il,wartoscc:integer;    wybor:byte;
  114. begin
  115.   repeat
  116.   clrscr;
  117.   writeln('menu - listy jednokierunkowe');
  118.   writeln('1-dodaj 5 elementow na sztywno do tablicy - na poczatek');
  119.   writeln('2-dodaj wlasna kombinacje elementow do tablicy - na poczatek');
  120.   writeln('3-wyswietl wartosci elementow tablicy w kolejnosci dodania');
  121.   writeln('4-wyswietl ilosc elementow listy');
  122.   writeln('5-posortuj');
  123.   writeln('');
  124.   writeln('0 - narka');
  125.   readln(wybor);
  126.   case wybor of
  127.  
  128.   1:begin
  129.   dodajelem(head,2); // pierwszy dodany // ostatni element
  130.   dodajelem(head,1);
  131.   dodajelem(head,3);
  132.   dodajelem(head,4);
  133.   dodajelem(head,2); // ostatni dodany // pierwszy element
  134.   end;
  135.  
  136.   2:begin
  137.   writeln('ile chcesz dodac elementow do listy?');
  138.   readln(il);
  139.   for i:=1 to il do begin
  140.   writeln('Podaj ',i,' element listy');
  141.   readln(wartoscc);
  142.   dodajelem(head,wartoscc);
  143.   end;
  144.   end;
  145.  
  146.   3:begin
  147.    wyswietlliste(head);
  148.    readln();
  149.   end;
  150.  
  151.   4:begin
  152.   writeln(ileelem(head));
  153.   readln();
  154.   end;
  155.  
  156.   5:begin
  157.     posortuj(head);
  158.  
  159.  
  160.   end;
  161.   end;
  162.   until wybor=0;;
  163. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement