JavaScript DHTML/Language Basics/Stack

Материал из Web эксперт
Перейти к: навигация, поиск

Pushing, Popping, and Displaying the Contents of a Stack

   <source lang="html4strict">

<HTML> <HEAD> <TITLE>Stacking up!</TITLE> <SCRIPT> var stack = new Array(); function pushStack(newVal) {

  stack.push(newVal); 

} function popStack() {

  var popVal = stack.pop(); 
  if (popVal == undefined) 
     return "Empty stack!"; 
  else 
  return popVal; 

} function showStack(theSelect){

  theSelect.options.length = 0; 
  for (var i = 0; i < stack.length; i++){ 
     var theOption = new Option(stack[i]); 
     theSelect.options[theSelect.options.length] = theOption; 
  } 

} </SCRIPT> </HEAD> <BODY> <FORM> <INPUT type=text name=txtPush> <INPUT type=button value="Push" onClick="pushStack(txtPush.value); txtPush.value=""; showStack(theList);"> <SELECT name="theList" size=12> <OPTION>Displays the current state of the stack! </SELECT> <INPUT type=text name=txtPop size=25> <INPUT type=button value="Pop" onClick="txtPop.value = popStack();

  showStack(theList);">

</FORM> </BODY> </HTML>


      </source>
   
  


Simple Stack Example

   <source lang="html4strict">

<HTML> <HEAD> <TITLE> Simple Stack Example </TITLE> </HEAD> <BODY>

<SCRIPT> var stack = new Array(); stack.push("Me", "Two"); for (var i = 0; i < stack.length; i++){ document.write(stack[i] + "
"); } document.write (stack.pop() + "
"); for (var i = 0; i < stack.length; i++){ document.write(stack[i] + "
"); } </SCRIPT>

</BODY> </HTML>

      </source>