Advertisement
Guest User

php crash segfault

a guest
Mar 10th, 2018
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.98 KB | None | 0 0
  1. <?php
  2.  
  3. class LinkedList {
  4.     // object Node representing the head of the list
  5.     private $head;
  6.     // object Node representing the tail of the list
  7.     private $tail;
  8.  
  9.     public function __construct() {
  10.         $this->head = new Node(null);
  11.         $this->tail = new Node(null);
  12.  
  13.         $this->head->setNext($this->tail);
  14.         $this->tail->setPrevious($this->head);
  15.     }
  16.  
  17.     /**
  18.      * Add node to the tail of the list
  19.      * @param type $key
  20.      * @param type $data
  21.      * @return boolean
  22.      */
  23.     public function addTail($data) {
  24.         $this->attach($this->tail->getPrevious(), new Node($data));
  25.     }
  26.  
  27.     /**
  28.      * Add node to the head of the list
  29.      * @param type $key
  30.      * @param type $data
  31.      */
  32.     public function addHead($data) {
  33.         $this->attach($this->head, new Node($data));
  34.     }
  35.  
  36.     /**
  37.      * Removes a node
  38.      * @param Node $node node to remove
  39.      */
  40.     public function remove($nodeToRemove) {
  41.         $this->detach($nodeToRemove);
  42.     }
  43.  
  44.     /**
  45.      * Returns the number of elements in the linked list
  46.      * @return int
  47.      */
  48.     public function count() {
  49.         $count = 0;
  50.         $p = $this->head->getNext();
  51.         while ($p->getNext() != null) {
  52.             $p = $p->getNext();
  53.             $count++;
  54.         }
  55.         return $count;
  56.     }
  57.  
  58.     /**
  59.      * Adds a node after another
  60.      * @param Node $prev the node after which we want to add (e.g. head, tail or another generic node)
  61.      * @param Node $node the node to add
  62.      */
  63.     private function attach($prev, $node) {
  64.         $node->setPrevious($prev);
  65.         $node->setNext($prev->getNext());
  66.         $node->getNext()->setPrevious($node);
  67.         $node->getPrevious()->setNext($node);
  68.     }
  69.  
  70.     /**
  71.      * Removes a node from the list
  72.      * @param Node $node the node to remove from the list
  73.      */
  74.     private function detach($node) {
  75.         $node->getPrevious()->setNext($node->getNext());
  76.         $node->getNext()->setPrevious($node->getPrevious());
  77.     }
  78.  
  79.     public function __toString() {
  80.         $r = 'LinkedList { ';
  81.         $h = $this->head->getNext();
  82.         while ($h->getNext() != null) {
  83.             $r .= $h->getData() . ', ';
  84.             $h = $h->getNext();
  85.         }
  86.         return $r . '}';
  87.     }
  88.  
  89. }
  90.  
  91. /**
  92.  * Class that represents a node in a doubly linked list
  93.  */
  94. class Node {
  95.     private $data;
  96.     // the next node
  97.     private $next;
  98.     // the previous node
  99.     private $previous;
  100.  
  101.     /**
  102.      * @param string $data the content of the node
  103.      */
  104.     public function __construct($data) {
  105.         $this->data = $data;
  106.     }
  107.  
  108.     /**
  109.      * Sets a new value for the node data
  110.      * @param string the new content of the node
  111.      */
  112.     public function setData($data) {
  113.         $this->data = $data;
  114.     }
  115.  
  116.     /**
  117.      * Sets a node as the next node
  118.      * @param Node $next the next node
  119.      */
  120.     public function setNext(Node $next) {
  121.         $this->next = $next;
  122.     }
  123.  
  124.     /**
  125.      * Sets a node as the previous node
  126.      * @param Node $previous the previous node
  127.      */
  128.     public function setPrevious(Node $previous) {
  129.         $this->previous = $previous;
  130.     }
  131.  
  132.     /**
  133.      * Returns the node data
  134.      * @return mixed the content of the node
  135.      */
  136.     public function getData() {
  137.         return $this->data;
  138.     }
  139.  
  140.     /**
  141.      * Returns the next node
  142.      * @return Node the next node of the node
  143.      */
  144.     public function getNext() {
  145.         return $this->next;
  146.     }
  147.  
  148.     /**
  149.      * Returns the previous node
  150.      * @return Node the previous node of the node
  151.      */
  152.     public function getPrevious() {
  153.         return $this->previous;
  154.     }
  155.  
  156.     public function __toString() {
  157.         return "Node($this->data)";
  158.     }
  159.  
  160. }
  161.  
  162. $foo = new LinkedList();
  163. for ($i = 0; $i < 200000; ++$i) {
  164.     // echo "putting $i\n";
  165.     $foo->addTail($i);
  166. }
  167. echo "start counting", PHP_EOL;
  168. echo "count: ",  $foo->count(), PHP_EOL;
  169. // php crashes during the count()!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement