-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.m
95 lines (82 loc) · 2.29 KB
/
queue.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
classdef queue < handle
properties
capacity;
s;
e;
end
properties (GetAccess = private)
q;
end
methods
function obj = queue(N)
obj.capacity = N;
obj.s=2;
obj.e=1;
obj.q=zeros(1,N);
end
function empty_criterion = isEmpty(obj)
empty_criterion = (obj.s==(obj.e+1));
end
function empty(obj)
obj.s = obj.e+1;
end
function full_criterion = isFull(obj)
full_criterion = (getSize(obj)==obj.capacity);
end
function size = getSize(obj)
if isEmpty(obj)
size=0;
else
size = (obj.e-obj.s+1);
end
end
function defragment(obj)
tmp = zeros(1,obj.capacity);
tmp(1:1:getSize(obj)) = obj.q(obj.s:1:obj.e);
obj.e = obj.e-obj.s + 1;
obj.s=1;
obj.q = tmp;
end
function add(obj,elem)
if isEmpty(obj)
obj.s=1;
obj.e=1;
obj.q(obj.e)=elem;
elseif ~isFull(obj)
if obj.e==obj.capacity
defragment(obj);
end
obj.q(obj.e+1)=elem;
obj.e=obj.e+1;
end
end
function elem = remove(obj,elem)
if ~isEmpty(obj)
output = obj.q(obj.s);
obj.s=obj.s+1;
elem=output;
end
end
function decision = exists(obj,elem)
if ~isEmpty(obj)
decision = sum(ismember(obj.q(obj.s:obj.e),elem));
else
decision=0;
end
end
function output = get(obj)
if ~isEmpty(obj)
output = obj.q(obj.s: obj.e);
else
output = NaN;
end
end
function output = history(obj)
if ~isEmpty(obj)
output = obj.q(obj.e:-1:1);
else
output = NaN;
end
end
end
end