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 417287a

Browse files
Queues
1 parent 9276de7 commit 417287a

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed

‎#5 - Queue/0-queue-implementation.js‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Basic Queue Implementation
2+
3+
class Queue {
4+
constructor() {
5+
this.queue = [];
6+
}
7+
8+
enqueue(element) {
9+
this.queue.push(element);
10+
}
11+
12+
dequeue() {
13+
if (this.isEmpty()) {
14+
return "Underflow, cannot perform dequeue";
15+
}
16+
17+
return this.queue.shift();
18+
}
19+
20+
isEmpty() {
21+
return this.size() === 0;
22+
}
23+
24+
front() {
25+
if (this.isEmpty()) {
26+
return "No Elements in the Queue";
27+
}
28+
29+
return this.queue[0];
30+
}
31+
32+
size() {
33+
return this.queue.length;
34+
}
35+
36+
printQueue() {
37+
let queueString = "";
38+
for (let i = 0; i < this.size(); i++) {
39+
queueString += this.queue[i] + ", ";
40+
}
41+
42+
console.log("Queue: " + queueString);
43+
}
44+
}
45+
46+
const myQueue = new Queue();
47+
48+
myQueue.enqueue(5);
49+
myQueue.enqueue(96);
50+
myQueue.enqueue(786);
51+
52+
myQueue.dequeue();
53+
myQueue.dequeue();
54+
myQueue.dequeue();
55+
console.log(myQueue.dequeue());
56+
console.log(myQueue.front());

‎#5 - Queue/1-circular-queue.js‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Ques 1 : Circular Queue Implementation
2+
// Design your implementation of the circular queue. The circular queue is a
3+
// linear data structure in which the operations are performed based on First In First Out
4+
// principle, and the last position is connected back to the first position to make a circle.
5+
6+
var MyCircularQueue = function (k) {
7+
this.queue = [];
8+
this.size = k;
9+
};
10+
11+
MyCircularQueue.prototype.enQueue = function (value) {
12+
if (this.size === this.queue.length) return false;
13+
this.queue.push(value);
14+
return true;
15+
};
16+
17+
MyCircularQueue.prototype.deQueue = function () {
18+
if (this.queue.length === 0) return false;
19+
this.queue.shift();
20+
return true;
21+
};
22+
23+
MyCircularQueue.prototype.Front = function () {
24+
if (this.queue.length === 0) return -1;
25+
return this.queue[0];
26+
};
27+
28+
MyCircularQueue.prototype.Rear = function () {
29+
if (this.queue.length === 0) return -1;
30+
return this.queue[this.queue.length - 1];
31+
};
32+
33+
MyCircularQueue.prototype.isEmpty = function () {
34+
return this.queue.length === 0;
35+
};
36+
37+
MyCircularQueue.prototype.isFull = function () {
38+
return this.size === this.queue.length;
39+
};
40+
41+
// [2,5,72]
42+
43+
var obj = new MyCircularQueue(3);
44+
obj.enQueue(1);
45+
obj.enQueue(4);
46+
obj.enQueue(2);
47+
obj.deQueue();
48+
obj.enQueue(5);
49+
obj.deQueue();
50+
obj.enQueue(72);
51+
52+
console.log(obj.Front(), obj.Rear());
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Ques 2 : Implement Stack using Queues
2+
// Implement a last -in -first - out(LIFO) stack using only two queues.
3+
// The implemented stack should support all the functions of a stack (push, top, pop, and empty).
4+
5+
var MyStack = function () {
6+
this.q1 = [];
7+
this.q2 = [];
8+
};
9+
10+
// q1 - [4,2,3,5]
11+
// q2 - []
12+
13+
MyStack.prototype.push = function (x) {
14+
while (this.q1.length !== 0) {
15+
this.q2.push(this.q1.shift());
16+
}
17+
this.q1.push(x);
18+
while (this.q2.length !== 0) {
19+
this.q1.push(this.q2.shift());
20+
}
21+
};
22+
23+
MyStack.prototype.pop = function () {
24+
return this.q1.shift();
25+
};
26+
27+
MyStack.prototype.top = function () {
28+
return this.q1[0];
29+
};
30+
31+
MyStack.prototype.empty = function () {
32+
return this.q1.length === 0;
33+
};
34+
35+
var stack = new MyStack();
36+
stack.push(3);
37+
stack.push(5);
38+
stack.push(96);
39+
stack.push(24);
40+
stack.pop();
41+
console.log(stack.top());
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Ques 3 : Implement Queue using Stacks
2+
// Implement a first in first out(FIFO) queue using only two stacks.
3+
// The implemented queue should have all functions of queue(enqueue, front, dequeue, and empty).
4+
5+
var MyQueue = function () {
6+
this.stack1 = [];
7+
this.stack2 = [];
8+
};
9+
10+
MyQueue.prototype.enqueue = function (x) {
11+
this.stack1.push(x);
12+
};
13+
14+
// stack1 = [9,10]
15+
// stack2 = []
16+
17+
MyQueue.prototype.dequeue = function () {
18+
if (this.stack2.length === 0) {
19+
while (this.stack1.length > 0) {
20+
this.stack2.push(this.stack1.pop());
21+
}
22+
}
23+
24+
return this.stack2.pop();
25+
};
26+
27+
MyQueue.prototype.front = function () {
28+
if (this.stack2.length === 0) {
29+
while (this.stack1.length > 0) {
30+
this.stack2.push(this.stack1.pop());
31+
}
32+
}
33+
34+
return this.stack2[this.stack2.length - 1];
35+
};
36+
37+
MyQueue.prototype.empty = function () {
38+
return this.stack1.length === 0 && this.stack2.length === 0;
39+
};
40+
41+
[3, 6, 7];
42+
43+
const queue = new MyQueue();
44+
queue.enqueue(3);
45+
queue.enqueue(6);
46+
queue.enqueue(7);
47+
queue.dequeue();
48+
console.log(queue.front());

0 commit comments

Comments
(0)

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