Skip to content

Commit c07d3d6

Browse files
author
Wakidur Rahaman
committed
fix(queue-28): init file added
1 parent b689970 commit c07d3d6

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

data-structure/linked-list/LinkedList.js

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Init

javascript-today-magazine/queue-data-structure.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
2-
* A Queue is a linear structure which follows a particular order in which the operations are performed.
3-
* The order is First In First Out (FIFO).
2+
* In computer science, a queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principle (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. Often a peek or front operation is also entered, returning the value of the front element without dequeuing it. A queue is an example of a linear data structure, or more abstractly a sequential collection.
43
* A good example of a queue would simply be a line at a grocery store. The customer in line (queue) first, will leave first.
54
*
65
* Here's a JavaScript implementation of a Queue data structure. 🔥

utils/comparator.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export default class Comparator {
2+
/**
3+
* Constructor.
4+
* @param {function(a: *, b: *)} [compareFunction] - It may be custom compare function that, let's
5+
* say may compare custom objects together.
6+
*/
7+
8+
constructor(compareFunction) {
9+
this.compare = compareFunction || Comparator.defaultCompareFunction;
10+
}
11+
12+
/**
13+
* Default comparison function. It just assumes that 'a' and 'b' are strings or numbers.
14+
* @param {(string|number)} a
15+
* @param {(string|number)} b
16+
* @returns {number}
17+
*/
18+
static defaultCompareFunction(a, b) {
19+
if (a === b) {
20+
return 0;
21+
}
22+
23+
return a < b ? -1 : 1;
24+
}
25+
}

utils/comparator.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export default class Comparator {
2+
[x: string]: (a: any, b: any) => 0 | 1 | -1;
3+
/**
4+
* Constructor.
5+
* @param {function(a: *, b: *)} [compareFunction] - It may be custom compare function that, let's
6+
* say may compare custom objects together.
7+
*/
8+
9+
constructor(compareFunction: (a: any, b: any) => 0 | 1 | -1) {
10+
this.compare = compareFunction || Comparator.defaultCompareFunction;
11+
}
12+
13+
/**
14+
* Default comparison function. It just assumes that 'a' and 'b' are strings or numbers.
15+
* @param {(string|number)} a
16+
* @param {(string|number)} b
17+
* @returns {number}
18+
*/
19+
static defaultCompareFunction(a: number, b: number) {
20+
if (a === b) {
21+
return 0;
22+
}
23+
24+
return a < b ? -1 : 1;
25+
}
26+
}
27+

0 commit comments

Comments
 (0)