File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
DataStructureImplementations Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ var stack = function ( ) {
2+ this . stackArray = [ ] ;
3+
4+ this . push = function ( num ) {
5+ this . stackArray . push ( num ) ;
6+ }
7+
8+ this . pop = function ( ) {
9+ if ( this . stackArray . length == 0 ) {
10+ return "Error: No elements left in array" ;
11+ }
12+ return this . stackArray . pop ( ) ;
13+ }
14+
15+ this . peek = function ( ) {
16+ if ( this . stackArray . length == 0 ) {
17+ return "Error: No elements present in array" ;
18+ }
19+ return this . stackArray [ this . stackArray . length - 1 ] ;
20+ }
21+ this . isEmpty = function ( ) {
22+ if ( this . stackArray . length == 0 ) {
23+ return true ;
24+ }
25+ return false ;
26+ }
27+
28+ this . printStack = function ( ) {
29+ console . log ( this . stackArray ) ;
30+ }
31+
32+ }
33+
34+ var stackTest = new stack ( ) ;
35+ stackTest . stackArray = [ 1 , 2 , 4 ] ;
36+ stackTest . push ( 5 ) ;
37+ stackTest . printStack ( ) ;
38+ console . log ( "Peek: " + stackTest . peek ( ) ) ;
39+ console . log ( "IsEmpty: " + stackTest . isEmpty ( ) ) ;
40+ console . log ( "Popped: " + stackTest . pop ( ) ) ;
41+
You can’t perform that action at this time.
0 commit comments