Matlab 类实例 队列的实现

本文介绍了一种在Matlab中实现队列数据结构的方法,通过面向对象编程解决Matlab官方库中缺乏队列的问题。文章提供了详细的代码示例,包括队列的基本操作如入队、出队等。

虽然从政治正确的角度看,是不推荐使用Matlab了。无奈老代码已经在Matlab上存活太久了。

本文是为了解决Matlab官方库中没有队列数据结构,故从社区中找了段代码。这也是自己首次在Matlab中使用面向对象的编程。语法说明见Matlab官方文档和本文最后的参考链接。

程序

classdef myQueue <handle  
properties (Access = public)%private
    buffer      % a cell, to maintain the data
    beg         % the start position of the queue
    rear        % the end position of the queue
                % the actually data is buffer(beg:rear-1)
end

properties (Access = public)
    capacity    % ص»µؤبفء؟£¬µ±بفء؟²»¹»ت±£¬بفء؟ہ©³نخھ2±¶،£
end

methods
    function obj = myQueue(c) 
        if nargin >= 1 && iscell(c)
            obj.buffer = [c(:); cell(numel(c), 1)];% numel - Number of array elements
            obj.beg = 1;
            obj.rear = numel(c) + 1;
            obj.capacity = 2*numel(c);
        elseif nargin >= 1
            obj.buffer = cell(100, 1);
            obj.buffer{1} = c;
            obj.beg = 1;
            obj.rear = 2;
            obj.capacity = 100; 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值