GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — TypeScriptAlgorithms/data_structures/queue/array_queue.ts at master · JSweet314/TypeScriptAlgorithms Expand file tree/ Copy patharray_queue.tsMore file actions 64 lines (57 loc) · 1.56 KB / Copy patharray_queue.tsFile metadata and controls64 lines (57 loc) · 1.56 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 /** * This is an array-based implementation of a Queue. * A Queue is a data structure that follows the FIFO (First In First Out) principle. * It means that the first element that was added to the queue will be the first one to be removed. * The time complexity of the operations is O(n). */ import { Queue } from './queue' export class ArrayQueue<T> implements Queue<T>{ private queue: T[] = []; /** * Returns the number of items in the queue. * * @returns {number} The number of items in the queue. */ length(): number { return this.queue.length; } /** * Checks if the queue is empty. * * @returns {boolean} Whether the queue is empty or not. */ isEmpty(): boolean { return this.queue.length === 0; } /** * Adds an item to the queue. * * @param item The item being added to the queue. */ enqueue(item: T): void { this.queue.push(item); } /** * Removes an item from the queue and returns it. * * @throws Queue Underflow if the queue is empty. * @returns The item that was removed from the queue. */ dequeue(): T { if (this.isEmpty()) { throw new Error("Queue Underflow"); } return this.queue.shift() as T; } /** * Returns the item at the front of the queue. * * @returns The item at the front of the queue or null if the queue is empty. */ peek(): T | null { if (this.isEmpty()) { return null; } return this.queue[0]; } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |