phpbar.de logo

Mailinglisten-Archive

[php]Doppelt verkettete List / Zeiger

[php]Doppelt verkettete List / Zeiger

Marco Peemöller listen at goto-marco.de
Fre Jun 3 14:29:33 CEST 2005


Hallo,
> <?php
> // ------------------
>    class tKurs
> // ------------------
> {
>    var $name;
> 	var $ort;
> 	var $stadt;
> 	var $pNext;
> 	var $pPrev;
> }
> // -------------------
>    class tKursListe
> // -------------------
> {
>    var $pHead;
> 	var $pTail;
> 	var $pAkt;
> 
>       function tKursListe()
>       {
>           $pHead = $pTail = $pAkt = NULL;
>       }

Müsste das nicht $this->pHead heissen?

> 
> 	function insertHead($data)
> 	// Fügt am Anfang der Liste ein Element hinzu.
> 	{
> 		$tmp = new tKurs;
> 		$tmp->name = $data;
> 		if($this->pHead == NULL) {
>                         $this->pHead = &$tmp;
> 			$this->pTail = &$tmp;
> 			$this->pHead->pNext = NULL;
> 			$this->pHead->pPrev = NULL;
> 			$this->pHead->ort = "Frankfurt";
> 		} else {
>                         $tmp->pPrev = NULL;
>                         $tmp->pNext = &$this->pHead;
>                         $this->pHead->pPrev = &$tmp;
> 			$this->pHead = &$tmp;
> 		}
> 	}
> 
>    function getKursObjekt($name)

Und hier müsste der Funktionskopf m. E. so aussehen:

Function &getKursObjekt($name)

>    {
>       $akt=&$this->pHead;
>       while($akt != NULL) {
>          if( $akt->name == $name ) return($akt);
>             $akt = &$akt->pNext;
>       }
>    }
> }
> //
> --------------------------------------------------------------------------
> ------------
> 
> $liste = new tKursListe;
> $liste->insertHead("nummer1");
> $liste->insertHead("nummer2");
> $liste->insertHead("nummer3");
> $liste->insertHead("nummer4");
> 
> $test = &$liste->getKursObjekt("nummer1");
> $test->ort = "Hamburg";
> print "<p>".$test->name." ".$test->ort;
> 
> print "<hr>";
> $akt = $liste->pHead;
> while( $akt != NULL ) {
>   echo "<p>".$akt->name." ".$akt->ort;
>   $akt = $akt->pNext;
> }
> print "<hr>";
> $akt = &$liste->pTail;
> while( $akt != NULL ) {
>   echo "<p>".$akt->name;
>   $akt = $akt->pPrev;
> }
> 
> ?>

Gruss

Marco


php::bar PHP Wiki   -   Listenarchive