Skip to content
This repository was archived by the owner on Aug 1, 2019. It is now read-only.

Commit 3ecb25d

Browse files
committed
add ArrayQueue
1 parent aaa7e16 commit 3ecb25d

File tree

8 files changed

+69
-78
lines changed

8 files changed

+69
-78
lines changed

dataStructure/ArrayQueue.js

Lines changed: 0 additions & 76 deletions
This file was deleted.

dataStructure/Queue/ArrayQueue.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class ArrayQueue {
2+
constructor(capacity) {
3+
this._arr = Array(capacity || 100);
4+
this._size = 0;
5+
this._front = -1;
6+
}
7+
8+
enqueue(data) {
9+
if (this.isFull()) {
10+
throw "Queue is full";
11+
} else {
12+
let position = (this._front + this._size) % this._arr.length;
13+
this._arr[position] = data;
14+
++this._size;
15+
}
16+
}
17+
18+
dequeue() {
19+
if (this.isEmpty()) {
20+
throw "Queue is Empty";
21+
} else {
22+
let data = this._arr[this._front];
23+
this._front = (this._front + 1) % this._arr.length;
24+
--this._size;
25+
return data;
26+
}
27+
}
28+
29+
front() {
30+
if (this.isEmpty()) {
31+
throw new Error("Queue is empty");
32+
} else {
33+
return this._arr[this._front];
34+
}
35+
}
36+
37+
isEmpty() {
38+
return this._size === 0;
39+
}
40+
41+
isFull() {
42+
return this._size === this._arr.length;
43+
}
44+
45+
size() {
46+
return this._size;
47+
}
48+
}
49+
50+
// main
51+
52+
// const q = new ArrayQueue(10);
53+
// q.enqueue(1);
54+
// q.enqueue(5);
55+
// q.enqueue(10);
56+
// q.enqueue(3);
57+
58+
// console.log(q.front());
59+
// console.log(q.dequeue());
60+
// console.log(q.dequeue());
61+
// q.enqueue(50);
62+
// console.log(q.dequeue());
63+
// console.log(q.dequeue());
64+
// console.log(q.size());
65+
// console.log(q.front());
66+
// console.log(q.dequeue());
67+
// console.log(q.isEmpty());

dataStructure/LinkedListQueue.js renamed to dataStructure/Queue/LinkedListQueue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const LinkedList = require("./SinglyLinkedList2"); // enhanced LinkedList with addBack in O(1)
1+
const LinkedList = require("../LinkedList/SinglyLinkedList2"); // enhanced LinkedList with addBack in O(1)
22

33
class Queue {
44
constructor() {
File renamed without changes.

dataStructure/LinkedListStack.js renamed to dataStructure/Stack/LinkedListStack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const LinkedList = require("./SinglyLinkedList");
1+
const LinkedList = require("../LinkedList/SinglyLinkedList");
22

33
class Stack {
44
constructor() {

0 commit comments

Comments
 (0)