Implement linked list in php

后端 未结 8 698
小蘑菇
小蘑菇 2020-12-08 05:22

How should I implement a linked list in PHP? Is there a implementation built in into PHP?

I need to do a lot of insert and delete operations, and at same time I need

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 05:47

    // Here's a basic implementation of SplDoublyLinkedList using PHP.
    $splDoubleLinkedList = new SplDoublyLinkedList();
    $splDoubleLinkedList->push('a');
    $splDoubleLinkedList->push('3');
    $splDoubleLinkedList->push('v');
    $splDoubleLinkedList->push('1');
    $splDoubleLinkedList->push('p');
    // $splDoubleLinkedList->unshift('10');
    // $splDoubleLinkedList->pop();
    $splDoubleLinkedList->add(3, 3.0);
    // First of all, we need to rewind list.
    $splDoubleLinkedList->rewind();
    // Use while, check if the list has valid node.
    while($splDoubleLinkedList->valid()){
      // Print current node's value.
       echo $splDoubleLinkedList->current()."\n";
     // Turn the cursor to next node.
     $splDoubleLinkedList->next();
    }
    

提交回复
热议问题