-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.js
91 lines (78 loc) · 1.99 KB
/
Stack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class StackNode {
constructor(value) {
this._value = value;
this._next = null;
}
}
class Stack {
constructor() {
this._root = null;
this._size = 0;
}
get size() {
return this._size;
}
isEmpty() {
return this._size === 0;
}
push(value) {
const oldFirst = this._root;
this._root = new StackNode(value);
this._root._next = oldFirst;
this._size += 1;
return value;
}
pop() {
if (this.isEmpty()) {
return null;
}
const oldFirst = this._root;
this._root = oldFirst._next;
this._size -= 1;
return oldFirst._value;
}
data() {
if(this.isEmpty()) {
console.log('Stack: empty!');
return;
}
let arrStack = [];
function logData(dataStack) {
if (dataStack !== null) {
arrStack.push(dataStack._value);
logData(dataStack._next);
}
}
logData(this._root);
console.log('Stack: ');
arrStack.forEach(node => console.log(node));
arrStack = null;
}
}
const stack = new Stack();
console.log(`1 in: ${stack.push(1)}`);
console.log(`2 in: ${stack.push(true)}`);
console.log(`3 in: ${stack.push('string')}`);
console.log(`4 in: ${stack.push(null)}`);
console.log(`5 in: ${stack.push(undefined)}`);
console.log(`6 in: ${stack.push({ name: 'object' })}`);
console.log(`7 in: ${stack.push([1, 2, 3])}`);
console.log(`8 in: ${stack.push(function func() {})}`);
console.log('-'.repeat(25));
stack.data();
console.log('-'.repeat(25));
console.log('размер стека: ', stack.size);
console.log('-'.repeat(25));
console.log(`1 out: ${stack.pop()}`);
console.log(`2 out: ${stack.pop()}`);
console.log(`3 out: ${stack.pop()}`);
console.log(`4 out: ${stack.pop()}`);
console.log(`5 out: ${stack.pop()}`);
console.log(`6 out: ${stack.pop()}`);
console.log(`7 out: ${stack.pop()}`);
console.log(`8 out: ${stack.pop()}`);
console.log('-'.repeat(25));
stack.data();
console.log('-'.repeat(25));
console.log('размер стека: ', stack.size);
console.log('-'.repeat(25));