Add Stack.

This commit is contained in:
Oleksii Trekhleb
2018-03-28 17:01:46 +03:00
parent 159d489e52
commit 8da6754523
9 changed files with 189 additions and 48 deletions

View File

@@ -0,0 +1,28 @@
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;
}
}