Skip to content

Commit

Permalink
feat: add sort and reverse methods (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed May 16, 2024
1 parent 7fbaaf4 commit 556c4de
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 7 deletions.
48 changes: 44 additions & 4 deletions src/DataStructures/DoublyLinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,51 @@ class DoublyLinkedList {
return node.value;
}

// TODO: Implement reverse()
reverse() { }
/**
* Reverses the linked list.
*
* @example
* const dll = new DoublyLinkedList([10,20,30]);
* dll.reverse(); // 30,20,10
*
* @returns {DoublyLinkedList} The current DoublyLinkedList instance.
*/
reverse() {
if (this.head === null || this.head === this.tail) return this;

let temp = this.head;
this.head = this.tail;
this.tail = temp;

let prev = null;
let next = null;

for (let i = 0; i < this.size; i++) {
next = temp.next;
temp.next = prev;
prev = temp;
temp = next;
}

// TODO: Implement sort()
sort() { }
return this;
}

/**
* Sorts the linked list in ascending order.
*
* @example
* const dll = new DoublyLinkedList([50,20,40,10,30]);
* dll.sort(); // 10,20,30,40,50
*
* @returns {DoublyLinkedList} The current DoublyLinkedList instance.
*/
sort() {
const arr = this.toArray();
arr.sort((a, b) => a - b);
this.clear();
arr.forEach(el => this.push(el));
return this;
}

/**
* Returns an array representation of the linked list.
Expand Down
18 changes: 16 additions & 2 deletions src/DataStructures/LinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,22 @@ class LinkedList {
return this;
}

// TODO: Implement sort()
sort() { }
/**
* Sorts the linked list in ascending order.
*
* @example
* const ll = new LinkedList([50,20,40,10,30]);
* ll.sort(); // 10,20,30,40,50
*
* @returns {LinkedList} The current LinkedList instance.
*/
sort() {
const arr = this.toArray();
arr.sort((a, b) => a - b);
this.clear();
arr.forEach(el => this.push(el));
return this;
}

/**
* Converts the linked list to an array.
Expand Down
77 changes: 77 additions & 0 deletions tests/DataStructures/DoublyLinkedList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,83 @@ describe("DataStructures/DoublyLinkedList.js", () => {
});
});

describe("reverse()", () => {
it("should reverse the list (5 elements)", () => {
const dll = new DoublyLinkedList([0, 10, 20, 30, 40]);

expect(dll.reverse()).toBeInstanceOf(DoublyLinkedList);

expect(dll.head.value).toBe(40);
expect(dll.tail.value).toBe(0);

expect(dll.toArray()).toEqual([40, 30, 20, 10, 0]);
});

it("should reverse the list (2 elements)", () => {
const dll = new DoublyLinkedList([10, 20]);
dll.reverse();

expect(dll.toArray()).toEqual([20, 10]);
});

test("does nothing on a list with only one element", () => {
const dll = new DoublyLinkedList(0);
dll.reverse();

expect(dll.head.value).toBe(0);
expect(dll.tail.value).toBe(0);

expect(dll.toArray()).toEqual([0]);
});

test("does nothing if the list is empty", () => {
const dll = new DoublyLinkedList();
dll.reverse();

expect(dll.head).toBeNull();
expect(dll.tail).toBeNull();

expect(dll.toArray()).toEqual([]);
});

it("should reverse twice the list", () => {
const dll = new DoublyLinkedList([10, 20, 30]);

dll.reverse();
expect(dll.toArray()).toEqual([30, 20, 10]);

dll.reverse();
expect(dll.toArray()).toEqual([10, 20, 30]);
});
});

describe("sort()", () => {
it("should sort the list", () => {
const dll = new DoublyLinkedList([50, 20, 40, 10, 30]);

expect(dll.sort()).toBeInstanceOf(DoublyLinkedList);
expect(dll.toArray()).toEqual([10, 20, 30, 40, 50]);
});

it("should sort an already sorted list", () => {
const dll = new DoublyLinkedList([10, 20, 30]);
dll.sort();
expect(dll.toArray()).toEqual([10, 20, 30]);
});

it("should sort a list with only one element", () => {
const dll = new DoublyLinkedList(10);
dll.sort();
expect(dll.toArray()).toEqual([10]);
});

it("should sort an empty list", () => {
const dll = new DoublyLinkedList();
dll.sort();
expect(dll.toArray()).toEqual([]);
});
});

describe("toArray()", () => {
it("should convert the list to an array", () => {
const dll = new DoublyLinkedList([10, 20, 30]);
Expand Down
27 changes: 27 additions & 0 deletions tests/DataStructures/LinkedList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,33 @@ describe("DataStructures/LinkedList.js", () => {
});
});

describe("sort()", () => {
it("should sort the list", () => {
const ll = new LinkedList([50, 20, 40, 10, 30]);

expect(ll.sort()).toBeInstanceOf(LinkedList);
expect(ll.toArray()).toEqual([10, 20, 30, 40, 50]);
});

it("should sort an already sorted list", () => {
const ll = new LinkedList([10, 20, 30]);
ll.sort();
expect(ll.toArray()).toEqual([10, 20, 30]);
});

it("should sort a list with only one element", () => {
const ll = new LinkedList(10);
ll.sort();
expect(ll.toArray()).toEqual([10]);
});

it("should sort an empty list", () => {
const ll = new LinkedList();
ll.sort();
expect(ll.toArray()).toEqual([]);
});
});

describe("toArray()", () => {
it("should return an array with the values of the linked list", () => {
const ll = new LinkedList([0, 10, 20, 30]);
Expand Down
2 changes: 1 addition & 1 deletion wiki/Custom-Build.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ git clone https://github.com/201flaviosilva-labs/utilidades.git
#### Move to the project folder
After clone the repository, you need to move to the project folder to start working :)
```sh
cd utilsjs
cd utilidades
```

#### Install
Expand Down

0 comments on commit 556c4de

Please sign in to comment.