Article View: comp.lang.misc
Article #452Re: Doing a Generic_Linked_List in Ada
From: William Thomas Wolfe,2847,
Date: Thu, 10 Aug 1989 17:07
Date: Thu, 10 Aug 1989 17:07
64 lines
2755 bytes
2755 bytes
From article <6448@ux.cs.man.ac.uk>, by chl@r1.uucp (Charles Lindsey):
> But how DO you manipulate linked lists in Ada using Generics?
>
> Suppose I want to write a simple procedure to find the last element of a
> linked list. I try the following:
>
> generic
> type T is private;
> type LINK;
> type PLINK is access LINK;
> type LINK is record
> VAL: T;
> NEXT: PLINK;
> end record;
> function FIND_LAST(LIST: PLINK) return PLINK is [...]
OK, let's start with basics. This is the general
paradigm for doing a data structure ADT in Ada:
generic
type Stored_ADT is limited private;
with procedure Assign (To : in out Stored_ADT;
From : in Stored_ADT) is <>;
-- other procedures & functions as necessary...
package Generic_Data_Structure is
type Data_Structure is limited private;
procedure Do_Something_With (The_Structure : in out Data_Structure);
-- other procedures and functions as necessary...
private
type Data_Structure_Descriptor;
-- definition of this type is left to the implementation...
type Data_Structure is access Data_Structure_Descriptor;
end Generic_Data_Structure;
It is now a simple matter to implement the function Obtain_Last_Element
which returns an object of type Stored_ADT. From there, all the user
needs to do is instantiate the Generic_Data_Structure package with the
type of data to be stored. Note that the Assign procedure should do
a complete copy of the ADT in question; if this overhead is intolerable,
it is a simple matter to use a pointer type as the type of object to be
stored in the list, rather than storing the ADT directly; thus, the user
would actually have a list of pointers.
I am sending Mr. Lindsey a copy of the Generic_Linked_List package
from my personal toolbox, so that he might have an example to work
with. Anyone interested in doing Ada ADTs should also read the text
_Software Components with Ada_, by Grady Booch, which will provide a
general introduction to the topic of designing and implementing ADTs.
Bill Wolfe, wtwolfe@hubcap.clemson.edu
Technical Details
Message-ID:
<6234@hubcap.clemson.edu>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!tut.cis.ohio-state.edu!cica!gatech!hubcap!billwolf%hazel.cs.clemson.eduReferences:
<6448@ux.cs.man.ac.uk>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!tut.cis.ohio-state.edu!cica!gatech!hubcap!billwolf%hazel.cs.clemson.edu From: billwolf%hazel.cs.clemson.edu@hubcap.clemson.edu (William Thomas Wolfe,2847,) Newsgroups: comp.lang.misc Subject: Re: Doing a Generic_Linked_List in Ada Message-ID: <6234@hubcap.clemson.edu> Date: 10 Aug 89 17:07:03 GMT References: <6448@ux.cs.man.ac.uk> Sender: news@hubcap.clemson.edu Reply-To: billwolf%hazel.cs.clemson.edu@hubcap.clemson.edu Lines: 64