Stack.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*------------------------------------------------------------------------------
  2. * NAME : Stack.js
  3. * PURPOSE : Stack dta structure using java script
  4. * AUTHOR : Prasad P. Khandekar
  5. * CREATED : August 21, 2005
  6. *------------------------------------------------------------------------------
  7. * Copyright (c) 2005. Khan Information Systems. All Rights Reserved
  8. * The contents of this file are subject to the KIS Public License 1.0
  9. * (the "License"); you may not use this file except in compliance with the
  10. * License. You should have received a copy of the KIS Public License along with
  11. * this library; if not, please ask your software vendor to provide one.
  12. *
  13. * YOU AGREE THAT THE PROGRAM IS PROVIDED AS-IS, WITHOUT WARRANTY OF ANY KIND
  14. * (EITHER EXPRESS OR IMPLIED) INCLUDING, WITHOUT LIMITATION, ANY IMPLIED
  15. * WARRANTY OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND ANY
  16. * WARRANTY OF NON INFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE
  22. * PROGRAM, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. *
  24. * See the License for the specific language governing rights and limitations
  25. * under the License.
  26. *-----------------------------------------------------------------------------*/
  27. // Stack object constructor
  28. function Stack()
  29. {
  30. this.arrStack = new Array();
  31. this.intIndex = 0;
  32. this.Size = getSize;
  33. this.IsEmpty = isStackEmpty;
  34. this.Push = pushElement;
  35. this.Pop = popElement;
  36. this.Get = getElement;
  37. this.toString = dumpStack;
  38. }
  39. // Converts stack contents into a comma seperated string
  40. function dumpStack()
  41. {
  42. var intCntr = 0;
  43. var strRet = "";
  44. if (this.intIndex == 0) return null;
  45. for (intCntr = 0; intCntr < this.intIndex; intCntr++)
  46. {
  47. if (strRet.length == 0)
  48. strRet += this.arrStack[intCntr];
  49. else
  50. strRet += "," + this.arrStack[intCntr];
  51. }
  52. return strRet;
  53. }
  54. // Returns size of stack
  55. function getSize()
  56. {
  57. return this.intIndex;
  58. }
  59. // This method tells us if this Stack object is empty
  60. function isStackEmpty()
  61. {
  62. if (this.intIndex == 0)
  63. return true;
  64. else
  65. return false;
  66. }
  67. // This method pushes a new element onto the top of the stack
  68. function pushElement(newData)
  69. {
  70. // Assign our new element to the top
  71. debugAssert ("Pushing " + newData);
  72. this.arrStack[this.intIndex] = newData;
  73. this.intIndex++;
  74. }
  75. // This method pops the top element off of the stack
  76. function popElement()
  77. {
  78. var retVal;
  79. retVal = null;
  80. if (this.intIndex > 0)
  81. {
  82. // Assign our new element to the top
  83. this.intIndex--;
  84. retVal = this.arrStack[this.intIndex];
  85. }
  86. return retVal;
  87. }
  88. // Gets an element at a particular offset from top of the stack
  89. function getElement(intPos)
  90. {
  91. var retVal;
  92. //alert ("Size : " + this.intIndex + ", Index " + intPos);
  93. if (intPos >= 0 && intPos < this.intIndex)
  94. retVal = this.arrStack[this.intIndex - intPos - 1];
  95. return retVal;
  96. }