Advertisement
Guest User

Untitled

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