shu*_*nyo 2 oop matlab linked-list data-structures
嗨,我想在Matlab中实现一个链表.
我希望实现的是(这是C等价物):
class Node{
public Node* child;
}
Run Code Online (Sandbox Code Playgroud)
我环顾四周,但似乎没有得到任何接近.
我想你想要实现一个链表:
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
Run Code Online (Sandbox Code Playgroud)
创作:
n1 = Node('Foo'); %Create one node
n2 = Node('Bar'); %Create another one
n1.Next = n2; %Link between them
Run Code Online (Sandbox Code Playgroud)
迭代:
n = n1;
while ~isempty(n)
disp(n.Data); %Change this line as you wish
n = n.Next;
end
Run Code Online (Sandbox Code Playgroud)