虽然从政治正确的角度看,是不推荐使用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;

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

被折叠的 条评论
为什么被折叠?



