Matlab 类实例 队列的实现

    技术2022-07-12  74

    虽然从政治正确的角度看,是不推荐使用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; else obj.buffer = cell(100, 1); obj.capacity = 100; obj.beg = 1; obj.rear = 1; end end function s = size(obj) if obj.rear >= obj.beg s = obj.rear - obj.beg; else s = obj.rear - obj.beg + obj.capacity; end end function b = isempty(obj) % return true when the queue is empty b = ~logical(obj.size()); end function s = empty(obj) % clear all the data in the queue s = obj.size(); obj.beg = 1; obj.rear = 1; end function push(obj, el) if obj.size >= obj.capacity - 1 sz = obj.size(); if obj.rear >= obj.beg obj.buffer(1:sz) = obj.buffer(obj.beg:obj.rear-1); else obj.buffer(1:sz) = obj.buffer([obj.beg:obj.capacity 1:obj.rear-1]); end obj.buffer(sz+1:obj.capacity*2) = cell(obj.capacity*2-sz, 1); obj.capacity = numel(obj.buffer); obj.beg = 1; obj.rear = sz+1; end obj.buffer{obj.rear} = el; obj.rear = mod(obj.rear, obj.capacity) + 1; end function el = front(obj) if obj.rear ~= obj.beg el = obj.buffer{obj.beg}; else el = []; warning('CQueue:NO_DATA', 'try to get data from an empty queue'); end end function el = back(obj) if obj.rear == obj.beg el = []; warning('CQueue:NO_DATA', 'try to get data from an empty queue'); else if obj.rear == 1 el = obj.buffer{obj.capacity}; else el = obj.buffer{obj.rear - 1}; end end end function el = pop(obj) if obj.rear == obj.beg error('CQueue:NO_Data', 'Trying to pop an empty queue'); else el = obj.buffer{obj.beg}; obj.beg = obj.beg + 1; if obj.beg > obj.capacity, obj.beg = 1; end end end function remove(obj) obj.beg = 1; obj.rear = 1; end function display(obj) if obj.size() if obj.beg <= obj.rear for i = obj.beg : obj.rear-1 disp([num2str(i - obj.beg + 1) '-th element of the stack:']); disp(obj.buffer{i}); end else for i = obj.beg : obj.capacity disp([num2str(i - obj.beg + 1) '-th element of the stack:']); disp(obj.buffer{i}); end for i = 1 : obj.rear-1 disp([num2str(i + obj.capacity - obj.beg + 1) '-th element of the stack:']); disp(obj.buffer{i}); end end else disp('The queue is empty'); end end function c = content(obj) if obj.rear >= obj.beg c = obj.buffer(obj.beg:obj.rear-1); else c = obj.buffer([obj.beg:obj.capacity 1:obj.rear-1]); end end end end

    使用

    参考链接

    [1] Is there a “queue” in MATLAB? - statckoverflow

    https://stackoverflow.com/questions/4142190/is-there-a-queue-in-matlab

    [2] MATLAB 面向对象编程(一)~(五)

    https://blog.csdn.net/qq_43157190/article/details/104039235

    [3] 类的结构说明

    https://blog.csdn.net/qinze5857/article/details/80545885

    [4] MATLAB的句柄类和实体值类

    https://blog.csdn.net/wuliangj/article/details/106987637

    Processed: 0.009, SQL: 10