Skip to content

Commit

Permalink
feat: created unshift linkedlist method
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed May 22, 2023
1 parent 08f1dc1 commit f735de7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
28 changes: 22 additions & 6 deletions src/DataStructures/LinkedList.js
@@ -1,3 +1,5 @@
import { isValidNumber } from "../Maths";

export class Node {
constructor(value) {
this.value = value;
Expand All @@ -7,10 +9,16 @@ export class Node {

export class LinkedList {
constructor(value) {
const newNode = new Node(value);
this.head = newNode;
this.tail = newNode;
this.size = 1;
if (isValidNumber(value)) {
const newNode = new Node(value);
this.head = newNode;
this.tail = newNode;
this.size = 1;
} else {
this.head = null;
this.tail = null;
this.size = 0;
}
}

print() {
Expand Down Expand Up @@ -46,7 +54,7 @@ export class LinkedList {

// Remove a value from the END of the list
pop() {
if (this.size === 0) return undefined;
if (!this.size) return undefined;

let pre = this.head;
let temp = this.head;
Expand All @@ -56,7 +64,7 @@ export class LinkedList {
}

this.tail = pre;
pre.next = null;
this.tail.next = null;

this.size--;

Expand All @@ -66,6 +74,14 @@ export class LinkedList {

// Add a new value to the BEGINNING of the list
unshift(value) {
const newNode = new Node(value);
newNode.next = this.head;
this.head = newNode;

if (!this.tail) this.tail = newNode;

this.size++;

return this;
}

Expand Down
48 changes: 46 additions & 2 deletions tests/DataStructures/LinkedList.test.js
Expand Up @@ -18,6 +18,13 @@ describe("LinkedList.js", () => {

expect(ll.size).toBe(1);
});

it("creates a new empty LinkedList", () => {
const ll = new LinkedList();
expect(ll.size).toBe(0);
expect(ll.head).toBe(null);
expect(ll.tail).toBe(null);
});
});

describe("clear()", () => {
Expand Down Expand Up @@ -95,6 +102,45 @@ describe("LinkedList.js", () => {
});
});

describe("unshift()", () => {
it("should add a new element at the start", () => {
const ll = new LinkedList(0);

ll.unshift(10);
expect(ll.size).toBe(2);
expect(ll.head.value).toBe(10);
expect(ll.tail.value).toBe(0);
expect(ll.tail.next).toBe(null);
expect(ll.toArray()).toEqual([10, 0]);

ll.unshift(20);
expect(ll.size).toBe(3);
expect(ll.head.value).toBe(20);
expect(ll.tail.value).toBe(0);
expect(ll.tail.next).toBe(null);
expect(ll.toArray()).toEqual([20, 10, 0]);
});

it("should add a new element if the list is empty", () => {
const ll = new LinkedList();
expect(ll.size).toBe(0);

ll.unshift(1);
expect(ll.size).toBe(1);
expect(ll.head.value).toBe(1);
expect(ll.tail.value).toBe(1);
expect(ll.tail.next).toBe(null);
expect(ll.toArray()).toEqual([1]);

ll.unshift(10);
expect(ll.size).toBe(2);
expect(ll.head.value).toBe(10);
expect(ll.tail.value).toBe(1);
expect(ll.tail.next).toBe(null);
expect(ll.toArray()).toEqual([10, 1]);
});
});

describe("toArray()", () => {
it("should return an array with the values of the linked list", () => {
const ll = new LinkedList(0);
Expand All @@ -104,8 +150,6 @@ describe("LinkedList.js", () => {

const result = ll.toArray();

console.log(result);

expect(Array.isArray(result)).toBe(true);
expect(result).toEqual([0, 10, 20, 30]);
});
Expand Down

0 comments on commit f735de7

Please sign in to comment.