Advertisement
Guest User

Untitled

a guest
Jan 15th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 3.60 KB | None | 0 0
  1.  
  2. with Ada.Unchecked_Deallocation;
  3. with Ada.Text_IO;                         use Ada.Text_IO;                    
  4. with Ada.Integer_Text_IO;                 use Ada.Integer_Text_IO;
  5. package body Sorted_list is
  6.      
  7.    
  8.    function Empty(List_Ptr : in List_Type) return Boolean is
  9.    begin
  10.      
  11.       return List_Ptr = null;  
  12.    end Empty;
  13.    
  14.      ----------------------------------------------------------------------------------
  15.      procedure Insert_First(List_Ptr : in out List_Type; tal : in Integer) is
  16.     Temp : List_Type;
  17.    
  18.      begin
  19.      
  20.      
  21.     if Empty(List_Ptr) then
  22.        List_ptr := new Node_Type;
  23.        List_ptr.Heltal := Tal;
  24.        List_ptr.Next:= null;
  25.     else
  26.        Temp := new Node_Type;
  27.        Temp.heltal := Tal;
  28.        Temp.Next:= List_ptr;
  29.        List_ptr := Temp;
  30.        end if;
  31.          
  32.  
  33.    
  34.    
  35.     end Insert_First;
  36.     ----------------------------------------------------------------------------------------------------
  37.     procedure Insert(List_Ptr : in out List_Type; Tal : in Integer) is
  38.        
  39.     begin
  40.        if Empty(List_Ptr) then
  41.       Insert_First(List_Ptr,Tal);
  42.        elsif Tal < List_Ptr.Heltal then
  43.       Insert_First(List_Ptr,Tal);
  44.        else
  45.       Insert(List_Ptr.Next,Tal);
  46.        end if;
  47.        end Insert;
  48.        
  49.        List_ptr : List_Type;
  50.        
  51.        procedure Put(List_Ptr : in List_Type) is
  52.      
  53.        begin
  54.       if not Empty(List_Ptr) then
  55.          Put(List_Ptr.Heltal, Width=>0);
  56.          New_Line;
  57.          Put(List_Ptr.Next);
  58.       end if;
  59.        end Put;
  60.        
  61.        function Member(List_Ptr : in List_Type; Soktal  : in Integer)
  62.               return Boolean is
  63.        begin
  64.      
  65.       if Empty(List_Ptr) then
  66.          return False;
  67.       elsif Soktal = List_Ptr.Heltal then
  68.          return True;
  69.       else
  70.          return Member(List_ptr.Next,Soktal);
  71.       end if;
  72.      
  73.        end Member;
  74.        
  75.        function Find (List_Ptr : in List_Type; Soktal : in Integer)
  76.      return Integer is
  77.      
  78. begin
  79.      if Empty(List_Ptr)then
  80.        raise Find_Error;
  81.       else
  82.          if Soktal = List_Ptr.Heltal then
  83.             return List_Ptr.Heltal;
  84.          else
  85.         return Find(List_ptr.Next,Soktal);
  86.          end if;
  87.      end if;
  88.      end Find;
  89.      
  90.      procedure Find(List_Ptr : in List_Type; Soktal : in Integer; Result : out Integer) is
  91.        
  92.      begin
  93.        
  94.         Result := Find(List_Ptr,Soktal);
  95.        
  96.      end Find;
  97.    
  98.      
  99.      procedure Free is new Ada.Unchecked_Deallocation(Node_Type, List_type);
  100.      
  101.      procedure Remove(List_Ptr : in out List_Type; Soktal : in Integer) is
  102.         procedure Remove_Process(List_Ptr : in out List_Type; Soktal : in Integer) is
  103.         Temp : List_Type;  
  104.         begin
  105.            if not Empty(List_Ptr) then
  106.           if List_Ptr.Heltal = Soktal then
  107.              Temp := List_Ptr.Next;
  108.              Free(List_Ptr);
  109.              List_Ptr := Temp;
  110.           else
  111.              Remove_Process(List_Ptr.Next,Soktal);
  112.           end if;
  113.            end if;
  114.            
  115.            
  116.            end Remove_Process;
  117.      begin
  118.         if not Empty(List_Ptr) then
  119.            Remove_Process(List_Ptr,Soktal);
  120.         else
  121.            raise Find_Error;
  122.         end if;
  123.  
  124.         end Remove;
  125.  
  126.         procedure Delete(List_Ptr : in out List_Type) is
  127.            
  128.         begin
  129.            if not Empty(List_Ptr) then
  130.           if List_Ptr.Next = null then
  131.              Free(List_Ptr);  
  132.           else
  133.              Delete(List_Ptr.next);
  134.           end if;
  135.           Delete(List_Ptr);
  136.            end if;
  137.         end Delete;
  138.        
  139.         function Length(List_Ptr : in List_Type; counter : in integer)
  140.         return Integer is
  141.             begin
  142.            
  143.            if Empty(List_Ptr) then
  144.           return Counter;
  145.            else return Length(List_Ptr.Next, Counter+1);
  146.            end if;
  147.            
  148.            end Length;
  149.        
  150. end Sorted_list;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement