Skip to content

Commit

Permalink
organize LL methods
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed May 22, 2023
1 parent f735de7 commit fe1a0a4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 52 deletions.
41 changes: 28 additions & 13 deletions src/DataStructures/LinkedList.js
Expand Up @@ -37,7 +37,7 @@ export class LinkedList {
return this;
}

// Add a new value to the END of the list
// ADD a new value to the END of the list
push(value) {
const newNode = new Node(value);
if (!this.head) {
Expand All @@ -52,7 +52,26 @@ export class LinkedList {
return this;
}

// Remove a value from the END of the list
// ADD a new value to the START 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;
}


// ADD a new value to the given index of the list
insert(value, index) {
return this;
}

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

Expand All @@ -72,21 +91,17 @@ export class LinkedList {
return temp.value;
}

// 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++;
// REMOVE a value to the START of the list
shift() {
return this;
}

// REMOVE a value in the given index of the list
remove(value, index) {
return this;
}

// Add a new value to the given index of the list
insert(value, index) {
reverse() {
return this;
}

Expand Down
78 changes: 39 additions & 39 deletions tests/DataStructures/LinkedList.test.js
Expand Up @@ -60,6 +60,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("pop()", () => {
it("remove the last element from the linked list", () => {
const ll = new LinkedList(100);
Expand Down Expand Up @@ -102,45 +141,6 @@ 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 Down

0 comments on commit fe1a0a4

Please sign in to comment.