comp.lang.ada
 help / color / mirror / Atom feed
* Non destructive summation of nodes (Linked List/FIFO Queue)
@ 2002-01-01 23:02 Liddle Feesh
  2002-01-02  0:50 ` Larry Hazel
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Liddle Feesh @ 2002-01-01 23:02 UTC (permalink / raw)


Hi,

Firstly - thankyou to everyone (Larry Hazel, Gautier, Ed Falis, Michal
Nowak, Machael Bode & others) for your kinds assitance in helping me take my
first steps in the great language of Ada95.

I am a little confused, however - at how to count the number of items in a
linked list, without destroying the items in the process.

Basically, I want to do this:

If queue is empty
        display warning message
Else
        loop while next pointer on node is not null
                    print node's value
        end loop
        print last node's value
end if

*So that I can loop through all the nodes. If this isn't possible, then
another solution could be:*

Declare Count_Of_Elements as Integer
Set Count_Of_Elements = 0
While Not Queue.Head = null
          Increment Count_Of_Elements = Count of Elements + 1 -- (Increment
Count)
End Loop
Print Count_Of_Elements


----
However, writing the above in Ada without destroying the queue is tricky.
Michal suggested:

while not (Queue.Is_Empty_Queue(Queue) ) loop
        Queues.Remove (Element, Queue);
        Put(Element);
        New_Line;
end loop;

------

However, the above basically removes each node whilst 'counting' back the
values stored in each.

And for those that have just joined this conversation, I am trying to create
a display function/procedure that gives me a count of the elements in the
queue.

TIA!

Mike

--
Liddle Feesh
 '  O 0 o <"//><  ' o'^
(Remove UNDERPANTS to reply)







^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Non destructive summation of nodes (Linked List/FIFO Queue)
  2002-01-01 23:02 Non destructive summation of nodes (Linked List/FIFO Queue) Liddle Feesh
@ 2002-01-02  0:50 ` Larry Hazel
  2002-01-02  2:06 ` Larry Hazel
  2002-01-02 11:17 ` Michal Nowak
  2 siblings, 0 replies; 5+ messages in thread
From: Larry Hazel @ 2002-01-02  0:50 UTC (permalink / raw)


Liddle Feesh wrote:
> 
> Hi,
> 
> Firstly - thankyou to everyone (Larry Hazel, Gautier, Ed Falis, Michal
> Nowak, Machael Bode & others) for your kinds assitance in helping me take my
> first steps in the great language of Ada95.
> 
> I am a little confused, however - at how to count the number of items in a
> linked list, without destroying the items in the process.
> 
> Basically, I want to do this:
> 
> If queue is empty
>         display warning message
> Else
>         loop while next pointer on node is not null
>                     print node's value
>         end loop
>         print last node's value
> end if
> 
> *So that I can loop through all the nodes. If this isn't possible, then
> another solution could be:*
> 
> Declare Count_Of_Elements as Integer
> Set Count_Of_Elements = 0
> While Not Queue.Head = null
>           Increment Count_Of_Elements = Count of Elements + 1 -- (Increment
> Count)
> End Loop
> Print Count_Of_Elements
> 
> ----
> However, writing the above in Ada without destroying the queue is tricky.
> Michal suggested:
> 
> while not (Queue.Is_Empty_Queue(Queue) ) loop
>         Queues.Remove (Element, Queue);
>         Put(Element);
>         New_Line;
> end loop;
> 
> ------
> 
> However, the above basically removes each node whilst 'counting' back the
> values stored in each.
> 
> And for those that have just joined this conversation, I am trying to create
> a display function/procedure that gives me a count of the elements in the
> queue.
> 
> TIA!
> 
> Mike
> 
Your package doesn't have a non-destructive get function or a way to iterate
over the queue getting each element  Looks like you need these.  Modify the
queue package to add them.

Larry



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Non destructive summation of nodes (Linked List/FIFO Queue)
  2002-01-01 23:02 Non destructive summation of nodes (Linked List/FIFO Queue) Liddle Feesh
  2002-01-02  0:50 ` Larry Hazel
@ 2002-01-02  2:06 ` Larry Hazel
  2002-01-02  2:41   ` James Rogers
  2002-01-02 11:17 ` Michal Nowak
  2 siblings, 1 reply; 5+ messages in thread
From: Larry Hazel @ 2002-01-02  2:06 UTC (permalink / raw)


Another thought is to add each removed element to another queue.  So, you've
still got the queue after displaying each element.  It just has a different
name.

Liddle Feesh wrote:
> 
> Michal suggested:
> 
> while not (Queue.Is_Empty_Queue(Queue) ) loop
>         Queues.Remove (Element, Queue);
          -- Add the removed element to another queue 
          Queues.Add (Element, Another_Queue);
>         Put(Element);
>         New_Line;
> end loop;
> 
> ------
> 
> However, the above basically removes each node whilst 'counting' back the
> values stored in each.
> 

Larry



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Non destructive summation of nodes (Linked List/FIFO Queue)
  2002-01-02  2:06 ` Larry Hazel
@ 2002-01-02  2:41   ` James Rogers
  0 siblings, 0 replies; 5+ messages in thread
From: James Rogers @ 2002-01-02  2:41 UTC (permalink / raw)


Of course, a very simple solution is to merely create a "print" 
procedure for the Queue type. This procedure could visit each node
in the queue and Put the element without removing any element from the
Queue.

Jim Rogers

Larry Hazel wrote:
> 
> Another thought is to add each removed element to another queue.  So, you've
> still got the queue after displaying each element.  It just has a different
> name.
> 
> Liddle Feesh wrote:
> >
> > Michal suggested:
> >
> > while not (Queue.Is_Empty_Queue(Queue) ) loop
> >         Queues.Remove (Element, Queue);
>           -- Add the removed element to another queue
>           Queues.Add (Element, Another_Queue);
> >         Put(Element);
> >         New_Line;
> > end loop;
> >
> > ------
> >
> > However, the above basically removes each node whilst 'counting' back the
> > values stored in each.
> >
> 
> Larry



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Non destructive summation of nodes (Linked List/FIFO Queue)
  2002-01-01 23:02 Non destructive summation of nodes (Linked List/FIFO Queue) Liddle Feesh
  2002-01-02  0:50 ` Larry Hazel
  2002-01-02  2:06 ` Larry Hazel
@ 2002-01-02 11:17 ` Michal Nowak
  2 siblings, 0 replies; 5+ messages in thread
From: Michal Nowak @ 2002-01-02 11:17 UTC (permalink / raw)
  To: comp.lang.ada usegroup->mailing list gateway

On 02-01-01 at 23:02 Liddle Feesh wrote:

>Hi,
>
>Firstly - thankyou to everyone (Larry Hazel, Gautier, Ed Falis, Michal
>Nowak, Machael Bode & others) for your kinds assitance in helping me take
>my
>first steps in the great language of Ada95.
>
>I am a little confused, however - at how to count the number of items in a
>linked list, without destroying the items in the process.

BTW, list is not the same what queue.

>Basically, I want to do this:
>
>If queue is empty
>        display warning message
>Else
>        loop while next pointer on node is not null
>                    print node's value
>        end loop
>        print last node's value
>end if
>
>*So that I can loop through all the nodes. If this isn't possible, then
>another solution could be:*

The queue I sent last time, was just simple, with only basic operations.
You may add your own fuction, which will give you value of node, without
removig it. It may be for example:

function Get_Value (Element_Number : Integer; Queue : Queue_Type) return Integer;

This function can iterate to specified node in the queue, and return its
value without destroying it.

>Declare Count_Of_Elements as Integer
>Set Count_Of_Elements = 0
>While Not Queue.Head = null
>          Increment Count_Of_Elements = Count of Elements + 1 -- (Increment
>Count)
>End Loop
>Print Count_Of_Elements
>
>
>----
>However, writing the above in Ada without destroying the queue is tricky.
>Michal suggested:
>
>while not (Queue.Is_Empty_Queue(Queue) ) loop
>        Queues.Remove (Element, Queue);
>        Put(Element);
>        New_Line;
>end loop;
>
>------

Just as I said, I proposed only basic operations - Add and Remove.
You may extend it by provinding another subprograms.

>However, the above basically removes each node whilst 'counting' back the
>values stored in each.
>
>And for those that have just joined this conversation, I am trying to
>create
>a display function/procedure that gives me a count of the elements in the
>queue.

If you want to know, how many elements is in queue, you may add another
function. In example I sent last time, I was storing number of elements
in queue in variable Number_Of_Elements.

function Count (Queue : Queue_Type) return Integer is
begin
    return Number_Of_Elements;
end Count;

Bye,
Mike




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-01-02 11:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-01-01 23:02 Non destructive summation of nodes (Linked List/FIFO Queue) Liddle Feesh
2002-01-02  0:50 ` Larry Hazel
2002-01-02  2:06 ` Larry Hazel
2002-01-02  2:41   ` James Rogers
2002-01-02 11:17 ` Michal Nowak

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