递归调用类(实现链表)



嗨,我正在尝试在Matlab中实现一个链表。

我希望实现的是(这相当于C):

class Node{
    public Node* child;
}

我环顾四周,但似乎什么也没找到。

我猜您想要实现一个链表:

   classdef Node < handle
       properties
          Next = Node.empty(0); %Better than [] because it has same type as Node.
          Data;
       end         
       methods
           function this = Node(data)
               if ~exist('data','var') 
                  data = [];
               end
               this.Data = data;
           end
       end
   end

创建:

   n1 = Node('Foo'); %Create one node
   n2 = Node('Bar'); %Create another one
   n1.Next = n2; %Link between them

迭代:

   n = n1;
   while ~isempty(n)
       disp(n.Data);   %Change this line as you wish
       n = n.Next;           
   end

相关内容

  • 没有找到相关文章

最新更新