From 3009c14c243b66c75156ef8f64fda87b3e987514 Mon Sep 17 00:00:00 2001 From: Revone Date: Tue, 31 Oct 2023 09:03:06 +0800 Subject: [PATCH] [queue] Added some alias methods. --- src/data-structures/queue/queue.ts | 35 ++++++++++++++++--- test/unit/data-structures/queue/queue.test.ts | 16 ++++----- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/data-structures/queue/queue.ts b/src/data-structures/queue/queue.ts index 6f10f59..bc3a78c 100644 --- a/src/data-structures/queue/queue.ts +++ b/src/data-structures/queue/queue.ts @@ -5,7 +5,7 @@ */ import {SinglyLinkedList} from '../linked-list'; -export class SkipQueue extends SinglyLinkedList { +export class LinkedListQueue extends SinglyLinkedList { /** * The enqueue function adds a value to the end of an array. * @param {E} value - The value parameter represents the value that you want to add to the queue. @@ -22,12 +22,20 @@ export class SkipQueue extends SinglyLinkedList { return this.shift(); } + /** + * The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty. + * @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`. + */ + getFirst(): E | undefined { + return this.head?.value; + } + /** * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty. * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`. */ peek(): E | undefined { - return this.head?.value; + return this.getFirst(); } } @@ -101,7 +109,7 @@ export class Queue { shift(): E | undefined { if (this.size === 0) return undefined; - const first = this.peek(); + const first = this.getFirst(); this.offset += 1; if (this.offset * 2 < this.nodes.length) return first; @@ -113,13 +121,23 @@ export class Queue { return first; } + /** + * The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`. + * @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at + * the `_offset` index. If the data structure is empty (size is 0), it returns `null`. + */ + getFirst(): E | undefined { + return this.size > 0 ? this.nodes[this.offset] : undefined; + } + + /** * The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`. * @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at * the `_offset` index. If the data structure is empty (size is 0), it returns `null`. */ peek(): E | undefined { - return this.size > 0 ? this.nodes[this.offset] : undefined; + return this.getFirst(); } /** @@ -131,6 +149,15 @@ export class Queue { return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined; } + /** + * The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty. + * @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the + * array is empty, it returns `null`. + */ + peekLast(): E | undefined { + return this.getLast(); + } + /** * The enqueue function adds a value to the end of a queue. * @param {E} value - The value parameter represents the value that you want to add to the queue. diff --git a/test/unit/data-structures/queue/queue.test.ts b/test/unit/data-structures/queue/queue.test.ts index eb062f6..9e2a263 100644 --- a/test/unit/data-structures/queue/queue.test.ts +++ b/test/unit/data-structures/queue/queue.test.ts @@ -1,4 +1,4 @@ -import {SkipQueue, Queue} from '../../../../src'; +import {LinkedListQueue, Queue} from '../../../../src'; import {bigO, magnitude} from '../../../utils'; import {isDebugTest} from '../../../config'; @@ -168,11 +168,11 @@ describe('Queue', () => { expect(values).toEqual([1, 2, 3]); }); }); -describe('SkipQueue', () => { - let queue: SkipQueue; +describe('LinkedListQueue', () => { + let queue: LinkedListQueue; beforeEach(() => { - queue = new SkipQueue(); + queue = new LinkedListQueue(); }); it('should enqueue elements to the end of the queue', () => { @@ -197,7 +197,7 @@ describe('SkipQueue', () => { expect(queue.peek()).toBe('A'); }); - // Add more test cases for other methods of SkipQueue. + // Add more test cases for other methods of LinkedListQueue. }); describe('Queue Performance Test', () => { @@ -228,16 +228,16 @@ describe('Queue Performance Test', () => { expect(performance.now() - startTime2).toBeLessThan(bigO.CUBED * 100); }); - it('should numeric SkipQueue be efficient', function () { + it('should numeric LinkedListQueue be efficient', function () { const startTime = performance.now(); - const queue = new SkipQueue(); + const queue = new LinkedListQueue(); for (let i = 0; i < dataSize; i++) { queue.enqueue(i); } for (let i = 0; i < dataSize; i++) { queue.dequeue(); } - console.log(`SkipQueue Performance Test: ${performance.now() - startTime} ms`); + console.log(`LinkedListQueue Performance Test: ${performance.now() - startTime} ms`); expect(performance.now() - startTime).toBeLessThan(bigO.LINEAR * 100); }); });