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