File tree 2 files changed +26
-14
lines changed
2 files changed +26
-14
lines changed Original file line number Diff line number Diff line change 1
1
function Stack ( ) {
2
2
3
- this . items = [ ] ;
3
+ var items = [ ] ;
4
4
5
5
this . push = function ( element ) {
6
- this . items . push ( element ) ;
6
+ items . push ( element ) ;
7
7
} ;
8
8
9
9
this . pop = function ( ) {
10
- return this . items . pop ( ) ;
10
+ return items . pop ( ) ;
11
11
} ;
12
12
13
13
this . peek = function ( ) {
14
- return this . items [ this . items . length - 1 ] ;
14
+ return items [ items . length - 1 ] ;
15
15
} ;
16
16
17
17
this . isEmpty = function ( ) {
18
- return this . items . length == 0 ;
18
+ return items . length == 0 ;
19
19
} ;
20
20
21
21
this . size = function ( ) {
22
- return this . items . length ;
22
+ return items . length ;
23
+ } ;
24
+
25
+ this . clear = function ( ) {
26
+ items = [ ] ;
23
27
} ;
24
28
25
29
this . print = function ( ) {
26
- console . log ( this . items . toString ( ) ) ;
30
+ console . log ( items . toString ( ) ) ;
31
+ } ;
32
+
33
+ this . toString = function ( ) {
34
+ return items . toString ( ) ;
27
35
} ;
28
36
}
Original file line number Diff line number Diff line change 1
1
function Queue ( ) {
2
2
3
- this . items = [ ] ;
3
+ var items = [ ] ;
4
4
5
5
this . enqueue = function ( element ) {
6
- this . items . push ( element ) ;
6
+ items . push ( element ) ;
7
7
} ;
8
8
9
9
this . dequeue = function ( ) {
10
- return this . items . shift ( ) ;
10
+ return items . shift ( ) ;
11
11
} ;
12
12
13
13
this . front = function ( ) {
14
- return this . items [ 0 ] ;
14
+ return items [ 0 ] ;
15
15
} ;
16
16
17
17
this . isEmpty = function ( ) {
18
- return this . items . length == 0 ;
18
+ return items . length == 0 ;
19
+ } ;
20
+
21
+ this . clear = function ( ) {
22
+ items = [ ] ;
19
23
} ;
20
24
21
25
this . size = function ( ) {
22
- return this . items . length ;
26
+ return items . length ;
23
27
} ;
24
28
25
29
this . print = function ( ) {
26
- console . log ( this . items . toString ( ) ) ;
30
+ console . log ( items . toString ( ) ) ;
27
31
} ;
28
32
}
You can’t perform that action at this time.
0 commit comments