A class queue implementation in Typescript for any generic type 'T'
TypeScript Queue Data Structure
This project provides a simple implementation of a queue data structure in TypeScript. A queue is a linear data structure that follows the First In, First Out (FIFO) principle. Elements are added to the rear and removed from the front of the queue.
This queue implementation supports generic types, allowing you to use it with any data type. It provides methods for adding, removing, and inspecting elements in the queue.
Table of Contents
Usage
To use this queue data structure in your TypeScript projects, you can follow these steps:
-
Add this library to your local project with jsr:
# Deno deno add @cm-iv/queue # npm npx jsr add @cm-iv/queue # pnpm pnpm dlx jsr add @cm-iv/queue # Bun bunx jsr add @cm-iv/queue -
Import the Queue class from this module into your TypeScript files:
import { Queue } from '@cm-iv/queue'; -
Create a new instance of the Queue class:
const queue = new Queue<number>(); // Create a queue of numbers -
Use the queue methods to manipulate and retrieve data:
queue.enqueue(5); // Enqueue an element to the rear of the queue queue.dequeue(); // Dequeue an element from the front of the queue queue.peek(); // Peek at the front element without removing it queue.isEmpty(); // Check if the queue is empty queue.size(); // Get the size of the queue queue.clear(); // Clear the queue queue.print(); // Print the queue elements
Methods
enqueue(element: T): void: // Adds an element to the rear of the queue. dequeue(): T | undefined: // Removes and returns the front element from the queue. peek(): T | undefined: // Returns the front element of the queue without removing it. isEmpty(): boolean: // Checks if the queue is empty. size(): number: // Returns the size of the queue. clear(): void: // Removes all elements from the queue. print(): void: // Prints the elements of the queue.
License
This project is licensed under the MIT License
Add Package
deno add jsr:@cm-iv/queue
Import symbol
import * as queue from "@cm-iv/queue";
Import directly with a jsr specifier
import * as queue from "jsr:@cm-iv/queue";
Add Package
pnpm i jsr:@cm-iv/queue
pnpm dlx jsr add @cm-iv/queue
Import symbol
import * as queue from "@cm-iv/queue";
Add Package
yarn add jsr:@cm-iv/queue
yarn dlx jsr add @cm-iv/queue
Import symbol
import * as queue from "@cm-iv/queue";
Add Package
vlt install jsr:@cm-iv/queue
Import symbol
import * as queue from "@cm-iv/queue";
Add Package
npx jsr add @cm-iv/queue
Import symbol
import * as queue from "@cm-iv/queue";
Add Package
bunx jsr add @cm-iv/queue
Import symbol
import * as queue from "@cm-iv/queue";