Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!munnari.oz.au!brolga!uqcspe!cs.uq.oz.au!warwick From: warwick@cs.uq.oz.au (Warwick Allison) Newsgroups: comp.lang.modula2 Subject: Re: POINTER/LINKED LIST HELP? Message-ID: <523@uqcspe.cs.uq.oz.au> Date: 2 Apr 91 02:00:21 GMT References: <1991Mar30.151406.29367@kuhub.cc.ukans.edu> <5164@uniol.UUCP> <1991Apr1.123810.29390@kuhub.cc.ukans.edu> Sender: news@cs.uq.oz.au Reply-To: warwick@cs.uq.oz.au Lines: 71 As a representative example, let me show you: DEFINITION MODULE Lists; FROM SYSTEM IMPORT BYTE; TYPE List; PROCEDURE EmptyList():List; PROCEDURE Insert(VAR Into:List; Element:ARRAY OF BYTE); END Lists. ------------------- IMPLEMENTATION MODULE Lists; FROM SYSTEM IMPORT BYTE; FROM Storage IMPORT ALLOCATE; TYPE List=POINTER TO ListRec; ListRec = RECORD Data:ADDRESS; Size:CARDINAL; Next:List; END; PROCEDURE EmptyList():List; BEGIN RETURN NIL END EmptyList; PROCEDURE Insert(VAR Into:List; Element:ARRAY OF BYTE); VAR Head:List; Byte:CARDINAL; BEGIN NEW(Head); WITH Head^ DO Size:=HIGH(Element)+1; ALLOCATE(Data,Size); FOR Byte:=0 TO HIGH(Element) DO Data[Byte]:=Element[Byte]; END; Next:=Into; END; Into:=Head; END Insert; END Lists. ------------------------------ Okay, ignoring any syntax errors I have made, this works on most Modula-2 compilers. In particular, those supporting the ARRAY OF BYTE standard. This standard dictates that ANY type can be passed to a field which is an ARRAY OF BYTE. This is in no way a full complement of procedures, but it is easy to implement the rest, once you see the basic technique. now try... Append, Delete, NthElement, Cardinality, IsIn, etc. Oh, notice that these procedures are not VERY efficient, with all the block data passing, but they work quite well, and are very generic. Have fun, Warwick. -- _--_|\ warwick@cs.uq.oz.au / * <-- Computer Science Department, \_.--._/ University of Queensland, v AUSTRALIA.