-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstack.js
More file actions
102 lines (86 loc) · 1.55 KB
/
stack.js
File metadata and controls
102 lines (86 loc) · 1.55 KB
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
92
93
94
95
96
97
98
99
100
101
102
class Stack {
constructor(size) {
this.top = 0;
this.size = size;
this.data = new Array(size);
}
// 打印栈中的所有元素
printStack() {
if (this.isEmpty()) {
console.log("栈为空");
return;
}
for (let i = 0; i < this.top; i++) {
process.stdout.write(this.data[i] + " ");
}
console.log();
}
// 判断栈是否已满
isFull() {
return this.top === this.size;
}
// 判断栈是否为空
isEmpty() {
return this.top === 0;
}
// 入栈操作
push(value) {
if (this.isFull()) {
return false;
}
this.data[this.top] = value;
this.top++;
return true;
}
// 出栈操作
pop() {
if (this.isEmpty()) {
return 0;
}
this.top--;
return this.data[this.top];
}
// 清空栈
makeEmpty() {
this.top = 0;
}
// 获取栈中元素的个数
len() {
return this.top;
}
}
// 测试栈功能
function testStack() {
let s = new Stack(5);
console.log("执行入栈操作");
console.log("入栈 1:", s.push(1));
s.printStack();
console.log("入栈 2:", s.push(2));
s.printStack();
console.log("执行出栈操作");
console.log("出栈:", s.pop());
s.printStack();
console.log("出栈:", s.pop());
s.printStack();
console.log("清空栈");
s.makeEmpty();
s.printStack();
console.log("测试结束");
}
testStack();
/*
jarry@MacBook-Pro stack % node stack.js
执行入栈操作
入栈 1: true
1
入栈 2: true
1 2
执行出栈操作
出栈: 2
1
出栈: 1
栈为空
清空栈
栈为空
测试结束
*/