A lightweight JavaScript library that provides essential data structures such as Stack, Queue, Linked List, and Tree (Arbre). This package simplifies the use of these data structures with clean APIs to help developers implement them in their projects effortlessly.
You can install this package using npm or pnpm:
# Using npm
npm install data-structures-package
# Using pnpm
pnpm install data-structures-package
This package provides simple, intuitive APIs to work with common data structures like Stack, Queue, Linked List, and Tree.
# Stack
const { Stack } = require('data-structures-package');
const stack = new Stack();
stack.push(5);
stack.push(10);
console.log(stack.pop()); // Output: 10
console.log(stack.peek()); // Output: 5
# Queue
const { Queue } = require('data-structures-package');
const queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
console.log(queue.dequeue()); // Output: 1
console.log(queue.peek()); // Output: 2
# Linked List
const { LinkedList } = require('data-structures-package');
const list = new LinkedList();
list.append(10);
list.append(20);
console.log(list.toArray()); // Output: [10, 20]
list.remove(10);
console.log(list.toArray()); // Output: [20]
list.append(10);
list.append(30);
console.log(list.display()); // Output : 20 -> 10 -> 30
# Tree (Arbre)
const { Tree } = require('data-structures-package');
const tree = new Tree(); // Root node
tree.addChild(2);
tree.addChild(3);
console.log(tree.inOrder()); // Output: [1, 2, 3]
push(value)
: Adds a value to the stack.pop()
: Removes the top value from the stack and returns it. Returnsnull
if the stack is empty.peek()
: Returns the top value of the stack without removing it.toString()
: Returns an array representation of the stack.isEmpty()
: Returnstrue
if the stack is empty, otherwise returnsfalse
.
enqueue(value)
: Adds a value to the front of the queue.dequeue()
: Removes the last value from the queue and returns it. Returnsnull
if the queue is empty.peek()
: Returns the last value in the queue without removing it.isEmpty()
: Returnstrue
if the queue is empty, otherwise returnsfalse
.
append(value)
: Adds a value to the end of the linked list.prepend(value)
: Adds a value to the beginning of the linked list.find(value)
: Finds and returns the value and position of a node in the linked list. Returnsnull
if the value is not found.remove(value)
: Removes the node with the specified value from the list.toArray()
: Converts the linked list into an array of nodes.display()
: Returns a string representation of the linked list in the formvalue1 -> value2 -> value3
.
addChild(value)
: Adds a new node to the tree at the correct position based on its value.preOrder()
: Traverses the tree in pre-order (node → left → right).inOrder()
: Traverses the tree in in-order (left → node → right).postOrder()
: Traverses the tree in post-order (left → right → node).
Contributions, issues, and feature requests are welcome!
Feel free to check the issues page if you want to contribute.
- Fork the repository.
- Create a new branch:
git checkout -b feature-branch
. - Make your changes and commit them:
git commit -m 'Add a new feature'
. - Push to the branch:
git push origin feature-branch
. - Submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.
Ayyoub
- GitHub: @ay-ub
- LinkedIn: ayyoubhadjyoucef
If you like this project, feel free to give it a star ⭐ on GitHub!
You can also share this repository with others who might find it useful.
Thank you for your support! 🙌