Hi!
Trying again.
I expect this code to produce:
8 9
Hej verden
but it actually produces
8
Hej verden
I suspect I have a scope problem in this line:
$this->tail->next = new IntNode($el,null);
Something about the left side having 3 parts...
Anyway. Help?
Lise
IntNode.obj
<?php
// a node in an integer singly linked list class
class IntNode {
var $info; // integer
var $next; // IntNode
function IntNode($i,$n) {
$this->info = $i;
$this->next = $n;
}
}
?>
IntSLList.obj
<?php
// singly linked list class to store integers
class IntSSList {
var $head; // IntNode
var $tail; // IntNode
function IntSLList() {
$this->head = null;
$this->tail = null;
}
function isEmpty() {
return ($this->head == null);
}
function addToHead($el) {
$this->head = new IntNode($el,$this->head);
if ($this->tail == null) {
$this->tail = $this->head;
}
}
function addToTail($el) {
if (!$this->isEmpty()) {
$this->tail->next = new IntNode($el,null);
$this->tail = $this->tail->next;
} else {
$this->tail = new IntNode($el,null);
$this->head = $this->tail;
}
}
function printAll() {
$tmp = $this->head;
while ($tmp != null) {
print $tmp->info;
$tmp = $tmp->next;
}
print "<br>\n";
}
}
?>
test.php
<?php
include "IntNode.obj";
include "IntSLList.obj";
$test3 = new IntSSList();
$test3->addToTail(8);
$test3->addToTail(9);
$test3->printAll();
?>
<html><head><title>Hej verden</title></head>
<body>
Hej verden
</body>
</html>