Page 2 of 2

Posted: Tue Sep 20, 2005 1:41 pm
by timvw
I also started with (Turbo)Pascal at the university. But i only remember that the prof said that OOP would not be handled in his course (faculty of economics) .. At the computer-science department things were pretty different :)

I've had 2 years with Cobol classes, but in the first year the real subject was algorithms and the second year they paid a lot of attention to data structures.. Basically, they could teach you this in any language..

Exactly the same happened with the Java course. First year focussed on OOD and second years spent a lot of time on patterns..

The advantage that you learn to think about a problem, and then pick the tools that help you realise the solution best..

Posted: Wed Sep 21, 2005 11:15 am
by BDKR
Buddha443556 wrote: In PHP you can use arrays as stacks with array_push(), array_pop(), array_shift(), array_unshift() and array(). There's also the empty() function too.
The above not only applies to stacks, but queues as well. Some things to remember.

1) A stack is LIFO (Last In First Out): Imagine a stack vertically with things being put on the top and popped off the top. Things on the bottom of the stack aren't touched until you've cleared (popped) every thing above it off.

2) A queue is FIFO (First In First Out): Visualize this one in the horizontal. Kinda like waiting in line for tickects. The person at the front of the line is obviously (or at least most likely) the first person to get in line. Therefore, being fair and all that, he get's served and out of line first.

While useful these things are, I've found more use in Web Development for recursion, hashes, and trees. That's not to say it's not useful becuase there will come a point when you're glad you learned it.

Cheers

Posted: Wed Sep 21, 2005 11:40 am
by BDKR
Here's a quick class I wrote some time ago for a queue.

Code: Select all

<?php

# The OO approach
class Que
    {
    var $q=array();
    var $cntr;

    function ingress($val)
        {
        $this->q[]=$val;
        ++$this->cntr;
        }

    function egress()
        {
        if($this->cntr>0)
            {
            --$this->cntr;
            return array_shift($this->q);
            }
        else
            { return false; }
        }

    function drop()
        {
        if($this->cntr>0)
            {
            array_shift($this->q);
            --$this->cntr;
            }
        else
            { return false; }
        }

    function depleted()
        {
        if($this->cntr==0)
            { return true; }
        else
            { return false; }
        }

    }

$que = &new Que;
$list=array('Cey', 'Jordan', 'Sprewell', 'Plunkett', 'Iverson', 'Duncan');

# Enter data into the queue
while(list($k, $v)=each($list))
{ $que->ingress($v); }

print_r($que);

# Remove data from the queue
while($que->depleted()==false)
{
 $val=$que->egress();
 if($val=='Plunkett')
     { echo "$val?? Football is teh suck!\n"; }
 else
     { echo $val."\n"; }
 }

print_r($que);
?>

With PHP5, some may say it would be a good idea to make the $q member variable private, and I agree, but like I said, I wrote this some time ago (as in during the days of PHP4) so I didn't really think about it. Also notice that I didn't use the array_push() function, but instead, "$this->q[]=$val". They both do the same thing, but I like this contruct better. My guess is that's it's faster too, but that doesn't really matter too much.

What would be nice is a method to report what (or whom?) is in the queue without affecting the queue.

Cheers

Posted: Wed Sep 21, 2005 1:30 pm
by pilau
I haven't yet studied queues (later this year though), but if it's like arrays you could iterate through it and read all the elements.

Posted: Thu Sep 22, 2005 5:55 pm
by BDKR
pilau wrote:I haven't yet studied queues (later this year though), but if it's like arrays you could iterate through it and read all the elements.
Well, depending on how the queue is implemented, you could iterate over it. If it's an array in the raw, sure. In my example, you could as the queue itself (realized as an array) is public, but if it was private, then you have no choice but to use it strictly as a queue.

Cheers

Posted: Fri Sep 23, 2005 7:16 am
by pilau
Queues are FIFO, aye?

Posted: Fri Sep 23, 2005 8:02 am
by feyd
pilau wrote:Queues are FIFO, aye?
I direct you to:
BDKR wrote:2) A queue is FIFO (First In First Out): Visualize this one in the horizontal. Kinda like waiting in line for tickects. The person at the front of the line is obviously (or at least most likely) the first person to get in line. Therefore, being fair and all that, he get's served and out of line first.

Posted: Fri Sep 23, 2005 12:23 pm
by pilau
Oops, I skipped that post because of the long code post beneath it... :?