fork download
  1. <?php
  2.  
  3. class LinkedListObject {
  4.  
  5. public $previous;
  6. public $next;
  7. public $data;
  8.  
  9. /* Initialization of linked list object */
  10. public function __construct() {
  11. $this->previous = 0;
  12. $this->next = NULL;
  13. $this->data = NULL;
  14. }
  15. }
  16.  
  17. class LinkedList extends LinkedListObject {
  18. public $len;
  19. public $head;
  20. public $tail;
  21.  
  22. /* Initialization of linked list */
  23. public function __construct() {
  24. $this->len = 0;
  25. $this->head = NULL;
  26. $this->tail = NULL;
  27. }
  28. }
  29.  
  30. function linked_list_create() {
  31.  
  32. $ll = new LinkedList;
  33.  
  34. return $ll;
  35. }
  36.  
  37. function linked_list_object_create() {
  38.  
  39. $ll_object = new LinkedListObject;
  40.  
  41. return $ll_object;
  42.  
  43. }
  44.  
  45. function linked_list_append($ll, $item) {
  46.  
  47. $appendant = linked_list_object_create();
  48. $appendant->data = $item;
  49. $appendant->previous = $ll->tail;
  50. $appendant->next = NULL;
  51.  
  52. if($ll->head == NULL) {
  53. $ll->head = & $appendant;
  54. }
  55.  
  56. $ll->tail->next = $appendant;
  57.  
  58. $ll->tail = $appendant;
  59.  
  60. /* Additional node with length plus one */
  61. $ll->len++;
  62.  
  63. }
  64.  
  65. function linked_list_len($ll) {
  66. return $ll->len;
  67. }
  68.  
  69. $ll = linked_list_create();
  70.  
  71. for($num = 1; $num <= 10; $num++) {
  72. linked_list_append($ll, $num);
  73. printf("%d", linked_list_len($ll));
  74. }
  75.  
  76.  
  77. ?>
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
12345678910