-
Notifications
You must be signed in to change notification settings - Fork 4
Stack
Rohit Aggarwal edited this page Nov 21, 2013
·
6 revisions
| Property | Description |
|---|---|
| top | Current Top of Stack |
| size | Current Size of the Stack |
| values | Array representation of the stored values in the Stack |
| compare | Reference to the function used by the Stack for comparing two stored values |
| Methods | Description |
|---|---|
| push | Pushes the data onto the top of Stack |
| insert | Similar to 'push', it will also push the data onto the top of Stack |
| pop | Pops the data present on the top of Stack |
| remove | Similar to 'pop', it will also pop the data present on the top of Stack |
| search | Searches for a particular data in the Stack |
| isEmpty | Checks whether the Stack is empty or not |
| toString | Provides a string representation of the Stack Object |
| iterator | Provides an iterator for traversing through all the elements in the Stack |
var Stack = require('data-structure-js').Stack;
var stackObj = new Stack();
var i = 0;
var numOfElem = 10;
var stackIterObj;
for (i=0; i<numOfElem; i++) {
stackObj.push(i);
stackObj.insert(i);
}
console.log("Size = " + stackObj.size());
console.log(stackObj.toString());
console.log(stackObj.search(1));
console.log(stackObj.search(11));
stackIterObj = stackObj.iterator();
while (stackIterObj.hasNext()) {
console.log(stackIterObj.next());
}
for(i=0; i<numOfElem; i++) {
stackObj.pop();
stackObj.remove();
}
console.log(stackObj.isEmpty());
console.log(stackObj.toString());Stack([compare])
Parameters
| Type | Description | |
|---|---|---|
| compare (Optional) | Function | To be used for comparing two objects stored in the Stack. |
push(data, [callback])
insert(data, [callback])
Parameters
| Type | Description | |
|---|---|---|
| data | Object | Represents the data to be stored onto the top of Stack |
| callback | Function | To be executed after the push operation has completed successfully |
pop()
Returns
Object representing the data stored on top of stack
isEmpty()
Returns
True/False, depicting whether the Stack is empty or not.
toString()
Returns
A String representation of the Stack Object
search(data)
Parameter
| Type | Description | |
|---|---|---|
| data | Object | Represents the data to be searched in the Stack |
Returns
iterator()
Returns
Object of StackIterator containing the containing the following methods:
| Method | Description |
|---|---|
| hasNext | Checks whether another element exists in the Stack or not |
| next | Provides the next element in the Stack |