递归调用类(实现链表)

时间:2012-02-20 21:33:27

标签: oop matlab data-structures linked-list

您好我正在尝试在Matlab中实现链接列表。

我希望实现的是(这是C等价物):

class Node{
    public Node* child;
}

我环顾四周但似乎没有得到任何接近的东西。

1 个答案:

答案 0 :(得分:6)

我想你想要实现一个链表:

   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