Skip to content

Commit

Permalink
Add doubly linked list class and reverse to method singly linked list
Browse files Browse the repository at this point in the history
Signed-off-by: Darko Draskovic <[email protected]>
  • Loading branch information
darkodraskovic committed Aug 26, 2021
1 parent c294fc9 commit ba82d7b
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
122 changes: 122 additions & 0 deletions src/doubly-linked-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
export class DoublyLinkedList {
constructor(value) {
this.head = {
value: value,
next: null,
prev: null,
}
this.tail = this.head;
this.length = 1;
}
append(value) {
this.tail.next = {
value: value,
next: null,
prev: this.tail,
},
this.tail = this.tail.next;
this.length++;
return this;
}
prepend(value) {
this.head = {
value: value,
next: this.head,
prev: null,
}
this.head.next.prev = this.head;
this.length++;
return this;
}

insert(index, value) {
if (index <= 0) return this.prepend(value);
if (index >= this.length) return this.append(value);

const current = this.traverse(index-1);
current.next = {
value: value,
next: current.next,
prev: current,
}
current.next.next.prev = current.next;
this.length++;
return this;
}

remove(index) {
if (index < 0 || index >= this.length) {
return;
}

const current = this.traverse(index-1);
if (index === 0) {
this.head = this.head.next;
this.head.prev = null;
}
else if (index === this.length-1) {
this.tail = current;
current.next = null;
} else {
current.next = current.next.next;
current.next.prev = current;
}
this.length--;
return this;
}

traverse(index) {
let current = this.head;
if (index >= 0) {
for (let i = 0; i < index; i++) {
current = current.next;
}
} else {
current = this.tail;
for (let i = this.length-1; i > this.length+index; i--) {
current = current.prev;
}
}
return current;
}

array() {
const arr = [];
let current = this.head;
while (current !== null) {
arr.push(current.value);
current = current.next;
}
return arr;
}
}

export function test() {
const linkedList = new DoublyLinkedList(0);
linkedList.append(1);
linkedList.append(2);
linkedList.append(3);
linkedList.append(4);
console.log(linkedList.array());
console.log(linkedList.traverse(-1).value);
console.log(linkedList.traverse(-3).value);
console.log(linkedList.traverse(-linkedList.length).value);

linkedList.remove(0);
console.log(linkedList.array());
console.log(linkedList.traverse(-linkedList.length).value);
linkedList.remove(1);
console.log(linkedList.array());
console.log(linkedList.traverse(-linkedList.length).value);
linkedList.remove(linkedList.length-1);
console.log(linkedList.array());
console.log(linkedList.traverse(-linkedList.length).value);

linkedList.insert(1, 0);
console.log(linkedList.array());
console.log(linkedList.traverse(-linkedList.length).value);

linkedList.prepend(-1);
console.log(linkedList.array());
console.log(linkedList.traverse(-linkedList.length).value);
}
19 changes: 19 additions & 0 deletions src/linked-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ export class LinkedList {
}
return arr;
}

reverse() {
let current = this.head;
let next = current.next;
this.head.next = null;
this.head = this.tail;

while(next) {
const tmp = next.next;
next.next = current;
current = next;
next = tmp;
}
this.head = current;
return this;
}
}

export function test() {
Expand Down Expand Up @@ -101,4 +117,7 @@ export function test() {
linkedList.remove(linkedList.length-1);
linkedList.remove(linkedList.length);
console.log(linkedList.array());

linkedList.reverse();
console.log(linkedList.array());
}

0 comments on commit ba82d7b

Please sign in to comment.