Meddelande

Minska
No announcement yet.

Stackalgoritm

Minska
X
 
  • Filter
  • Klockan
  • Show
Clear All
new posts

  • Stackalgoritm

    Jag lyckades inte hitta några bra stackfunktioner i PHP3 så jag slängde ihop en egen klass.

    I PHP4 finns stackmetoderna push och pop implementerade i Arrays.

    Detta är en vanlig LIFO ( Last in, First Out) stack.

    [kod]
    /*************************************
    * PsStack
    * Class that implements a standard
    * LIFO stack in PHP.
    *
    * (C) Copyright 1999, 2000 by
    * Pär Svanström (per@svanstrom.nu)
    *
    * The code may be used freely but this
    * note must remain intact if the code
    * is redistributed.
    *
    * http://www.svanstrom.nu
    *************************************/

    class PsStack
    {
    var $stack;
    var $stackPointer = -1;
    var $maxSize;

    /*************************************
    * CONSTRUCTOR
    * Initialize the max size of the stack.
    * Default size is set to 100
    *************************************/
    function PsStack( $maxSize = 100 )
    {
    $this->maxSize = $maxSize;
    }

    /*************************************
    * ISEMPTY
    * Returns TRUE if the stack is empty,
    * otherwise false.
    *************************************/
    function isEmpty()
    {
    return ($this->stackPointer == -1);
    }

    /*************************************
    * ISFULL
    * Returns TRUE if the stack is full,
    * otherwise false.
    *************************************/
    function isFull()
    {
    return ($this->stackPointer >= $this->maxSize - 1);
    }

    /*************************************
    * MAKEEMPTY
    * Sets the internal stack pointer to
    * -1, thereby reseting the stack.
    *************************************/
    function MakeEmpty()
    {
    $this->stackPointer = -1;
    }

    /*************************************
    * PUSH
    * Pushes data onto the stack. Dies
    * with an error if the stack is full.
    *************************************/
    function push( $data )
    {
    if($this->isFull())
    {
    die("Stack overflow!");
    }
    else
    {
    $this->stackPointer++;
    $this->stack[ $this->stackPointer ] = $data;
    }
    }

    /*************************************
    * POP
    * Pops data from the stack. Dies
    * with an error if the stack is empty.
    *************************************/
    function pop()
    {
    if($this->isEmpty())
    {
    die("Stack is empty!");
    }
    else
    {
    $temp = $this->stack[$this->stackPointer];
    $this->stackPointer--;
    return $temp;
    }
    }

    /*************************************
    * TOP
    * Peeks at the top data without
    * removing it. Dies if stack is empty.
    *************************************/
    function top()
    {
    if(!$this->isEmpty())
    {
    return $this->stack[$this->stackPointer];
    }
    else
    {
    die("Stack is empty!");
    }
    }
    }[/kod]

    Du kan testa stacken med följande kod:

    [kod]
    $myStack = new PsStack(10);

    $myStack->push("Ett");
    $myStack->push("Två");
    $myStack->push("Tre");
    $myStack->push("Fyra");
    $myStack->push("Fem");
    $myStack->push("Sex");
    $myStack->push("Sju");
    $myStack->push("Åtta");
    $myStack->push("Nio");
    $myStack->push("Tio");

    print $myStack->pop() . "<br>";
    print $myStack->pop() . "<br>";
    print $myStack->pop() . "<br>";
    print $myStack->pop() . "<br>";
    print $myStack->pop() . "<br>";
    print $myStack->pop() . "<br>";
    print $myStack->pop() . "<br>";
    print $myStack->pop() . "<br>";
    print $myStack->pop() . "<br>";
    print $myStack->pop() . "<br>";
    print $myStack->pop() . "<br>";
    [/kod]

    Utdatat blir följande:


    Tio
    Nio
    Åtta
    Sju
    Sex
    Fem
    Fyra
    Tre
    Två
    Ett
    Stack is empty!


    Vid fel (stack overflow, stack empty) så dör scriptet med ett felmeddelande.

    Om stackens konstruktor ej har något argument (dvs. new PsStack() så sätts stackens maxstorlek till 100.


    ------------------

    1010011010 - The binary number of the beast

    [Redigerat av sgtpepper den 02 jul 2000]

    [Redigerat av sgtpepper den 02 jul 2000]
Working...
X