with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; with Ada.Unchecked_Deallocation; -- Generic procedure package body listpkg is ----------------------------------------------------------------- procedure clear(l: in out List) is -- Delete all nodes in list -- Instantiate deallocation routine for Lists procedure free is new Ada.Unchecked_Deallocation(Node, List); t: List; begin -- Deallocate all nodes while l /= null loop t := l; l := l.all.next; free(t); end loop; end clear; ----------------------------------------------------------------- procedure makeList(first, last: Natural; l: out List) is -- Creates a list of integers from first to last begin l := null; for i in reverse first .. last loop l := new Node'(i, l); end loop; end makeList; ----------------------------------------------------------------- procedure putList(l: List) is -- Print the list t: List := l; -- l is in mode and so can't be changed begin while t /= null loop put(t.all.val); t := t.all.next; end loop; end putList; ----------------------------------------------------------------- function equal(l, r: List) return Boolean is -- compare entire list, not just pointers l and r tl: List := l; tr: List := r; ans: Boolean := true; begin while tl /= null and tr /= null loop if tl.all.val /= tr.all.val then ans := false; end if; tl := tl.all.next; tr := tr.all.next; end loop; return ans and then tl = tr; end equal; ----------------------------------------------------------------- procedure copy(dest: out List; source: In List) is -- Make dest point to a deep copy of souce curNode : List := source; curNodeCopy : List; begin if source = null then dest := null; else curNodeCopy := new Node'(source.val, null); dest := curNodeCopy; curNode := curNode.all.Next; while curNode /= null loop curNodeCopy.all.Next := new Node'(curNode.all.val, null); curNodeCopy := curNodeCopy.all.Next; curNode := curNode.all.Next; end loop; end if; end copy; ----------------------------------------------------------------- procedure shallowcopy(dest: out List; source: In List) is -- Make dest point to a deep copy of souce -- This is the original version. It makes a shallow copy. begin dest := source; -- warning: shallow copy end shallowcopy; end listpkg;