-- file name listndbl.adb 2:51PM 9/1/95 with gnat.IO; use gnat.IO; -- needed for printing message on exception package body listn.dbl is procedure insert(value:in element; afterX, inList: in out pointer) is t: pointer:= new dlistnode'(data => value, next => null, previous => null); -- note that, even though t is initialized as a dlistnode ptr, you still -- must use conversion to get access to non-listnode'class fields -- in this case, "previous" begin if inList = null then inList := t; -- inserting into the empty list elsif afterX = null -- inserting at front of list then dlistnode(inList.all).previous := t; inList := t; else t.next := afterX.next; -- insert after afterX dlistnode(t.all).previous := afterX; dlistnode(t.next.all).previous := t; afterX.next := t; end if; end insert; function cons(x:in element; toList : in pointer) return pointer is L: pointer; begin L := new dlistnode'(data => x, next => toList, previous => null); if toList /= null then dlistnode(toList.all).previous := L; end if; return L; end cons; function prev(DL : in pointer) return pointer is noprevmt : exception; begin if IsEmpty(DL) then raise noprevmt; else return dlistnode(DL.all).previous; end if; exception when noprevmt => put_line("tried to find prev of empty list"); return null; end prev; procedure delete(x, precededByY, inList: in out pointer) is begin if precededByY = null then inList := inList.next; -- deleting first element dlistnode(inList.all).previous := null; -- fix previous pointer of new first element else precededByY.next := x.next; dlistnode(x.next.all).previous := dlistnode(x.all).previous; end if; -- dispose(x); or free(x); if you choose to define it and do so end delete; end listn.dbl;