Created
March 31, 2021 21:00
-
-
Save YusufAbdelaziz/9cc1cf22315ec7a7b7580ff571a4c8fc to your computer and use it in GitHub Desktop.
A linked list created in JavaScript.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// This is only a SKELETON file for the 'Linked List' exercise. It's been provided as a | |
// convenience to get you started writing code faster. | |
// | |
/// Supposing that the list has at least one item. | |
class Node { | |
constructor(value, previous, next) { | |
this.value = value; | |
this.previous = previous; | |
this.next = next; | |
} | |
} | |
// export | |
export class LinkedList { | |
constructor() { | |
this._sentinel = new Node(0, null, null); | |
this._sentinel.next = this._sentinel; | |
this._sentinel.previous = this._sentinel; | |
this.itemsNum = 0; | |
} | |
// Inserts value at back | |
push(value) { | |
let lastItem; | |
if (this._sentinel.previous != null) { | |
lastItem = this._sentinel.previous; | |
lastItem.next = new Node(value, lastItem, null); | |
lastItem = lastItem.next; | |
} else { | |
lastItem = new Node(value, this._sentinel, this._sentinel); | |
this._sentinel.next = lastItem; | |
} | |
// while (lastItem.next != null) { | |
// lastItem = lastItem.next; | |
// } | |
this._sentinel.previous = lastItem; | |
lastItem.next = this._sentinel; | |
this.itemsNum++; | |
} | |
// Removes value from the back | |
pop() { | |
let lastItem = this._sentinel.previous; | |
// while (lastItem.next != null) { | |
// lastItem = lastItem.next; | |
// } | |
this._sentinel.previous = lastItem.previous; | |
let value = lastItem.value; | |
lastItem = null; | |
this.itemsNum--; | |
return value; | |
} | |
// Removes value at front | |
shift() { | |
let frontItem; | |
frontItem = this._sentinel.next; | |
let value = frontItem.value; | |
frontItem.next.previous = this._sentinel; | |
this._sentinel.next = frontItem.next; | |
this.itemsNum--; | |
return value; | |
} | |
// Inserts value at front | |
unshift(value) { | |
let frontItem; | |
frontItem = this._sentinel.next; | |
this._sentinel.next = new Node(value, this._sentinel, frontItem); | |
frontItem.previous = this._sentinel.next; | |
this.itemsNum++; | |
} | |
// Deletes specific element. | |
delete(value) { | |
let currentItem = this._sentinel.next; | |
while (currentItem != this._sentinel) { | |
if (currentItem.value == value) { | |
let previousItem = currentItem.previous; | |
let nextItem = currentItem.next; | |
previousItem.next = nextItem; | |
nextItem.previous = previousItem; | |
this.itemsNum--; | |
break; | |
} else { | |
currentItem = currentItem.next; | |
} | |
} | |
} | |
count() { | |
return this.itemsNum; | |
} | |
} | |
var list = new LinkedList(); | |
list.push(5); | |
list.push(4); | |
list.push(3); | |
list.push(2); | |
list.delete(2); | |
console.log(list.pop()); | |
console.log(list.pop()); | |
console.log(list.pop()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment