递归调用类(实现链表)

发布于 2025-01-07 15:05:30 字数 157 浏览 0 评论 0原文

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

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

class Node{
    public Node* child;
}

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

Hi I am trying to implement a linked list in Matlab.

The implementation I am hoping to do is (this is the C equivalent):

class Node{
    public Node* child;
}

I have looked around but don't seem to get anything quite close.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没结果 2025-01-14 15:05:30

我猜你想实现一个链表:

   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

I guess that you want to implement a linked list:

   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

Creation :

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

Iteration:

   n = n1;
   while ~isempty(n)
       disp(n.Data);   %Change this line as you wish
       n = n.Next;           
   end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文