-- File listg.adb -- This package implements a generic list, using access types -- The list is intended to be used as part of lab5, being used -- to implement a queue package body Listg is procedure Copy (Source: in List; Destination: in out List) is from, to: List; begin from := Source; if Source = null then Destination := null; else Destination := new Node' (Datum => Source.Datum, Tail => null); from := Source.Tail; to := Destination; while from /= null loop to.Tail := new Node' (Datum => from.Datum, Tail => null); to := to.Tail; from := from.Tail; end loop; end if; exception when Storage_Error => raise Overflow; end Copy; procedure Clear (AList: in out List) is begin AList := null; end Clear; procedure Cons (Item: in Element; AndList: in out List) is -- insert a new node containing Item at the front of the list begin AndList := new Node' (Datum => Item, Tail => AndList); exception when Storage_Error => raise Overflow; end Cons; procedure Append (AList: in List; ToList: in out List) is -- make AList the tail of ToList index: List; begin if ToList = null then ToList := AList; else index := ToList; while index.Tail /= null loop index := index.Tail; end loop; index.Tail := AList; end if; end Append; function Equal (Left: in List; Right: in List) return Boolean is lindex, rindex: List; begin lindex := Left; rindex := Right; while lindex /= null loop if lindex.Datum /= rindex.Datum then return False; else lindex := lindex.Tail; rindex := rindex.Tail; end if; end loop; return (rindex = null); exception when Constraint_Error => return False; end Equal; function Empty (AList: in List) return Boolean is begin return (AList = null); end Empty; function First (OfList: in List) return Element is -- return element stored in first node of list begin if OfList = null then raise Underflow; else return OfList.Datum; end if; end First; function Rest (OfList: in List) return List is -- return list following first node begin if OfList = null then raise Underflow; else return OfList.Tail; end if; end Rest; end Listg;