Path: utzoo!attcan!uunet!husc6!mailrus!cornell!uw-beaver!uw-june!ka
From: ka@june.cs.washington.edu (Kenneth Almquist)
Newsgroups: comp.lang.fortran
Subject: Re: Data types _without_ pointers
Message-ID: <5871@june.cs.washington.edu>
Date: 28 Sep 88 19:34:34 GMT
References: <1032@amelia.nas.nasa.gov> <4026@lanl.gov>
Organization: U of Washington, Computer Science, Seattle
Lines: 52

jlg@lanl.gov (Jim Giles) writes:
> I have never seen a data structure that couldn't be described without
> pointers.
>
> [...]
>
> _NONE_ of these require user accessable pointers not even the linked list!
> Items of type 'list' have values of type 'list' - _not_ values of type
> 'address_of_list'.  In no case may the user of any of these data type
> alter the memory location of a data object (without copying the data).

When *I* talk about a linked list, I mean (among other things) a data
structure in which it is possible to insert an element at an arbitrary
location in the list in time O(1).  Jim doesn't say how the data structures
he talks about being able to describe can be manipulated, but the last
comment quoted above certainly suggests that inserting an element in a
list using Hoare's mechanism requires copying the entire list up to the
point of an insertion, so an insertion at an arbitrary point in the list
requires time O(n).  In article <4108@lanl.gov>, Jim gives an implementation
of a fifo queue using an array.  The standard implementation of a queue
using a linked list requires time O(1) to add an element, while Jim's code
has a worst case time of O(n) to add an element, where n is the number of
elements in the queue.

The whole point of chosing a data structure is to find one which will
handle the operations you want to perform on it efficiently.  If Hoare's
method of describing data structures does not let us describe linked lists
and queues with access times of order O(1), then it is only useful in
contexts where efficiency doesn't matter.  To quote again from article
<4108@lanl.gov>:

> Fifo's _can_ be implemented as recursive data structures, but arrays
> with indices are easier and more efficient.

But if your language supports pointers, implementing a queue using a
linked list is easier than using an array.  I'm pretty sure I could get
a linked list implementation right the first time, whereas getting the
array implementation right is considerably harder.  Jim's code contains
a number of bugs (e.g. queue%empty and queue%full are not set correctly,
growing the array is done incorrectly).  Fixing the bugs will increase
the complexity of the code significantly, and it will *still* suffer
from an O(n) worst case time.

> Use the structure which best fits the problem.  This means: rarely use
> pointers

In my view, pointers quite often fit the problem.  I do see the value
of abstract data types, which allow the user to define a queue data
type once and then use it where ever a queue is needed, but abstract
data types don't eliminate the need for pointers to allow queues to be
implemented easily in the first place.
				Kenneth Almquist