comp.lang.ada
 help / color / mirror / Atom feed
From: Michal Nowak <vinnie@inetia.pl>
To: "comp.lang.ada usegroup->mailing list gateway"
	<comp.lang.ada@ada.eu.org>
Subject: Re: Can someone help me understand this queue package?
Date: Sun, 30 Dec 2001 14:14:56 +0100
Date: 2001-12-30T14:14:56+01:00	[thread overview]
Message-ID: <mailman.1009717862.32290.comp.lang.ada@ada.eu.org> (raw)
In-Reply-To: <L4rX7.11472$Zg2.517773@news11-gui.server.ntli.net>

On 01-12-29 at 22:02 Liddle Feesh wrote:

>"Michal Nowak" wrote:
>
>> "ADA 95: THE CRAFT OF OBJECT-ORIENTED PROGRAMMING" - chapter 11
>> (in particular - 11.4)
>
>Okay. In the example given from that page - adding a new "Node" or
>"Appoinment" would be done using the procedure (in my example) "Add".
>
>This procedure is listed as follows:
>
> procedure add (n:  in integer;  q:  in out queue) is
> begin
>  null; -- Do nothing
> end add;
>
>Now - in the example from the site:
>
>    New_Item := new Appointment_Record;
>    if Current /= null then        -- insert after another appointment
>        New_Item.Next := Current.Next;
>        Current.Next  := New_Item;
>    else                           -- insert at start of list
>        New_Item.Next := Diary.First;
>        Diary.First   := New_Item;
>    end if;
>
>
>But the problem comes when you think, what is "New_Item"? Is it a function
>call? No - it can't be, no parameters. Is it an instance of a record?
>Maybe,
>but my compiler (OE) throws up the error "Direct name, New_Item is not
>visable" leading me to believe that I haven't instantiated the name
>somewhere. But what does it instantiate to? And surely when setting up
>"New_Item", you want to fill it with data - which happens in the example
>above. But how can you fill it with data when it doesn't exist...?
>
>Even if I wrote:
>
>New_Item := new Node;
>
>This would still fall over with the same error.


Here is your package spec:
-------------------------------
package queue_package is

  TYPE queue is LIMITED PRIVATE;
   empty_queue   :   EXCEPTION;  -- !! FIRST ERROR REPORTED HERE
          -- !! SECOND ERROR REPORTED HERE
  procedure initialise (q:  in out queue);
  function is_empty_queue (q:  queue) return boolean;
  procedure add(n:  in integer;  q:  in out queue);
  procedure remove(n:  out integer;  q: in out queue);
  --remove raises the exception "empty-queue" if applied to an empty queue

  PRIVATE
  TYPE node;
  TYPE link is ACCESS node;   --ACCESS is a pointer type
  TYPE node is RECORD
    value   :     integer;
    next    :   link :=NULL;
  END RECORD;

  TYPE queue is RECORD
    head :  link;
    tail :  link;
  END RECORD;

END queue_package;
----------------------------

Look at the line where you defined access type to node (you even marked
it with comment).

You had errors, because you did not declared a variable of this type.
Try now:

> procedure add (n:  in integer;  q:  in out queue) is
    New_Item : Link;   --declare a variable of link type
> begin
    New_Item := new node;

    --now set appriopriate fields in New_Item,
    --set Head and Tail for queue, and should be OK.

>  null; -- Do nothing
> end add;

>
>How do I create a new node, and point the 'next' value to the 'value' of
>the
>next node? I know how this is done on paper...

link is access type to node. So speaking in C language, it should point
to element of type node. In your queue you have components Head and Tail,
that should point to first and last element in the queue. They are of the
same type as New_Item is. So:

New_Item.Next := Tail;     --next points you to element, which is still
                           --considered to be on the end of the queue
Tail          := New_Item; --now the last element is the one you created.

Was that helpful?
Mike
-----------------------------------------
                             ____|
                             \%/ |~~\
  O                                  |
 o>>        Mike Nowak               |
 T                                   |
/ >       vinnie@inetia.pl           |
http://www.geocities.com/vinnie14pl _|__




  reply	other threads:[~2001-12-30 13:14 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-12-28 17:14 Can someone help me understand this queue package? Liddle Feesh
2001-12-28 18:16 ` Michal Nowak
2001-12-28 22:57   ` Liddle Feesh
2001-12-29 11:35     ` Michal Nowak
2001-12-29 19:37       ` Liddle Feesh
2001-12-29 20:05         ` Michal Nowak
2001-12-29 20:44           ` Liddle Feesh
2001-12-29 22:02       ` Liddle Feesh
2001-12-30 13:14         ` Michal Nowak [this message]
2001-12-30 22:28           ` Liddle Feesh
2001-12-31 10:32             ` Michal Nowak
2001-12-29 17:13   ` Liddle Feesh
2001-12-29 18:42     ` martin.m.dowie
2001-12-29 19:09       ` Liddle Feesh
2001-12-29 17:13   ` Liddle Feesh
2001-12-29 17:13   ` Liddle Feesh
2001-12-29 17:14     ` Liddle Feesh
2001-12-29 17:39     ` Liddle Feesh
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox