Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commiteaba737

Browse files
committed
Stack 👗
1 parentdf7eaf7 commiteaba737

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

‎examples/datastructures/stack.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
classStack{
2+
constructor(){
3+
this.dataStore=[];
4+
this.top=-1;
5+
}
6+
7+
push(element){
8+
this.top++;
9+
this.dataStore.push(element);
10+
}
11+
12+
pop(){
13+
this.top--;
14+
returnthis.dataStore.pop();
15+
}
16+
17+
peek(){
18+
returnthis.dataStore[this.top];
19+
}
20+
21+
clear(){
22+
if(~this.top)
23+
while(this.top--)
24+
this.dataStore.pop();
25+
}
26+
27+
empty(){
28+
return!~this.top;
29+
}
30+
31+
length(){
32+
returnthis.top+1;
33+
}
34+
}
35+
36+
module.exports=Stack;

‎test/datastructures/stack.test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
constexpect=require('chai').expect;
2+
constStack=require('../../examples/datastructures/stack');
3+
4+
describe('=> STACK',function(){
5+
letnumbers;
6+
before(function(){
7+
numbers=newStack();
8+
});
9+
10+
it('should create an Empty Stack',function(done){
11+
expect(numbers.dataStore).to.deep.equal([]);
12+
expect(numbers.top).to.equal(-1);
13+
done();
14+
});
15+
16+
it('should push elements into the Stack',function(done){
17+
numbers.push(1);
18+
numbers.push(2);
19+
numbers.push(3);
20+
numbers.push(4);
21+
expect(numbers.dataStore).to.deep.equal([1,2,3,4]);
22+
expect(numbers.top).to.equal(3);
23+
done();
24+
});
25+
26+
describe('=> pop',function(){
27+
28+
it('should pop 1 element of the Stack',function(done){
29+
numbers.pop();
30+
expect(numbers.dataStore).to.deep.equal([1,2,3]);
31+
expect(numbers.top).to.equal(2);
32+
done();
33+
});
34+
35+
it('should pop all elements of the Stack',function(done){
36+
numbers.pop();
37+
numbers.pop();
38+
numbers.pop();
39+
expect(numbers.dataStore).to.deep.equal([]);
40+
expect(numbers.top).to.equal(-1);
41+
done();
42+
});
43+
44+
});
45+
46+
it('should peek the Stack',function(done){
47+
expect(numbers.peek()).to.deep.equal(4);
48+
done();
49+
});
50+
51+
it('should check if the Stack is empty',function(done){
52+
expect(numbers.empty()).to.equal(false);
53+
done();
54+
});
55+
56+
it('should check the length of the Stack',function(done){
57+
expect(numbers.length()).to.equal(4);
58+
done();
59+
});
60+
61+
describe('=> clear',function(){
62+
it('should clear the Stack',function(done){
63+
numbers.clear();
64+
expect(numbers.top).to.equal(-1);
65+
expect(numbers.dataStore).to.deep.equal([]);
66+
done();
67+
});
68+
});
69+
70+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp