StackImpl.htm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <HTML>
  2. <HEAD>
  3. <TITLE>Simple Stack Object Example</TITLE>
  4. <SCRIPT LANGUAGE="JavaScript">
  5. <!-
  6. // This is the constructor for our stack object
  7. // If the "length" instance variable is 0, the stack is empty
  8. function oStack() {
  9. this.top = null;
  10. this.length = 0;
  11. this.isEmpty = oStackIsEmpty;
  12. this.push = oStackPush;
  13. this.pop = oStackPop;
  14. }
  15. // This method tells us if this oStack object is empty
  16. function oStackIsEmpty() {
  17. if (this.length == 0)
  18. return true;
  19. else
  20. return false;
  21. }
  22. // This method pushes a new element onto the top of the stack
  23. function oStackPush( newData) {
  24. // Create the new element
  25. var newElement = new oStackElement();
  26. // Add the data to it
  27. newElement.setData( newData);
  28. // Put the current top in our next field
  29. newElement.setNext( this.top);
  30. // Assign our new element to the top
  31. this.top = newElement;
  32. this.length++;
  33. }
  34. // This method pops the top element off of the stack
  35. function oStackPop() {
  36. // Put old top away for safe keeping
  37. var oldTop = this.top;
  38. // Get the new top
  39. this.top = this.top.getNext();
  40. this.length-;
  41. return oldTop;
  42. }
  43. // This is the constructor for an element in our stack object
  44. // The "data" variable holds our actual data (whatever it may be)
  45. // and the "next" variable holds the oStackElement object that is
  46. // 'underneath' this oStackElement object.
  47. function oStackElement() {
  48. this.data = 0;
  49. this.next = null;
  50. this.setData = oStackElementSetData;
  51. this.getData = oStackElementGetData;
  52. this.setNext = oStackElementSetNext;
  53. this.getNext = oStackElementGetNext;
  54. }
  55. // This method sets the data field of this oStackElement
  56. function oStackElementSetData( newData) {
  57. this.data = newData;
  58. }
  59. // This method returns the data field of this oStackElement
  60. function oStackElementGetData() {
  61. return this.data;
  62. }
  63. // This method sets the next field of this oStackElement
  64. function oStackElementSetNext( newNext) {
  65. this.next = newNext;
  66. }
  67. // This method returns the next field of this oStackElement
  68. function oStackElementGetNext() {
  69. return this.next;
  70. }
  71. // create a new oStack object
  72. var stack1 = new oStack();
  73. // Fill it with something interesting
  74. // In this case, the names of my cats
  75. stack1.push( "Puddin' Head")
  76. stack1.push( "Sunshine")
  77. stack1.push( "Shadow")
  78. stack1.push( "Sassy")
  79. stack1.push( "Toby")
  80. stack1.push( "Sandy")
  81. stack1.push( "Pixie")
  82. stack1.push( "Edi")
  83. stack1.push( "Gizmo")
  84. // Get the number of items in the stack
  85. var numElements = stack1.length;
  86. // Print out the contents of the stack
  87. for (var i = 0; i < numElements; i++)
  88. document.write( stack1.length + " - " + stack1.pop().getData()
  89. + "<br>");
  90. //->
  91. </SCRIPT>
  92. </HEAD>
  93. <BODY>
  94. </BODY>
  95. </HTML>