daily pastebin goal
91%
SHARE
TWEET

Untitled

a guest Oct 26th, 2017 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- lab4.adb
  2. -- Materiały dydaktyczne
  3. -- J.P. 2017
  4.  
  5. with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Numerics.Discrete_Random;
  6. use Ada.Text_IO, Ada.Integer_Text_IO;
  7.  
  8. procedure Lab4Lista is
  9. package Los_Liczby is new Ada.Numerics.Discrete_Random(Integer);
  10. use Los_Liczby;
  11.  
  12. type Element is
  13.   record
  14.     Data : Integer := 0;
  15.     Next : access Element := Null;
  16.   end record;
  17.  
  18. type Elem_Ptr is access all Element;
  19.  
  20. procedure Print(List : access Element) is
  21.   L : access Element := List;
  22. begin
  23.   if List = Null then
  24.     Put_Line("List EMPTY!");
  25.   else
  26.     Put_Line("List:");
  27.   end if;
  28.   while L /= Null loop
  29.     Put(L.Data, 4); -- z pakietu Ada.Integer_Text_IO
  30.     New_Line;
  31.     L := L.Next;
  32.   end loop;
  33. end Print;
  34.  
  35. procedure Insert(List : in out Elem_Ptr; D : in Integer) is
  36.   E : Elem_Ptr := new Element;
  37. begin
  38.   E.Data := D;
  39.   E.Next := List;
  40.   -- lub E.all := (D, List);
  41.   List := E;
  42. end Insert;
  43.  
  44. -- wstawianie jako funkcja - wersja krótka
  45. function Insert(List : access Element; D : in Integer) return access Element is
  46.   ( new Element'(D,List) );
  47.  
  48. -- do napisania !!
  49. procedure Insert_Sort(List : in out Elem_Ptr; D : in Integer) is
  50.   E : Elem_Ptr := new Element;
  51.   N : Elem_Ptr := List.Next;
  52.   P : Elem_Ptr := List;
  53. begin
  54.   E.Data := D;
  55.   if(E.Data < P.Data) then
  56.     begin
  57.       E.Next := P;
  58.       List := E;
  59.       return;
  60.     end;
  61.   end if;
  62.  
  63.   while N /= Null loop
  64.     if(E.Data < N.Data) then
  65.       begin
  66.         E.Next := N;
  67.         P.Next := E;
  68.         return;
  69.       end;
  70.     else
  71.       P:=N;
  72.       N:=N.Next;
  73.     end if;
  74.   end loop;
  75.  
  76.   P.Next := E;
  77.  
  78. end Insert_Sort;
  79.  
  80. procedure Random_Insert_Sort(Lista :in out Elem_Ptr; N : in Integer; M: in Integer) is
  81.   Wart: Integer := 0;
  82.   Gen: Generator;
  83. begin
  84.   Reset(Gen);
  85.   for I in 1..N loop
  86.     Wart := Random(Gen) mod M;
  87.     Insert_Sort(Lista, Wart);
  88.   end loop;
  89. end Random_Insert_Sort;
  90.  
  91. procedure List_Search(Lista : in out Elem_Ptr; I: in Integer) is
  92.   N: Elem_Ptr := Lista;
  93. begin
  94.   while N /= Null loop
  95.     if(N.Data = I) then begin
  96.       Put_Line("Siupa" & N.Data'Img);
  97.       return;
  98.     end;
  99.     end if;
  100.     N := N.Next;
  101.   end loop;
  102.   return;
  103. end List_Search;
  104.  
  105. function List_Search_return(Lista : in out Elem_Ptr; I: in Integer) return Elem_Ptr is
  106.   N: Elem_Ptr := Lista;
  107. begin
  108.   while N /= Null loop
  109.     if(N.Data = I) then begin
  110.       Put_Line("Siupa" & N.Data'Img);
  111.       return N;
  112.     end;
  113.     end if;
  114.     N := N.Next;
  115.   end loop;
  116.   return N;
  117. end List_Search_return;
  118.  
  119. procedure List_remove(Lista: in out)
  120. --procedure List_remove(Lista: in out Elemt_Ptr; I: in Integer) is
  121. --  N: Elem_Ptr := Lista;
  122. --begin
  123. --  while N /= Null loop
  124. --  end loop;
  125. --end List_remove;
  126.  
  127. Lista : Elem_Ptr := Null;
  128. V1 : Elem_Ptr;
  129. begin
  130.   Print(Lista);
  131.   Lista := Insert(Lista, 21);
  132.   Print(Lista);
  133.   Insert(Lista, 20);
  134.   Print(Lista);
  135.   for I in reverse 1..12 loop
  136.     Insert(Lista, I);
  137.   end loop;
  138.   Print(Lista);
  139.   Insert_Sort(Lista,13);
  140.   Print(Lista);
  141.   --Random_Insert_Sort(Lista, 10, 20);
  142.   --Print(Lista);
  143.   V1 := List_Search_return(Lista, 20);
  144.   Put_Line("przeszlo" & V1.Data'Img);
  145. end Lab4Lista;
RAW Paste Data
Top