Structures_LinkedList
[ class tree: Structures_LinkedList ] [ index: Structures_LinkedList ] [ all elements ]

Source for file single_link_example.php

Documentation is available at single_link_example.php

  1. <?php
  2.  
  3. require 'Structures/LinkedList/Single.php';
  4.  
  5. /* To do anything useful with a linked list, you need to
  6.  * extend the Node class to hold data associated with the
  7.  * node. In this case, we're just going to hold a single
  8.  * integer in the $_my_number property.
  9.  */
  10.     protected $_my_number;
  11.     protected $_my_letter;
  12.  
  13.     function __construct($num$letter{
  14.         $this->_my_number = $num;
  15.         $this->_my_letter = $letter;
  16.     }
  17.  
  18.     function getNumber({
  19.         return $this->_my_number;
  20.     }
  21.  
  22.     function getLetter({
  23.         return $this->_my_letter;
  24.     }
  25.  
  26.     function setNumb($numb{
  27.         $this->_my_number = $numb;
  28.     }
  29.  
  30.     function __toString({
  31.         return "{$this->getNumber()}";
  32.     }
  33. }
  34.  
  35. /* To enable key=>value iteration, we must override the default key()
  36.  * method in Structures_LinkedList_Single to return a meaningful value */
  37.     function key({
  38.         return $this->current()->getLetter();
  39.     }
  40. }
  41.  
  42. /* Now we'll create some instances of the new class */
  43. $node1 = new LinkNodeTester(1'a');
  44. $node2 = new LinkNodeTester(2'b');
  45. $node3 = new LinkNodeTester(3'c');
  46. $node4 = new LinkNodeTester(4'd');
  47. $node5 = new LinkNodeTester(5'e');
  48.  
  49. /* Start by instantiating a list object.
  50.  * You can either pass the first node to the constructor,
  51.  * or leave it null and add nodes later.
  52.  */
  53. $list = new LinkListTester($node1)// 1
  54.  
  55. /* appendNode() adds a node to the end of the list */
  56. $list->appendNode($node2)// 1-2
  57.  
  58. /* prependNode() adds a node to the start of the list */
  59. $list->prependNode($node3)// 3-1-2
  60.  
  61. /* insertNode($new_node, $reference_node, $before) adds a node
  62.  * before the reference node if the third parameter is true,
  63.  * or after the reference node if the third parameter is false
  64.  */
  65. $list->insertNode($node4$node2true)// 3-1-4-2
  66.  
  67. /* current() returns the current pointer node in the list */
  68. $link $list->current()// 1
  69.  
  70. /* You can iterate through a list with the next() method */
  71. do {
  72.     print $link->getNumber();
  73. while ($link $list->next())// 1-4-2
  74.  
  75. /* rewind() resets the pointer to the root node of the list */
  76. $link $list->rewind()// 3
  77.  
  78. /* You can also iterate through a list with foreach() */
  79. foreach ($list as $bull{
  80.   print $bull->getNumber();
  81. // 3-1-4-2
  82.  
  83. /* Override the key() method to enable $key=>$value iteration */
  84. foreach ($list as $key=>$value{
  85.   print "$key => $value\n";
  86. }
  87.  
  88. /* end() resets the pointer to the last node of the list */
  89. $link $list->end()// 2
  90.  
  91. /* You can iterate backwards through a list with previous() */
  92. do {
  93.     print $link->getNumber();
  94. while ($link $list->previous())// 2-4-1-3
  95. ?>

Documentation generated on Mon, 11 Mar 2019 15:38:21 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.