Skip to content

Commit

Permalink
feat: ✨ Added Doubly Linked implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mandy8055 committed Oct 24, 2024
1 parent 123db65 commit 2515505
Show file tree
Hide file tree
Showing 9 changed files with 998 additions and 61 deletions.
10 changes: 2 additions & 8 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@ jobs:
with:
deno-version: v2.x

- name: Verify formatting
run: deno fmt --check

- name: Run linter
run: deno lint

- name: Type check
run: deno check **/*.ts
- name: Verify formatting, linting, proper JSDoc checking
run: deno task verify

- name: Run tests with coverage
run: deno test --coverage=coverage
Expand Down
69 changes: 19 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# data-structures

A comprehensive collection of generic data structure implementations for TypeScript/JavaScript, designed for use with Deno and published on JSR.
A comprehensive collection of generic data structure implementations which are not supported natively by the for TypeScript/JavaScript, and published on JSR.

[![codecov](https://codecov.io/gh/mandy8055/data-structures/branch/main/graph/badge.svg)](https://codecov.io/gh/mandy8055/data-structures)

Expand All @@ -16,77 +16,46 @@ A comprehensive collection of generic data structure implementations for TypeScr
## Installation

```typescript
import { ... } from "jsr:@msk/data-structures";
import { LinkedList, DoublyLinkedList } from 'jsr:@msk/data-structures';
```

## Available Data Structures

- **LinkedList**: Singly linked list implementation

## Quick Start

```typescript
import { LinkedList } from 'jsr:@msk/data-structures';

// Using LinkedList
const list = new LinkedList<number>();
list.append(1);
list.append(2);
list.prepend(0);
console.log([...list]); // [0, 1, 2]
```

## Detailed Documentation

Each data structure is fully documented with TypeScript types and JSDoc comments. Here's an overview of the main data structures:

### LinkedList<T>

A singly linked list implementation that stores elements of type `T`.

```typescript
const list = new LinkedList<number>();

// Basic operations
list.append(1); // Add to end
list.prepend(0); // Add to start
list.insertAt(2, 1); // Insert at index
list.removeFirst(); // Remove from start
list.removeAt(1); // Remove at index
list.get(0); // Get element at index
list.indexOf(1); // Find index of element
list.contains(1); // Check if element exists
list.size(); // Get number of elements
list.isEmpty(); // Check if empty
list.clear(); // Remove all elements

// Iteration
for (const value of list) {
console.log(value);
}
// Using DoublyLinkedList with reverse iteration
const dList = new DoublyLinkedList<number>();
dList.append(1);
dList.append(2);
dList.prepend(0);
console.log([...dList.reverseIterator()]); // [2, 1, 0]
```

## Error Handling

The library includes custom error types for proper error handling:

- `EmptyStructureError`: Thrown when attempting to access or remove elements from an empty structure
- `IndexOutOfBoundsError`: Thrown when attempting to access an invalid index
## Available Data Structures

## Performance Considerations
- [LinkedList](./docs/LinkedList.md): Singly linked list implementation
- [DoublyLinkedList](./docs/DoublyLinkedList.md): Doubly linked list with bidirectional traversal

Time complexities for common operations:
## Error Handling

### LinkedList
The library includes custom error types:

- Append/Prepend: O(1)
- Insert/Remove at index: O(n)
- Search: O(n)
- `EmptyStructureError`: For operations on empty structures
- `IndexOutOfBoundsError`: For invalid index access
- `InvalidOperationsError`: For performing invalid operations

## Contributing
## Contribution

Contributions are welcome! Please feel free to submit a Pull Request.

- [Contributing Guide](./docs/CONTRIBUTING.md)

## License

MIT License - feel free to use this in your own projects!
9 changes: 6 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
"exports": "./src/mod.ts",
"license": "MIT",
"publish": {
"include": ["src", "README.md", "LICENSE"],
"include": ["src", "README.md", "LICENSE", "docs"],
"exclude": ["src/tests", ".github", ".vscode", "deno.json"]
},
"tasks": {
"test": "deno test --allow-none",
"test": "deno test",
"coverage": "deno test --coverage=coverage && deno coverage --html coverage && open coverage/html/index.html",
"check": "deno check **/*.ts",
"lint": "deno lint",
"fmt": "deno fmt"
"fmt": "deno fmt",
"check-jsdoc": "deno doc --lint **/*.ts",
"verify": "deno task check && deno task fmt --check && deno task lint && deno task check-jsdoc"
},
"fmt": {
"include": ["src/", "tests/"],
Expand Down
40 changes: 40 additions & 0 deletions docs/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Code of Conduct

## Our Pledge

We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.

We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.

## Our Standards

Examples of behavior that contributes to a positive environment include:

- Using welcoming and inclusive language
- Being respectful of differing viewpoints and experiences
- Gracefully accepting constructive criticism
- Focusing on what is best for the community
- Showing empathy towards other community members

Examples of unacceptable behavior include:

- The use of sexualized language or imagery, and sexual attention or advances of any kind
- Trolling, insulting or derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information without explicit permission
- Other conduct which could reasonably be considered inappropriate in a professional setting

## Enforcement Responsibilities

Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement. All complaints will be reviewed and investigated promptly and fairly.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org/), version 2.0.

For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq.
147 changes: 147 additions & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# Contributing Guidelines

Thank you for considering contributing to the data-structures library! This document provides guidelines and steps for contributing.

## Code of Conduct

Please read and follow our [Code of Conduct](CODE_OF_CONDUCT.md) to maintain a respectful and inclusive environment.

## Getting Started

1. Fork the repository
2. Clone your fork:

```bash
git clone https://github.com/your-username/data-structures.git
```

3. Install dependencies(currently we do not hav):

```bash
deno cache
```

## Development Process

1. Create a new branch:

Branch naming convention:

- feat/your-feature-name: For feature branches
- fix/your-fix-name: For bugfix and hotfix branches
- release/release-name: For releasing features

```bash
git checkout -b feat/your-feature-name
```

1. Make your changes following our coding standards

2. Write/update tests for your changes

3. Run tests:

```bash
deno test
```

5. Update documentation:

- Update JSDoc comments
- Update relevant markdown files
- Update examples if needed

6. Test the linting, formatting and JSDOCs in case anything missed:

```bash
deno task verify
```

7. Commit your changes:

```bash
git commit -m "feat: add your feature description"
```

Follow [Conventional Commits](https://www.conventionalcommits.org/)

## Pull Request Process

1. Update the README.md with details of changes if needed
2. Ensure all tests pass and coverage is maintained
3. Update all relevant documentation
4. Submit a pull request

### PR Title Format

- feat: Add new feature
- fix: Fix bug
- docs: Update documentation
- test: Add/update tests
- refactor: Code refactoring
- perf: Performance improvements

## Coding Standards

### TypeScript

- Use TypeScript strict mode
- Provide type definitions for all parameters and return values
- Use generics where appropriate
- Follow functional programming principles where possible

### Documentation

- Use JSDoc for all public methods
- Include examples in documentation
- Document time complexities
- Keep documentation up to date

### Testing

- Write unit tests for all new features
- Maintain test coverage above 85%
- Include edge cases in tests
- Use descriptive test names

## Example Pull Request

Here's an example of a good pull request:

```typescript
/**
* Reverses the linked list in-place
* Time complexity: O(n)
* Space complexity: O(1)
*/
reverse(): void {
if (this.size <= 1) return;

let prev = null;
let current = this.head;
this.tail = current;

while (current) {
const next = current.next;
current.next = prev;
prev = current;
current = next;
}

this.head = prev;
}

// Tests
Deno.test("LinkedList - reverse", () => {
const list = new LinkedList<number>();
list.append(1);
list.append(2);
list.append(3);

list.reverse();

assertEquals(list.toArray(), [3, 2, 1]);
assertEquals(list.head?.value, 3);
assertEquals(list.tail?.value, 1);
});
```
Loading

0 comments on commit 2515505

Please sign in to comment.