Skip to content
Battistella Stefano edited this page Apr 24, 2014 · 31 revisions

In computer science, a stack is a particular kind of abstract data type or collection in which the principal (or only) operations on the collection are the addition of an entity to the collection, known as push and removal of an entity, known as pop. The relation between the push and pop operations is such that the stack is a Last-In-First-Out (LIFO) data structure. In a LIFO data structure, the last element added to the structure must be the first one to be removed. This is equivalent to the requirement that, considered as a linear data structure, or more abstractly a sequential collection, the push and pop operations occur only at one end of the structure, referred to as the top of the stack.

Wikipedia

var stackA = new Stack(); //the stack is empty
var stackB = new Stack(0, 1, 2, 3); //the stack contains (from the top) 3, 2, 1, 0

Methods

getIterator()

This method returns an iterator for scanning the stack. The iterator is useful in order to get a full decoupling in your classes. This avoid, to the class that uses the iterator, to know what type of data structure stores the data.

var stack = new Stack();
var it = stack.getIterator();
for(it.first(); !it.isDone(); it.next()) {
  var item = it.getItem();
  //do something
}

The iterator starts from the top of the stack.

Complexity: O(1)

push(item)

This method pushes the item at the top of the stack. The item could be whatever you want.

var stack = new Stack();
stack.push(4); //stack contains (from the top) 4
stack.push(2); //stack contains (from the top) 2, 4

Complexity: O(1)

multiPush(items)

This method pushes the items at the top of the stack. The items could be whatever you want.

var stack = new Stack();
stack.multiPush(4, 2); //stack contains (from the top) 2, 4
stack.multiPush(5, 6); //stack contains (from the top) 6, 5, 2, 4

Complexity: O(n)

pop()

This method pops the item at the top of the stack. The item is returned.

var stack = new Stack();
stack.multiPush(4, 2); //stack contains (from the top) 2, 4
stack.pop(); //2 and stack contains 4

Complexity: O(1)

multiPop(times)

This method pops the first times items at the top of the stack. The items are returned as an array.

var stack = new Stack();
stack.multiPush(6, 8, 4, 2); //stack contains (from the top) 2, 4, 8, 6
stack.multiPop(2); //[2, 4] and stack contains (from the top) 8, 6

Complexity: O(n)

peek()

This method takes the item at the top of the stack without removing it.

var stack = new Stack();
stack.multiPush(6, 8, 4, 2); //stack contains (from the top) 2, 4, 8, 6
stack.peek(); //2 and stack contains (from the top) 2, 4, 8, 6

Complexity: O(1)

clear()

This method empty the stack.

var stack = new Stack();
stack.multiPush(6, 8, 4, 2);
stack.clear(); //the stack is empty

Complexity: O(1)

contains(item, callback)

This method checks if the stack contains an item that satisfy the condition represented by the callback function. The callback could be omitted. In this case the method checks if the item is simply contained in the stack.

var stack = new Stack();
stack.multiPush(6, 8, 4, 2);
stack.contains(4); //true
stack.contains(1); //false

If you desire a more complex research of an item you need to pass also the callback parameter. The callback must accept the item the iteration working with. In that case the item parameter could be omitted because it won't be used to evaluate the condition.

var stack = new Stack();
stack.multiPush(6, 8, 4, 2);
var callback = function(item) { //this function checks if there is a number lower than 5.
  return item < 5;
};
stack.contains(null, callback); //true

In this example we deal with more complex items.

var stack = new Stack();
var itemA = {parent: null; key: 0};
var itemB = {parent: itemA; key: 1};
stack.multiPush(itemA, itemB);
var callback = function(item) { //this function checks if there is an item whose parent is itemA 
  return item.parent = itemA;
};
stack.contains(null, callback); //true

Complexity: O(n)

Clone this wiki locally