|
| 1 | +//----------------------------------------------------------------- |
| 2 | +// Class Deque |
| 3 | +//----------------------------------------------------------------- |
| 4 | +// Author(s) |
| 5 | +// Nicholas C. Zakas (NCZ), 9/5/02 |
| 6 | +// |
| 7 | +// Description |
| 8 | +// A classic Deque interface. |
| 9 | +//----------------------------------------------------------------- |
| 10 | +function Deque() { |
| 11 | + this.items = new DoubleLinkedList |
| 12 | +} |
| 13 | + |
| 14 | +//----------------------------------------------------------------- |
| 15 | +// Method Deque.getBack() |
| 16 | +//----------------------------------------------------------------- |
| 17 | +// Author(s) |
| 18 | +// Nicholas C. Zakas (NCZ), 9/5/02 |
| 19 | +// |
| 20 | +// Description |
| 21 | +// This method gets off the back item in the Deque and returns it. |
| 22 | +// |
| 23 | +// Arguments |
| 24 | +// (none) |
| 25 | +// |
| 26 | +// Returns |
| 27 | +// The back item. |
| 28 | +//----------------------------------------------------------------- |
| 29 | +Deque.prototype.getBack = function() { |
| 30 | + |
| 31 | + var vItem = null; |
| 32 | + |
| 33 | + if (!this.isEmpty()) { |
| 34 | + |
| 35 | + vItem = this.items.item(this.items.length - 1); |
| 36 | + |
| 37 | + this.items.remove(this.items.length - 1); |
| 38 | + } |
| 39 | + |
| 40 | + return vItem; |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +//----------------------------------------------------------------- |
| 45 | +// Method Deque.getFront() |
| 46 | +//----------------------------------------------------------------- |
| 47 | +// Author(s) |
| 48 | +// Nicholas C. Zakas (NCZ), 9/5/02 |
| 49 | +// |
| 50 | +// Description |
| 51 | +// This method gets the first item in the Deque and returns it. |
| 52 | +// |
| 53 | +// Arguments |
| 54 | +// (none) |
| 55 | +// |
| 56 | +// Returns |
| 57 | +// The first item. |
| 58 | +//----------------------------------------------------------------- |
| 59 | +Deque.prototype.getFront = function() { |
| 60 | + |
| 61 | + var vItem = null; |
| 62 | + |
| 63 | + if (!this.isEmpty()) { |
| 64 | + |
| 65 | + vItem = this.items.item(0); |
| 66 | + |
| 67 | + this.items.remove(0); |
| 68 | + } |
| 69 | + |
| 70 | + return vItem; |
| 71 | +} |
| 72 | + |
| 73 | +//----------------------------------------------------------------- |
| 74 | +// Method Deque.isEmpty() |
| 75 | +//----------------------------------------------------------------- |
| 76 | +// Author(s) |
| 77 | +// Nicholas C. Zakas (NCZ), 9/5/02 |
| 78 | +// |
| 79 | +// Description |
| 80 | +// This method determines if the Deque is empty. |
| 81 | +// |
| 82 | +// Arguments |
| 83 | +// (none) |
| 84 | +// |
| 85 | +// Returns |
| 86 | +// True if the Deque is empty, false if not. |
| 87 | +//----------------------------------------------------------------- |
| 88 | +Deque.prototype.isEmpty = function() { |
| 89 | + return this.items.length == 0; |
| 90 | +} |
| 91 | + |
| 92 | +//----------------------------------------------------------------- |
| 93 | +// Method Deque.putBack() |
| 94 | +//----------------------------------------------------------------- |
| 95 | +// Author(s) |
| 96 | +// Nicholas C. Zakas (NCZ), 9/5/02 |
| 97 | +// |
| 98 | +// Description |
| 99 | +// This method puts the given argument at the back of the Deque. |
| 100 | +// |
| 101 | +// Arguments |
| 102 | +// vItem (variant) - the object to put into the Deque. |
| 103 | +// |
| 104 | +// Returns |
| 105 | +// (nothing) |
| 106 | +//----------------------------------------------------------------- |
| 107 | +Deque.prototype.putBack = function (vItem) { |
| 108 | + this.items.add(vItem); |
| 109 | + return vItem; |
| 110 | +} |
| 111 | + |
| 112 | +//----------------------------------------------------------------- |
| 113 | +// Method Deque.putFront() |
| 114 | +//----------------------------------------------------------------- |
| 115 | +// Author(s) |
| 116 | +// Nicholas C. Zakas (NCZ), 9/5/02 |
| 117 | +// |
| 118 | +// Description |
| 119 | +// This method puts the given argument at the front of the Deque. |
| 120 | +// |
| 121 | +// Arguments |
| 122 | +// oElement (Object) - the object to put into the Deque. |
| 123 | +// |
| 124 | +// Returns |
| 125 | +// (nothing) |
| 126 | +//----------------------------------------------------------------- |
| 127 | +Deque.prototype.putFront = function (vData) { |
| 128 | + var oNode = new DoubleLinkedListNode(vData); |
| 129 | + |
| 130 | + if (this.isEmpty()) { |
| 131 | + this.items.__first__ = oNode; |
| 132 | + this.items.__last__ = oNode; |
| 133 | + } else { |
| 134 | + var oPtr = this.items.__first__; |
| 135 | + this.items.__first__ = oNode; |
| 136 | + oNode.next = oPtr; |
| 137 | + oPtr.prev = oNode; |
| 138 | + } |
| 139 | + |
| 140 | + this.items.length++; |
| 141 | +} |
| 142 | + |
| 143 | +//----------------------------------------------------------------- |
| 144 | +// Method Deque.toString() |
| 145 | +//----------------------------------------------------------------- |
| 146 | +// Author(s) |
| 147 | +// Nicholas C. Zakas (NCZ), 9/5/02 |
| 148 | +// |
| 149 | +// Description |
| 150 | +// This method returns the contents of the Deque as a string. |
| 151 | +// |
| 152 | +// Arguments |
| 153 | +// (none) |
| 154 | +// |
| 155 | +// Returns |
| 156 | +// A String representing the contents of the Deque. |
| 157 | +//----------------------------------------------------------------- |
| 158 | +Deque.prototype.toString = function() { |
| 159 | + return this.items.toString(); |
| 160 | +} |
0 commit comments