# @ahmeds.gaafer/ts-data-structures

## v1.1.5

### Published

a light weight package for typescript Data structures

Downloads

1

### Maintainers

### Readme

# TypeScript Data Structures

Data Structures package made by TypeScript.

## Install

`$ npm i @ahmeds.gaafer/ts-data-structures`

## Data Structure list:

- [x] Singly Linked List
- [x] Doubly Linked List
- [x] Queue
- [x] Stack
- [x] Heap
- [x] Graph

## Usage

### Singly Linked List:

```
import { SinglyLinkedList } from "@ahmeds.gaafer/ts-data-structures";
// The constructor can accept an array as an input and it will transform the array to the linked list
const arr = [1, 2, 3];
const list = new SinglyLinkedList<number>(arr);
list.push(1);
list.view();
// You can chain methods that does not return a value
list.popStart().pop().view(); // vaild
list.getHead().view(); // invalid
```

functions:

=> Insert data to certian position.*.insert(data, pos)*=> Insert data at the head position.*.pushStart(data)*=> Insert data at tail position.*.push(data)*=> Delete element at a certian position.*.delete(pos)*=> Delete element from the end of the tail.*.pop()*=> Delte element from the head.*.popStart()*=> Returns a copy of the head node.*.getHead()*=> Returns a copy of the tail node.*.getTail()*=> Prints a visual dispaly of the linked list.*.view()*=> Returns the linked list as an array*.toArray()*

### Doubly Linked List:

```
import { DoublyLinkedList } from "@ahmeds.gaafer/ts-data-structures";
// The constructor can accept an array as an input and it will transform the array to the linked list
const arr = [1, 2, 3];
const list = new DoublyLinkedList<number>(arr);
list.push(1);
list.view();
// You can chain methods that does not return a value
list.popStart().pop().view(); // vaild
list.getHead().view(); // invalid
```

functions:

=> Insert data to certian position.*.insert(data, pos)*=> Insert data at the head position.*.pushStart(data)*=> Insert data at tail position.*.push(data)*=> Delete element at a certian position.*.delete(pos)*=> Delete element from the end of the tail.*.pop()*=> Delte element from the head.*.popStart()*=> Returns a copy of the head node.*.getHead()*=> Returns a copy of the tail node.*.getTail()*=> Prints a visual dispaly of the linked list.*.view()*=> Returns the linked list as an array.*.toArray()*

### Queue:

```
import { Queue } from "@ahmeds.gaafer/ts-data-structures";
// The constructor can accept an array as an input and it will transform the array to the Queue
const arr = [1, 2, 3];
const queue = new Queue<number>(arr);
queue.enqueue(5);
queue.deqeue();
// You can chain methods that does not return a value
queue.enqueue(5).dequeue().view(); //vaild
queue.peak().view(); // invalid
```

functions:

=> Add data to the end of the queue.*.enqueue(data)*=> Remove first element of the queue.*.dequeue()*=> Returns the first element in the queue.*.peak()*=> Returns the size of the queue.*.getSize()*=> Prints a visual dispaly of the queue.*.view()*

### Stack:

```
import { Stack } from "@ahmeds.gaafer/ts-data-structures";
// The constructor can accept an array as an input and it will transform the array to the Heap
const arr = [1, 2, 3];
const stack = new Stack<number>(arr);
stack.push(1);
// You can chain methods that does not return a value
stack.push(1).push(2).pop(); // vaild
stack.peak().push(1); // invalid
```

functions:

=> Inserts data to top of stack.*.push(data)*=> Removes the item on the top of the stack.*.pop()*=> Returns the item on the top of the stack.*.peak()*=>Returns the size of the stack;*.getSize()*=> Prints a visual display of the stack.*.view()*

### Heap:

```
import { Heap } from "@ahmeds.gaafer/ts-data-structures";
// The constructor can accept an array as an input and it will transform the array to the Heap
const arr = [1, 2, 3];
const maxHeapOptions = {
cmp: (a, b) => a < b;
}
const minHeapOptions = {
cmp: (a, b) => a > b;
}
// Heap type is set to number by default but you can change it
//min heap
const minHeap = new Heap(arr, minHeapOptions);
//max heap
const maxHeap = new Heap(arr, maxHeapOptions);
/// or
const maxHeap = new Heap<string>([], maxHeapOptions);
maxHeap.push(5);
// You can chain methods that does not return a value
minHeap.push(1).push(2); // vaild
minHeap.peak().push(1); // invalid
```

functions:

=> Insert a data to the heap.*.push(data)*=> Removes the root of the heap and fixes the the heap.*.pop()*=> Returns the value of the root of the heap.*.peak()*=> Returns the size of the heap;*.getSize()*=> Prints the array of the heap;*.view()*

### Graph:

```
import { Graph } from "@ahmeds.gaafer/ts-data-structures";
//The graph can has 2 arguments that are set to false by default.
//The first argument is "isUniDirectional" if you set it to true the graph will be a directed graph.
//The second argument is "isWeighted" if you set it to true the graph will be weighted.
const g = new Graph(); // un-directed un-weighted graph
//or
const g = new Graph(true, false); // directed un-weighted graph.
//or
const g = new Graph(false, true); // un-directed weighted graph.
//or
const g = new Graph(true, true); // directed weighted graph
g.addVertex(1);
g.addVertex(2);
// You can chain methods that does not return a value
g.addVertex(1).addVertex(2); // valid
g.getNeighbors(1).addVertex(3); // invalid
g.removeVertex(5); // remove a non-existent vertex
// etc...
```

functions:

=> Adds vertex to the graph.*.addVertex(v)*=> Add edge between vertex "u" and vertex "v" . weight of the edge "w" is valid if only the graph is set to be weighted.*.addEdge(u, v, w: conditional)*=> Removes the vertex from the graph and removes all of the linked edges to it.*.removeVertex(v)*=> Removes edge between vertex "u" and "v".*.removeEdge(u, v)*=> Returns the number of vertices in the Graph.*.getVerticesNumbers()*=> Returns a the list of neighbors of vertex v*.getNeighbors(v)*=> Display a visual display of the graphs adjacency list.*.view()*

# Important Note:

Any usage of the functions not mentioned in the functions above might lead to un-expected behavior and may lead to the functions throwing an error.