help? (trying to write a generic linked list) 
Author Message
 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.



Fri, 07 May 1999 03:00:00 GMT  
 help? (trying to write a generic linked list)

Quote:

>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.

There's a potential ambiguity in the meaning of ADDRESS.
If I recall correctly, Wirth's original compilers defined
ADDRESS to mean POINTER TO WORD.  Subsequently some
compiler-writers followed the same convention, while many
others decided that it should really be POINTER TO BYTE.
What you get therefore depends on which compiler you're
using.

The new Modula-2 standard resolves the ambiguity by
insisting that ADDRESS be precisely equivalent to
ARRAY OF LOC.  However, it sounds as if you're using a
pre-standard compiler.

Quote:
>Please email me if you have an answer as I can`t read usenet often. Thanks


Posted and mailed.

--

       http://www.ee.newcastle.edu.au/users/staff/peter/Moylan.html



Fri, 07 May 1999 03:00:00 GMT  
 help? (trying to write a generic linked list)

Quote:

>The new Modula-2 standard resolves the ambiguity by
>insisting that ADDRESS be precisely equivalent to
>ARRAY OF LOC.  However, it sounds as if you're using a
>pre-standard compiler.

Sorry, everyone; Pat Terry has just told me of a
serious thinko.

ADDRESS is, of course, a POINTER TO LOC.  Nothing to do
with arrays.  My error.

--

       http://www.ee.newcastle.edu.au/users/staff/peter/Moylan.html



Sat, 08 May 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Generic Doublely Linked List Module

2. Generic Linked List -- Notes for useage

3. Generic Linked List -- Comments

4. Generic Linked List -- Example of Useage

5. Generic Linked List -- Other necessary files

6. Generic Linked List -- Implementation Module

7. Generic Linked List -- Definition Module

8. Generic Linked List

9. Newbie question about generic linked list tasks

10. Generic Linked Lists

11. Generic linked list

12. Are generic linked lists possible in Fortran95?

 

 
Powered by phpBB® Forum Software