您好我正在尝试在Matlab中实现链接列表。
我希望实现的是(这是C等价物):
class Node{
public Node* child;
}
我环顾四周但似乎没有得到任何接近的东西。
答案 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