-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.js
More file actions
142 lines (116 loc) · 2.66 KB
/
Copy pathStack.js
File metadata and controls
142 lines (116 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import EmptyStackException from './EmptyStackException'
import List from './List'
/**
* @see http://download.oracle.com/javase/6/docs/api/java/util/Stack.html
*
* @extends {List}
* @constructor
* @private
*/
export default function Stack() {
/**
* @type {Array}
* @private
*/
this.array_ = [];
};
Stack.prototype = new List();
/**
* @override
*/
Stack.prototype.add = function(e) {
this.array_.push(e);
return true;
};
/**
* @override
*/
Stack.prototype.get = function(index) {
if (index < 0 || index >= this.size()) {
throw new IndexOutOfBoundsException();
}
return this.array_[index];
};
/**
* Pushes an item onto the top of this stack.
* @param {Object} e
* @return {Object}
*/
Stack.prototype.push = function(e) {
this.array_.push(e);
return e;
};
/**
* Pushes an item onto the top of this stack.
* @param {Object} e
* @return {Object}
*/
Stack.prototype.pop = function(e) {
if (this.array_.length === 0) {
throw new EmptyStackException();
}
return this.array_.pop();
};
/**
* Looks at the object at the top of this stack without removing it from the
* stack.
* @return {Object}
*/
Stack.prototype.peek = function() {
if (this.array_.length === 0) {
throw new EmptyStackException();
}
return this.array_[this.array_.length - 1];
};
/**
* Tests if this stack is empty.
* @return {boolean} true if and only if this stack contains no items; false
* otherwise.
*/
Stack.prototype.empty = function() {
if (this.array_.length === 0) {
return true;
} else {
return false;
}
};
/**
* @return {boolean}
*/
Stack.prototype.isEmpty = function() {
return this.empty();
};
/**
* Returns the 1-based position where an object is on this stack. If the object
* o occurs as an item in this stack, this method returns the distance from the
* top of the stack of the occurrence nearest the top of the stack; the topmost
* item on the stack is considered to be at distance 1. The equals method is
* used to compare o to the items in this stack.
*
* NOTE: does not currently actually use equals. (=== is used)
*
* @param {Object} o
* @return {number} the 1-based position from the top of the stack where the
* object is located; the return value -1 indicates that the object is
* not on the stack.
*/
Stack.prototype.search = function(o) {
return this.array_.indexOf(o);
};
/**
* @return {number}
* @export
*/
Stack.prototype.size = function() {
return this.array_.length;
};
/**
* @return {Array}
*/
Stack.prototype.toArray = function() {
var array = [];
for (var i = 0, len = this.array_.length; i < len; i++) {
array.push(this.array_[i]);
}
return array;
};