-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularQueue.js
137 lines (128 loc) · 2.58 KB
/
circularQueue.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// require('stack.js')
function circleQueue(n){
this.head = -1;
this.tail = -1;
this.dataStore = [];
this.capacity = n;
}
circleQueue.prototype.enqueue = function(value){
if(this.isFull()){
console.log('Cannot enqueue ' + value + '. Queue is FULL.');
return null;
}
if(this.head == -1 && this.tail == -1){
this.head = 0;
this.tail = 0;
}
else if(this.tail == this.capacity - 1){
this.tail = 0;
} else {
this.tail++;
}
this.dataStore[this.tail] = value;
return this;
}
circleQueue.prototype.dequeue = function(){
let output = this.dataStore[this.head];
this.dataStore[this.head] = null;
if(this.tail == this.head){
this.head = -1;
this.tail = -1;
}
else if(this.head == this.capacity - 1){
this.head = 0;
} else {
this.head++;
}
return output;
}
circleQueue.prototype.isFull = function(){
if((this.head === 0 && this.tail === this.capacity - 1) || (this.head === this.tail + 1)){
return true;
} else {
return false;
}
}
circleQueue.prototype.isEmpty = function(){
if(this.head == -1 && this.tail == -1){
return true;
} else {
return false;
}
}
circleQueue.prototype.grow = function(size){
let end = this.capacity;
this.capacity = size;
for(let i = 0; i < this.head; i++){
let node = this.dataStore[i];
if(node){
this.dataStore[end] = node;
this.dataStore[i] = null;
if(end == this.capacity - 1){
end = 0;
} else {
end++;
}
}
}
if(end == 0){
this.tail = end
} else {
this.tail = end - 1;
}
}
circleQueue.prototype.reorderAbsolute = function(){
let stack = [];
let tail = this.tail;
while(this.head != tail){
let node = this.dataStore[this.head];
if(node >= 0){
this.enqueue(this.dequeue());
} else {
stack.push(this.dequeue());
}
}
if(this.dataStore[this.head] >= 0){
this.enqueue(this.dequeue());
} else {
stack.push(this.dequeue())
}
if(stack.length == 0){
return this;
}
while(stack.length != 0){
this.enqueue(stack.pop());
}
while(this.dataStore[this.head] > 0){
this.enqueue(this.dequeue());
}
return this;
}
circleQueue.prototype.display = function(){
for(let i = this.head; i != this.tail; i++){
if(i == this.dataStore.length){
i = 0;
}
console.log(this.dataStore[i])
}
console.log(this.dataStore[this.tail])
}
var cq = new circleQueue(7);
cq.enqueue(-10);
cq.enqueue(20);
cq.enqueue(-20);
cq.enqueue(-30);
cq.enqueue(40);
cq.enqueue(-40);
cq.dequeue()
cq.enqueue(-50);
cq.enqueue(-50);
cq.enqueue(-50);
console.log('Input Queue:')
console.log(cq);
console.log('Reordering...')
cq.grow(20);
console.log('Output Queue:')
console.log(cq);
console.log('Displaying Values:')
cq.display();