
help? (trying to write a generic linked list)
Hello!
I`m trying to implement a linked list where the type of item it lists
is not known beforehand (for data hiding etc) so the module can be used with
many different applications. Anyway, I get the error
`Expression is not assignment compatible` when I try to compile the module.
with the lines that use location^
e.g. location^ := Item[byteCount]
(see the enclosed source)
Have I made some obvious typo or whats going on (the theory is straight out of
some textbook so it should all work). Any help would be really appreciated.
Please email me if you have an answer as I can`t read usenet often. Thanks
IMPLEMENTATION MODULE GenList;
(*********************************************************************)
(* Description : A generic implementation of the linked list dynamic*)
(* data structure (one which allows any type of data *)
(* item to be stored in the structure). *)
(*********************************************************************)
FROM SYSTEM IMPORT ADDRESS, BYTE;
FROM Storage IMPORT ALLOCATE;
FROM InOut IMPORT WriteString;
TYPE
ListNode = RECORD
data : ADDRESS;
size : CARDINAL;
next : List;
END;
List = POINTER TO ListNode;
PROCEDURE Create(): List;
(* Pre-condition : none.
Post-condition : returns a new, empty list. *)
BEGIN
RETURN NIL;
END Create;
PROCEDURE Cons(OurList : List; Item : ARRAY OF BYTE): List;
(* Add an item to the head of the list
Pre-condition : none.
Post-condition : returns list OurList with Item added to the head. *)
VAR
new : List;
byteCount : CARDINAL; (* Number of bytes needed to store data item. *)
location : ADDRESS; (* Address of Item. *)
BEGIN
(* First we must allocate space for the whole list node. *)
ALLOCATE(new, SIZE(ListNode));
new^.size := (HIGH(Item) + 1);
(* Now allocate the correct number of bytes to store data item. *)
ALLOCATE(new^.data, new^.size);
(* Store data item in memory one byte at a time. *)
location := new^.data;
FOR byteCount := 0 TO HIGH(Item) DO
location^ := Item[byteCount];
INC(location);
END;
(* Set up the node's next pointer to point to the head of the original
list, and then return this node. *)
new^.next := OurList;
RETURN new;
END Cons;
PROCEDURE Head(OurList : List; VAR Item : ARRAY OF BYTE);
(* Set variable Item to contain the data item stored in the head of the list.
Pre-condition : OurList is not empty.
Post-condition : Item contains a copy of the item at the head of OurList *)
VAR
byteCount : CARDINAL; (* Number of bytes taken by item. *)
location : ADDRESS; (* Starting address of item. *)
BEGIN
IF IsEmpty(OurList) THEN
WriteString("Error - Head of empty list");
HALT;
ELSE
(* Set location to the starting address of the data. *)
location := OurList^.data;
(* Now copy each byte into item. *)
FOR byteCount := 0 TO OurList^.size -1 DO
Item[byteCount] := location^;
INC(location);
END;
END;
END Head;
PROCEDURE Tail(OurList : List): List;
(* Return the list formed by removing the head of OurList.
Pre-condition : none.
Post-condition : returns the tail of OurList. *)
BEGIN
IF IsEmpty(OurList) THEN
WriteString("Error - Tail of empty list");
HALT;
ELSE
RETURN OurList^.next;
END;
END Tail;
PROCEDURE IsEmpty(OurList : List): BOOLEAN;
(* Pre-condition : none.
Post-condition : returns TRUE if OurList is empty, FALSE if it isn't. *)
BEGIN
RETURN OurList = Create();
END IsEmpty;
BEGIN
(* No Initialisation code. *)
END GenList.