1+ classdef CQueue < handle
2+ % CQueue define a queue data strcuture
3+ %
4+ % It likes java.util.Queue, however, it could use CQueue.content() to
5+ % return all the data (in cells) of the Queue, and it is a litter faster
6+ % than java's Queue.
7+ %
8+ % q = CQueue(c); c is a cells, and could be omitted
9+ % s.size() return the numble of element
10+ % s.isempty() return true when the queue is empty
11+ % s.empty() delete all the elements in the queue.
12+ % s.push(el) push el to the top of qeueu
13+ % s.pop() pop out the the beginning of queue, and return the element
14+ % s.front() return the the the beginning element of the qeueu
15+ % s.back() return the the the rear element of the qeueu
16+ % s.remove() remove all data from the queue
17+ % s.content() return all the data of the queue (in the form of a
18+ % cells with size [s.size(), 1]
19+ %
20+ % See also CList, CStack
21+ %
22+ % 定义了一个队列
23+ % q = CQueue; 定义一个空的队列对象
24+ % q = CQueue(c); 定义队列对象,并用c初始化q,当c为cell时,c的元素为栈的数据,
25+ % 否则c本身为栈的第一个数据
26+ %
27+ % 支持操作:
28+ % sz = q.size() 返回队列内元素个数,也可用来判断队列是否非空。
29+ % q.isempty() 用来判断队列为空
30+ % q.empty() 清空队列
31+ % q.push(el) 将新元素el压入队列内
32+ % s.pop() 弹出队首元素,用户需自己确保队列非空
33+ % el = q.front() 返回队首元素,用户需自己确保队列非空
34+ % el = q.back() 返回队尾元素,用户需自己确保队列非空
35+ % q.remove() 清空队列
36+ % q.content() 按顺序返回q的数据,为一个cell数组
37+ %
38+ % See also CStack, CList
39+ %
40+ % Copyright: zhang@zhiqiang.org, 2010.
41+ % url: http://zhiqiang.org/blog/it/matlab-data-structures.html
42+
43+ properties (Access = private )
44+ buffer % a cell, to maintain the data
45+ beg % the start position of the queue
46+ rear % the end position of the queue
47+ % the actually data is buffer(beg:rear-1)
48+ end
49+
50+ properties (Access = public )
51+ capacity % 栈的容量,当容量不够时,容量扩充为2倍。
52+ end
53+
54+ methods
55+ function obj = CQueue(c ) % 初始化
56+ if nargin >= 1 && iscell(c )
57+ obj.buffer = [c(: ); cell(numel(c ), 1 )];
58+ obj.beg = 1 ;
59+ obj.rear = numel(c ) + 1 ;
60+ obj.capacity = 2 * numel(c );
61+ elseif nargin >= 1
62+ obj.buffer = cell(100 , 1 );
63+ obj.buffer{1 } = c ;
64+ obj.beg = 1 ;
65+ obj.rear = 2 ;
66+ obj.capacity = 100 ;
67+ else
68+ obj.buffer = cell(100 , 1 );
69+ obj.capacity = 100 ;
70+ obj.beg = 1 ;
71+ obj.rear = 1 ;
72+ end
73+ end
74+
75+ function s = size(obj ) % 队列长度
76+ if obj .rear >= obj .beg
77+ s = obj .rear - obj .beg ;
78+ else
79+ s = obj .rear - obj .beg + obj .capacity ;
80+ end
81+ end
82+
83+ function b = isempty(obj ) % return true when the queue is empty
84+ b = ~logical(obj .size());
85+ end
86+
87+ function s = empty(obj ) % clear all the data in the queue
88+ s = obj .size();
89+ obj.beg = 1 ;
90+ obj.rear = 1 ;
91+ end
92+
93+ function push(obj , el ) % 压入新元素到队尾
94+ if obj .size >= obj .capacity - 1
95+ sz = obj .size();
96+ if obj .rear >= obj .front
97+ obj .buffer(1 : sz ) = obj .buffer(obj .beg : obj .rear - 1 );
98+ else
99+ obj .buffer(1 : sz ) = obj .buffer([obj .beg : obj .capacity 1 : obj .rear - 1 ]);
100+ end
101+ obj .buffer(sz + 1 : obj .capacity * 2 ) = cell(obj .capacity * 2 - sz , 1 );
102+ obj.capacity = numel(obj .buffer );
103+ obj.beg = 1 ;
104+ obj.rear = sz + 1 ;
105+ end
106+ obj.buffer{obj .rear } = el ;
107+ obj.rear = mod(obj .rear , obj .capacity ) + 1 ;
108+ end
109+
110+ function el = front(obj ) % 返回队首元素
111+ if obj .rear ~= obj .beg
112+ el = obj.buffer{obj .beg };
113+ else
114+ el = [];
115+ warning(' CQueue:NO_DATA' , ' try to get data from an empty queue' );
116+ end
117+ end
118+
119+ function el = back(obj ) % 返回队尾元素
120+
121+ if obj .rear == obj .beg
122+ el = [];
123+ warning(' CQueue:NO_DATA' , ' try to get data from an empty queue' );
124+ else
125+ if obj .rear == 1
126+ el = obj.buffer{obj .capacity };
127+ else
128+ el = obj.buffer{obj .rear - 1 };
129+ end
130+ end
131+
132+ end
133+
134+ function el = pop(obj ) % 弹出队首元素
135+ if obj .rear == obj .beg
136+ error(' CQueue:NO_Data' , ' Trying to pop an empty queue' );
137+ else
138+ el = obj.buffer{obj .beg };
139+ obj.beg = obj .beg + 1 ;
140+ if obj .beg > obj .capacity , obj.beg = 1 ; end
141+ end
142+ end
143+
144+ function remove(obj ) % 清空队列
145+ obj.beg = 1 ;
146+ obj.rear = 1 ;
147+ end
148+
149+ function display(obj ) % 显示队列
150+ if obj .size()
151+ if obj .beg <= obj .rear
152+ for i = obj .beg : obj .rear - 1
153+ disp([num2str(i - obj .beg + 1 ) ' -th element of the stack:' ]);
154+ disp(obj.buffer{i });
155+ end
156+ else
157+ for i = obj .beg : obj .capacity
158+ disp([num2str(i - obj .beg + 1 ) ' -th element of the stack:' ]);
159+ disp(obj.buffer{i });
160+ end
161+ for i = 1 : obj .rear - 1
162+ disp([num2str(i + obj .capacity - obj .beg + 1 ) ' -th element of the stack:' ]);
163+ disp(obj.buffer{i });
164+ end
165+ end
166+ else
167+ disp(' The queue is empty' );
168+ end
169+ end
170+
171+ function c = content(obj ) % 取出队列元素
172+ if obj .rear >= obj .beg
173+ c = obj .buffer(obj .beg : obj .rear - 1 );
174+ else
175+ c = obj .buffer([obj .beg : obj .capacity 1 : obj .rear - 1 ]);
176+ end
177+ end
178+ end
179+ end
0 commit comments