[queue] Added some alias methods.

This commit is contained in:
Revone 2023-10-31 09:03:06 +08:00
parent 1822084407
commit 3009c14c24
2 changed files with 39 additions and 12 deletions

View file

@ -5,7 +5,7 @@
*/
import {SinglyLinkedList} from '../linked-list';
export class SkipQueue<E = any> extends SinglyLinkedList<E> {
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
/**
* 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<E = any> extends SinglyLinkedList<E> {
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<E = any> {
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<E = any> {
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<E = any> {
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.

View file

@ -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<string>;
describe('LinkedListQueue', () => {
let queue: LinkedListQueue<string>;
beforeEach(() => {
queue = new SkipQueue<string>();
queue = new LinkedListQueue<string>();
});
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<number>();
const queue = new LinkedListQueue<number>();
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);
});
});