-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueues.js
More file actions
140 lines (109 loc) · 4.19 KB
/
queues.js
File metadata and controls
140 lines (109 loc) · 4.19 KB
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
138
/*
A QUEUE is an ordered collection of items with FIFO output
examples include lines at movies, cafeteria where the last person has to wait for the person infront to be served.
A very popular computer science example is the printing line where one document has to wait for the one infront to be printed
*/
////////////////////////////////////////////////////
//CREATING A QUEUE
//first we create our own class of queue
function Queue() {
//we need a data structure that will store the queue elements
let items = [];
/*
we will use the following methods;
enqueue(element) - this adds a new item at the back of the queue
dequeue() - this removes and returns the first item from the queue
front() = this returns the first element from the queue, the first one added and the first one that will be removed
isEmpty() - this returns true if the queue contains no elements.
size() - this returns the number of elements in the queue. Similar to the .length property
*/
//adding new items to the queue
this.enqueue = function (element) {
items.push(element);
}
//removing first item from the queue
this.dequeue = function () {
return items.shift();
}
//returning an item that is at the front (0 index)
this.front = function () {
return items[0];
}
//verifying if the queue is empty
this.isEmpty = function () {
return items.length === 0;
}
//knowing how many elements are in the queue
this.size = function () {
return items.length;
}
//checking the element in the queue
this.print = function () {
console.log(items.toString());
}
}
//////////////////////////////////////////////////
//to use the queue class we instantiate it
let queue = new Queue();
//we can add elements to our queue
queue.enqueue('John');
queue.enqueue('Jack');
queue.enqueue('camila');
//we can then implement the different method we declared so that we use the queue
///////////////////////////////////////////////////////
//SOLVING ALGORITHMS WITH THE QUEUE DATA STRUCTURE
/*
The PRIORITY QUEUE
there are times when the items are removed according to priority and not position.
*/
//this is how we implement the priority queue class
function PriorityQueue() {
let items = [];
function QueueElement(element, priority) {
this.element = element;
this.priority = priority;
}
//then we add elements according to their priority
this.enqueue = function (element, priority) {
let queueElement = new QueueElement(element, priority);
let added = false;
for (let i = 0; i < items.length; i++) {
if (queueElement.priority < items[i].priority) {
items.splice(i,0,queueElement);
added = true;
break;
}
}
if (!added) {
items.push(queueElement);
}
}
//we can also see the new elements according to their priorities.
this.print = function () {
for (let i = 0; i < items.length; i++) {
console.log(`${items[i].element} - ${items[i].priority}`);
}
}
//we can then add other methods as we did above
}
/////////////////////////////////////////////
//The CIRCULAR QUEUE(The Hot Potato Queue)
/*
This type of queue is similar to the the hop potato game where children are organised in a circle and a hot poptato is passed around. When the hot potato stops being passed around, the child with it is eliminated and the game goes on until only one child is left
*/
//let us implement the simulation of the hot potato game using the queue data structure
function hotPotato(nameList, num) {
let queue = new Queue();
for (let i = 0; i < nameList.length; i++) {
queue.enqueue(nameList[i]);
}
let eliminated = '';
while (queue.size() > 1) {
for (let i = 0; i < num; i++) {
queue.enqueue(queue.dequeue()); //this starts simulating the hot potato game
}
eliminated = queue.dequeue();
console.log(eliminated + ' was eliminated from the hot potato game.');
}
return queue.dequeue();
}