dpkk8687 2011-12-12 03:21
浏览 49
已采纳

如何在PHP中实现双向链表?

I got an interview question on Friday and I think I flunked it. The question was:

Write a class that process a double linked list in PHP.

I understand the concept, and here's the code I gave:

class element {
 private $current;
 public function __construct($e) {
  $this->current = $e;
 }
 // method
 // etc..
}

class doublelist
{
  private $prev;
  private $next;
  private $current;
  private $list;
  public function add(element $e) {
   if($this->current == NULL) {
    $this->prev = $this->current;
   }
   $this->current = $e;
  }
}

$list = new doublelist();
$list->add(new element('a'));
$list->add(new element('b'));

This works initially, but if I add a second element I "lose" the first one, and I don't understand why.

  • 写回答

2条回答 默认 最新

  • dongtan2017 2011-12-12 03:26
    关注

    You need to be keeping track of $prev and $next on the elements, not the list. If you want to make it transparent, you can wrap each element in a bean that has pointers to the next and previous ones, or just make element have those by definition.

    The way you're doing it right now, the list will only know which one is the current element, and which one came before that. But what you should really be doing is figuring it out from the element (or bean) which one will be the next or previous.

    Edit

    Since this question has been getting the occasional view, I thought I'd add a little code to help explain this better.

    class DoublyLinkedList {
        private $start = null;
        private $end = null;
    
        public function add(Element $element) {
            //if this is the first element we've added, we need to set the start
            //and end to this one element
            if($this->start === null) {
                $this->start = $element;
                $this->end = $element;
                return;
            }
    
            //there were elements already, so we need to point the end of our list
            //to this new element and make the new one the end
            $this->end->setNext($element);
            $element->setPrevious($this->end);
            $this->end = $element;
        }
    
        public function getStart() {
            return $this->start;
        }
    
        public function getEnd() {
            return $this->end;
        }
    }
    
    class Element {
        private $prev;
        private $next;
        private $data;
    
        public function __construct($data) {
            $this->data = $data;
        }
    
        public function setPrevious(Element $element) {
            $this->prev = $element;
        }
    
        public function setNext(Element $element) {
            $this->next = $element;
        }
    
        public function setData($data) {
            $this->data = $data;
        }
    }
    

    There are, of course, other methods you could add; and if anyone is interested in those I can add them as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器