-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
single and doubly lists #24
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
const Node = (value, next = null) => ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not a class or prototype constructor so I'd like to use lowerCamelCase here. |
||
value, | ||
next, | ||
}); | ||
|
||
const LinkedList = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lowerCamelCase |
||
const list = { | ||
head: null, | ||
|
||
append(value) { | ||
if (!list.head) { | ||
list.head = Node(value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. semicolon |
||
} else { | ||
let node = list.head; | ||
while (node.next) { | ||
node = node.next; | ||
} | ||
node.next = Node(value); | ||
} | ||
}, | ||
|
||
remove(value) { | ||
let prev = null; | ||
let node = list.head; | ||
|
||
while (node) { | ||
if (node.value === value) { | ||
if (node === list.head) { | ||
list.head = node.next; | ||
} else { | ||
prev.next = node.next; | ||
} | ||
} | ||
prev = node; | ||
node = node.next; | ||
} | ||
}, | ||
|
||
[Symbol.iterator]: function* () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use shorter syntax for generator as a class/object method:
|
||
let node = list.head; | ||
while (node) { | ||
yield node.value; | ||
node = node.next; | ||
} | ||
} | ||
}; | ||
return list; | ||
}; | ||
|
||
// Usage | ||
|
||
const list = LinkedList(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lowerCamelCase |
||
|
||
list.append(1); | ||
list.append(2); | ||
list.append(3); | ||
list.append(4); | ||
list.remove(3); | ||
|
||
for (item of list) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
console.log(item); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'use strict'; |
||
const Node = (value, prev = null, next = null) => ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lowerCamelCase |
||
value, | ||
prev, | ||
next, | ||
}); | ||
|
||
const DoublyLinkedList = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lowerCamelCase |
||
const list = { | ||
head: null, | ||
|
||
append(value) { | ||
if (!list.head) { | ||
list.head = Node(value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. semicolon |
||
} else { | ||
let node = list.head; | ||
while (node.next) { | ||
node = node.next; | ||
} | ||
node.next = Node(value, node); | ||
} | ||
}, | ||
|
||
remove(value) { | ||
let node = list.head; | ||
|
||
while (node) { | ||
if (node.value === value) { | ||
if (node === list.head) { | ||
list.head = node.next; | ||
} else { | ||
node.prev.next = node.next; | ||
if (node.next) { | ||
node.next.prev = node.prev; | ||
} | ||
} | ||
} | ||
node = node.next; | ||
} | ||
}, | ||
|
||
[Symbol.iterator]: function* () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use shorter syntax for generators. |
||
let node = list.head; | ||
while (node) { | ||
yield node.value; | ||
node = node.next; | ||
} | ||
} | ||
}; | ||
return list; | ||
}; | ||
|
||
// Usage | ||
|
||
const list = DoublyLinkedList(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lowerCamelCase |
||
|
||
list.append(1); | ||
list.append(2); | ||
list.append(3); | ||
list.append(4); | ||
list.remove(3); | ||
|
||
for (item of list) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
console.log(item); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'use strict';