LIFO ( Last | Pushing element on the top of the stack ; Popping element from the top of the stack ; Displaying element on the top of the stack ; Searching element in the stack ; Driver code
function stack_push ( stack ) { for ( var i = 0 ; i < 5 ; i ++ ) { stack . push ( i ) ; } return stack ; } function stack_pop ( stack ) { document . write ( " " ) ; for ( var i = 0 ; i < 5 ; i ++ ) { var y = parseInt ( stack [ stack . length - 1 ] ) ; stack . pop ( ) ; document . write ( y + " " ) ; } return stack ; } function stack_peek ( stack ) { var element = parseInt ( stack [ stack . length - 1 ] ) ; document . write ( " " + element + " " ) ; } function stack_search ( stack , element ) { var pos = - 1 , co = 0 ; while ( stack . length > 0 ) { co ++ ; if ( stack [ stack . length - 1 ] == element ) { pos = co ; break ; } stack . pop ( ) ; } if ( pos == - 1 ) document . write ( " " + " " ) ; else document . write ( " " + pos + " " ) ; } stack = [ ] ; stack = stack_push ( stack ) ; stack = stack_pop ( stack ) ; stack = stack_push ( stack ) ; stack_peek ( stack ) ; stack_search ( stack , 2 ) ; stack_search ( stack , 6 ) ;