Skip to content

Commit 3797f27

Browse files
author
Trevor Sears
committed
Completely reworked queue with doubly linked list implementation.
1 parent 6b50565 commit 3797f27

File tree

3 files changed

+83
-32
lines changed

3 files changed

+83
-32
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
"url": "https://github.com/jsdsl/queue/issues"
3131
},
3232
"homepage": "https://github.com/jsdsl/queue#readme",
33+
"dependencies": {
34+
"@jsdsl/doubly-linked-list": "^0.3.0"
35+
},
3336
"devDependencies": {
3437
"typescript": "latest",
3538
"@types/jest": "latest",

ts/main.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
/**
2424
* NPM main class used for exporting this package's contents.
25-
*
26-
* @author Trevor Sears <trevorsears.main@gmail.com>
27-
* @version v0.1.0
25+
*
26+
* @author Trevor Sears <trevor@trevorsears.com> (https://trevorsears.com/)
27+
* @version v1.0.0
2828
* @since v0.1.0
2929
*/
3030

31-
export { Queue } from "./queue";
31+
export { Queue } from "./queue";

ts/queue.ts

Lines changed: 76 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,88 +20,136 @@
2020
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2121
*/
2222

23+
import { DoublyLinkedList } from "@jsdsl/doubly-linked-list";
24+
2325
/**
24-
* A queue/FIFO data structure.
25-
*
26-
* @author Trevor Sears <trevorsears.main@gmail.com>
27-
* @version v0.1.0
26+
* A queue (FIFO) implementation written in TypeScript.
27+
*
28+
* @author Trevor Sears <trevor@trevorsears.com> (https://trevorsears.com/)
29+
* @version v1.0.0
2830
* @since v0.1.0
2931
*/
3032
export class Queue<E = any> {
3133

32-
private internalArray: E[] = [];
34+
/**
35+
* The internal doubly linked list data structure that is used to maintain this queue.
36+
*/
37+
private internalDLL: DoublyLinkedList<E>;
3338

39+
/**
40+
* Initializes a new Queue with the provided elements.
41+
*
42+
* @param {E} elements An optional list of elements to enqueue into this queue.
43+
*/
3444
public constructor(...elements: E[]) {
3545

46+
this.internalDLL = new DoublyLinkedList<E>();
47+
3648
this.enqueue(...elements);
3749

3850
}
3951

52+
/**
53+
* Inserts an element at the start of this queue.
54+
*
55+
* @param {E} elements The element to insert into this queue.
56+
*/
4057
public enqueue(...elements: E[]): void {
4158

42-
this.internalArray.push(...elements);
59+
for (let element of elements) this.internalDLL.insertFirst(element);
4360

4461
}
4562

63+
/**
64+
* Returns and removes the element at the end of this queue if such an element exists, otherwise returning
65+
* undefined.
66+
*
67+
* @returns {E | undefined} The element at the end of this queue if such an element exists, otherwise undefined.
68+
*/
4669
public dequeue(): E | undefined {
4770

48-
return this.internalArray.shift();
71+
return this.internalDLL.removeLast();
4972

5073
}
5174

75+
/**
76+
* Returns the element at the end of this queue (without removing it) if such an element exists, otherwise returning
77+
* undefined.
78+
*
79+
* @returns {E | undefined} The element at the end of this queue if such an element exists, otherwise undefined.
80+
*/
5281
public peek(): E | undefined {
5382

54-
return this.internalArray[0];
83+
return this.internalDLL.getLast();
5584

5685
}
5786

87+
/**
88+
* Randomly reorders the elements of this queue over the number of specified iterations, defaulting to 1 iteration.
89+
*
90+
* @param {number} iterations The number of iterations over which to shuffle the elements of this queue.
91+
*/
5892
public shuffle(iterations: number = 1): void {
5993

60-
for (let count: number = 0; count < iterations; count++) {
61-
62-
let elements: E[] = this.toArray();
63-
this.clear();
64-
65-
while (elements.length !== 0) {
66-
67-
let random: number = Math.floor(Math.random() * elements.length);
68-
let element: E = elements.splice(random, 1)[0];
69-
this.enqueue(element);
70-
71-
}
72-
73-
}
94+
this.internalDLL.shuffle(iterations);
7495

7596
}
7697

98+
/**
99+
* Returns an integer representative of the number of elements present in this queue.
100+
*
101+
* @returns {number} An integer representative of the number of elements present in this queue.
102+
*/
77103
public size(): number {
78104

79-
return this.internalArray.length;
105+
return this.internalDLL.size();
80106

81107
}
82108

109+
/**
110+
* Returns true if this queue contains no elements.
111+
*
112+
* @returns {boolean} true if this queue contains no elements.
113+
*/
83114
public isEmpty(): boolean {
84115

85-
return (this.size() === 0);
116+
return this.internalDLL.isEmpty();
86117

87118
}
88119

120+
/**
121+
* Returns true if this queue contains the specified element.
122+
*
123+
* @param {E} element The element to search for in this queue.
124+
* @returns {boolean} true if this queue contains the specified element.
125+
*/
89126
public contains(element: E): boolean {
90127

91-
return this.internalArray.includes(element);
128+
return this.internalDLL.contains(element);
92129

93130
}
94131

132+
/**
133+
* Removes all elements from this queue, rendering it empty.
134+
*/
95135
public clear(): void {
96136

97-
this.internalArray = [];
137+
this.internalDLL.clear();
98138

99139
}
100140

141+
/**
142+
* Converts this queue to an array, returning the result.
143+
*
144+
* The resulting array will be ordered such that those elements at the end of the queue will also appear near the
145+
* end of the resultant array.
146+
*
147+
* @returns {E[]} An array representation of this queue.
148+
*/
101149
public toArray(): E[] {
102150

103-
return this.internalArray;
151+
return this.internalDLL.toArray();
104152

105153
}
106154

107-
}
155+
}

0 commit comments

Comments
 (0)