1.0.7 • Published 6 months ago

queue-list v1.0.7

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

queue-list

queue-list is a simple npm package that provides an implementation of a queue data structure in JavaScript. It allows you to easily create and manipulate queues with enqueue, dequeue, peek, isEmpty, size, clear, and toArray operations. LinkedList is used to achieve this.

👉Note: all the methods including enqueue, dequeue, peek, isEmpty, size, clear is having constant time complexity😎 O(1)

Installation

To install the package, use npm:

npm install queue-list

Usage

Importing the package

import Queue from 'queue-list';

Creating a Queue

// Creating an empty queue
const myQueue = new Queue();

// Creating a queue with initial values
const initializedQueue = new Queue(1, 2, 3);
console.log(initializedQueue);

//Expected output will be like 
Queue {
  start: Node { value: 1, next: Node { value: 2, next: Node { value: 3, next: null } } },
  last: Node { value: 3, next: null }
}
//Here start is referring to starting Node and last is referring to the last Node of the LinkedList.

console.log(initializedQueue.length);
//expected output is 3

Enqueue

Add an element to the end of the queue.

myQueue.Enqueue(4);

Dequeue

Remove and return the element from the front of the queue.

const dequeuedItem = myQueue.Dequeue();

Peek

Get the element at the front of the queue without removing it.

const frontElement = myQueue.Peek();

IsEmpty

Check if the queue is empty.

const emptyStatus = myQueue.IsEmpty();

Size

Get the number of elements in the queue.

const queueSize = myQueue.Size();

Clear

Remove all elements from the queue.

myQueue.Clear();

ToArray

Convert the queue to an array.

const queueArray = myQueue.ToArray();

Example

const myQueue = new Queue(1, 2, 3);

myQueue.Enqueue(4);
myQueue.Enqueue(5);

console.log(myQueue.ToArray()); // [1, 2, 3, 4, 5]

const dequeuedItem = myQueue.Dequeue();
console.log(dequeuedItem); // 1

console.log(myQueue.Size()); // 4
console.log(myQueue.Peek()); // 2

myQueue.Clear();
console.log(myQueue.IsEmpty()); // true
MethodsUse caseTime Complexity
EnqueueAdd an element to the end of the queue.O(1)
DequeueRemove and return the element from the front of the queue.O(1)
PeekGet the element at the front of the queue without removing it.O(1)
IsEmptyCheck if the queue is empty and return a boolean valueO(1)
SizeGet the number of elements in the queue.O(1)
ClearRemove all elements from the queue.O(1)
ToArrayConvert the queue to an array.O(n)

➡️ the length of the queue can be accessed in two ways

  1. length property
  2. Size method

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Acknowledgments

This package is inspired by the need for a simple and efficient queue implementation in JavaScript. Feel free to use and contribute to make this package even better! If you encounter any issues or have suggestions for improvement, please open an issue on the GitHub repository. Here is the GitHub Repo Link.

1.0.7

6 months ago

1.0.6

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago

1.0.3

3 years ago