Collection Kit

Deque

Linear

A Deque (Double-Ended Queue) is a linear data structure that allows insertion and deletion of elements from both ends.

Import Statement
import { Deque } from "collection-kit";

Key Features

  • Flexible Operations: Can be used as both a stack and a queue.
  • Dynamic Size: Can grow and shrink as elements are added or removed.

Common Operations

AddFirst
Insert an element at the front.
AddLast
Insert an element at the back.
RemoveFirst
Remove and return the front element.
RemoveLast
Remove and return the back element.
Size
Return the number of elements in the deque.

Example Code

import { Deque } from "collection-kit";

const deque = new Deque();
deque.addFirst(10);
deque.addLast(20);
deque.addFirst(5);

console.log("Size:", deque.size());           // 3
console.log("First:", deque.removeFirst());   // 5
console.log("Last:", deque.removeLast());     // 20