-
Notifications
You must be signed in to change notification settings - Fork 37
Implemented with ES6 classes, additional functionality. #7
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
base: master
Are you sure you want to change the base?
Changes from 2 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,141 @@ | ||
'use strict'; | ||
|
||
class Node { | ||
constructor(list, name, data) { | ||
this.list = list; | ||
this.data = data; | ||
this.name = name; | ||
this.prev = null; | ||
this.next = null; | ||
} | ||
} | ||
|
||
class LinkedList { | ||
|
||
constructor() { | ||
this.first = null; | ||
this.last = null; | ||
this.length = 0; | ||
} | ||
|
||
push(name, data) { | ||
const node = new Node(this, name, data); | ||
node.prev = this.last; | ||
if (this.length === 0) { | ||
this.first = node; | ||
} else { | ||
this.last.next = node; | ||
} | ||
this.last = node; | ||
this.length++; | ||
return node; | ||
} | ||
|
||
pop() { | ||
if (this.length > 0) { | ||
const node = this.last; | ||
this.last = node.prev; | ||
node.list = null; | ||
node.prev = null; | ||
node.next = null; | ||
this.length--; | ||
return node.data; | ||
} | ||
|
||
} | ||
|
||
searchPos(searchPosition) { | ||
|
||
let current = this.first; | ||
let position = 0; | ||
|
||
if (this.length === 0 || | ||
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. Форматирование этого длинного условия нужно как-то причесать. Я думаю, что если разобраться в сути, то условие будет покороче. |
||
searchPosition < 0 || | ||
searchPosition >= this.length) { | ||
throw new Error('Position out of bounds.'); | ||
} | ||
|
||
while (position < searchPosition) { | ||
current = current.next; | ||
++position; | ||
} | ||
|
||
return current; | ||
} | ||
|
||
remove(removePosition) { | ||
let current = this.first; | ||
let position = 0; | ||
|
||
if (this.length < 0 || removePosition >= this.length) { | ||
throw new Error('Position out of bounds.'); | ||
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. Не принято ошибки бросать по таким поводам, лучше вернуть ошибку или false из функции |
||
} | ||
|
||
if (removePosition === 0) { | ||
this.first = current.next; | ||
this.first.prev = null; | ||
this.length--; | ||
return; | ||
} | ||
|
||
while (position < removePosition) { | ||
current = current.next; | ||
++position; | ||
} | ||
|
||
current.prev.next = current.next; | ||
current.next.prev = current.prev; | ||
this.length--; | ||
} | ||
|
||
findFirst(name) { | ||
let current = this.first; | ||
|
||
while (current !== null && current.name !== name) { | ||
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. Можно даже |
||
current = current.next; | ||
} | ||
return current; | ||
} | ||
|
||
findAll(name) { | ||
let current = this.first; | ||
const arr = []; | ||
|
||
while (current !== null) { | ||
if (current.name === name) { | ||
arr.push(current); | ||
} | ||
current = current.next; | ||
} | ||
return arr; | ||
} | ||
|
||
find(name, callback) { | ||
const arr = this.findAll(name); | ||
let i; | ||
for (i = 0; i < arr.length; ++i) { | ||
callback(arr[i]); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
const list1 = new LinkedList(); | ||
list1.push('first', 1); | ||
list1.push('second', 2); | ||
list1.push('third', 3); | ||
list1.push('second', 5); | ||
|
||
console.dir(list1.findFirst('first')); | ||
console.dir(list1.findAll('second')); | ||
list1.find('second', n => console.log(n)); | ||
//console.dir(list1.searchPos(-2)); | ||
console.dir(list1.searchPos(1)); | ||
|
||
console.dir(list1); | ||
list1.remove(0); | ||
console.dir(list1); |
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.
Лучше
itemAtPos(position)