LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Linked list data structure in PHP? (https://www.linuxquestions.org/questions/programming-9/linked-list-data-structure-in-php-579205/)

ivanatora 08-23-2007 07:35 AM

Linked list data structure in PHP?
 
I'm wondering how can I use pointers in PHP. I've experience in C with pointers and creating data structures like lists, linked lists (chains) and similar, and now I want to do these in PHP. The first thing I've read about references are that they ARE NOT pointers :)
I'm interested if PHP can do structs?
*sigh* Why all PHP learning books write only about making SQL queries and writing HTML :(

jiml8 08-23-2007 09:18 AM

In PHP4 or 5 you would set up a class, which would give you an object. You can use the object like a struct, if that is what you want to do. You also can do indirection with PHP. The syntax is not nearly so elegant as C pointer syntax, but it is possible.

95se 08-23-2007 12:42 PM

In PHP 5, everything is now passed-by-reference, which is good, because it makes making data structures simpler. In PHP4, when you would pass an object to a function, it only passed a value (a copy, essentially). So, any where you wanted to pass an object by reference, you had to use an & (ampersand), but, with PHP, it becamse pretty easy to accidently make a copy instead. The main point is, if you plan on supporting PHP4, it'll take a bit more work and be a bit uglier. PHP5 references are not pointers per se, no, but they are used in the way you expect. Anyways, here is part of a simple linked list in PHP5.

Code:

class LinkedList {
  private $head = null;
  private $tail = null;
  private $size = 0;

  public function __construct()
  {
  }

  public function add($data)
  {
    $node = new Node($data);
    if (is_null($this->tail)) {
      $this->head = $this->tail = $data;
      $this->size++;
      return;
    }

    $this->tail->next = $node;
    $node->prev = $this->tail;
    $this->tail = $node;
    $this->size++;
  }

  // ... more functionality goes here
}

class Node {
  public $data = null;
  public $prev = null;
  public $next = null;

  public function __construct($data = null)
  {
    $this->data = $data;
  }
}

Quote:

Originally Posted by ivanatora (Post 2867959)
*sigh* Why all PHP learning books write only about making SQL queries and writing HTML :(

98% of the people who are programming with PHP only want to do that.

ivanatora 08-23-2007 04:03 PM

Thank you!
I'd better sit on these classes.

graemef 08-23-2007 04:57 PM

In the strict sense of the definition a reference is not a pointer, however it is a way of referring to another variable and so from a data structures view point it performs the task of a pointer. Many years ago I wrote some code in PHP illustrating how the basic data structures work, so it's possible, and yes I used the class approach suggested by the previous posters.

ivanatora 08-24-2007 06:57 AM

Just for clearance, I believe that line of your code:
Code:

$this->head = $this->tail = $data;
Must be changed with that:
Code:

$this->head = $this->tail = $node;
If I'm right, I've began to get the point :)

Yahoo! My first PHP stack:
Code:

<?php
class Node {
        var $next,$data;
        function Node($data){
                $this->next = null;
                $this->data = $data;
        }
}     

class Stack {
        function Stack(){
        }
        var $sp = null;
        function push($data){
                $node = new Node($data);
                $node->next = $this->sp;
                $this->sp = $node;
        }
        function pop(){
                if ($this->sp == null) return;
                $tmp = $this->sp;
                //print $tmp->data . "\n";
                $this->sp = $this->sp->next;
                return $tmp->data;
        }
}
$st = new Stack();

$str = "begin";
while (strcmp("end",$str) != 0){
        print "IN: ";
        $str = trim(fgets(STDIN));
        $st->push($str);
}
print_r($st);
$tmp = $st->pop();
while ($tmp != null){
        print $tmp . "\n";
        $tmp = $st->pop();
}
?>


graemef 08-24-2007 07:09 PM

That looks correct to me, although I've not tried to run it! What I would say is that the constructor you are using is PHP4 syntax, in PHP5 they changed it to be __construct(), if you have access to PHP5 I'd use that because it has much better OO support.


All times are GMT -5. The time now is 04:22 AM.