Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 11964de

Browse files
committed
Changed queue from object type to array type and adjusted methods to reflect this change
1 parent 42998ac commit 11964de

File tree

1 file changed

+7
-29
lines changed

1 file changed

+7
-29
lines changed

‎Data Structures/Queue/Queue.js‎

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,29 @@
99

1010
var Queue = function() {
1111

12-
//This keeps track of where the end of the queue is
13-
this.back = 0;
1412
//This is the array representation of the queue
15-
this.queue = {};
13+
this.queue = [];
1614

1715
//Add a value to the end of the queue
1816
this.enqueue = function(item) {
19-
this.queue[this.back] = item;
20-
this.back++;
17+
this.queue[this.queue.length] = item;
2118
}
2219

2320
//Removes the value at the front of the queue
2421
this.dequeue = function() {
25-
if (this.back === 0) {
22+
if (this.queue.length === 0) {
2623
return "Queue is Empty";
2724
}
2825

29-
var result = this.queue[this.front];
30-
delete this.queue[this.front];
31-
32-
//Shift all the other items forward
33-
for (var i = 1; i < this.back; i++) {
34-
this.queue[i - 1] = this.queue[i];
35-
}
36-
37-
//clean up the leftover duplicated value at the back of the queue
38-
delete this.queue[this.back];
39-
this.back--;
26+
var result = this.queue[0];
27+
this.queue.splice(0, 1); //remove the item at position 0 from the array
4028

4129
return result;
4230
}
4331

4432
//Return the length of the queue
4533
this.length = function() {
46-
return this.back;
34+
return this.queue.length;
4735
}
4836

4937
//Return the item at the front of the queue
@@ -53,17 +41,7 @@ var Queue = function() {
5341

5442
//List all the items in the queue
5543
this.view = function() {
56-
var str = "{"
57-
//construct a single string to represent the items in the queue
58-
for (var i = 0; i < this.back; i++) {
59-
str += this.queue[i];
60-
if (i !== this.back - 1) {
61-
str += ", ";
62-
}
63-
}
64-
str += "}";
65-
66-
console.log(str);
44+
console.log(this.queue);
6745
}
6846
}
6947

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /