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.

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

Clone this wiki locally