mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2025-01-01 06:09:42 +08:00
33 lines
564 B
JavaScript
33 lines
564 B
JavaScript
import LinkedList from '../linked-list/LinkedList';
|
|
|
|
export default class Stack {
|
|
constructor() {
|
|
this.linkedList = new LinkedList();
|
|
}
|
|
|
|
isEmpty() {
|
|
return !this.linkedList.tail;
|
|
}
|
|
|
|
peek() {
|
|
if (!this.linkedList.tail) {
|
|
return null;
|
|
}
|
|
|
|
return this.linkedList.tail.value;
|
|
}
|
|
|
|
push(value) {
|
|
this.linkedList.append(value);
|
|
}
|
|
|
|
pop() {
|
|
const removedTail = this.linkedList.deleteTail();
|
|
return removedTail ? removedTail.value : null;
|
|
}
|
|
|
|
toString(callback) {
|
|
return this.linkedList.toString(callback);
|
|
}
|
|
}
|