Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Battistella Stefano edited this pageFeb 1, 2015 ·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

varstackA=newStack();//an empty stackvarstackB=newStack(0,1,2,3);//stackB contains (from the top) 3, 2, 1, 0varstackC=newStack(Range(0,3));//stackC 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.

varstack=newStack();varit=stack.getIterator();for(it.first();!it.isDone();it.next()){varitem=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.

varstack=newStack();stack.push(4);//stack contains (from the top) 4stack.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.

varstack=newStack();stack.multiPush([4,2]);//stack contains (from the top) 2, 4stack.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.

varstack=newStack(4,2);//stack contains (from the top) 2, 4stack.pop();//2 and stack contains 4

Complexity:O(n)

In this case the method require to restore indexes of the array that mantains the items of the stack.

multiPop(times)

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

varstack=newStack(6,8,4,2);//stack contains (from the top) 2, 4, 8, 6stack.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.

varstack=newStack(6,8,4,2);//stack contains (from the top) 2, 4, 8, 6stack.peek();//2 and stack contains (from the top) 2, 4, 8, 6

Complexity:O(1)

clear()

This method empty the stack.

varstack=newStack(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.

varstack=newStack(6,8,4,2);stack.contains(4);//truestack.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 is working on. In that case the item parameter could be omitted because it won't be used to evaluate the condition.

varstack=newStack(6,8,4,2);varcallback=function(item){//this function checks if there is a number lower than 5.returnitem<5;};stack.contains(null,callback);//true

In this example we deal with more complex items.

varitemA={parent:null; key:0};varitemB={parent:itemA; key:1};varstack=newStack(itemA,itemB);varcallback=function(item){//this function checks if there is an item whose parent is itemAreturnitem.parent===itemA;};stack.contains(null,callback);//true

Complexity:O(n)

execute(callback)

This method execute the callback function for each item stored in the stack. The callback must accept the item the iteration is working on. The callback must also return a value to assign to the item.

varstack=newStack(6,8,4,2);varcallback=function(item){//this function returns the square for each itemreturnitem*item;};stack.execute(callback);//stack contains (from the top) 4, 16, 64, 36

In this example we deal with more complex items.

varstack=newStack(0,1);varcallback=function(item){//this function encapsulate each item into an objectreturn{x:item,y:item+2};};stack.execute(callback);//stack contains (from the top) {x: 1, y: 3}, {x: 0, y: 2}

Complexity:O(n)

getItem(index)

This method returns the item at position index starting from the top of the stack. So index 0 will be for the item at the top of the stack, 1 is for the second and so on.

varstack=newStack(6,8,4,2);stack.getItem(0);//2stack.getItem(1);//4

Complexity:O(1)

getLength()

This method returns the length of the stack so the number of items stored.

varstack=newStack();stack.getLength()//0 - the stack is emptystack.multiPush([6,8,4,2]);stack.getLength();//4

Complexity:O(1)

isEmpty()

This method checks if the stack is empty or not.

varstack=newStack();stack.isEmpty()//truestack.multiPush([6,8,4,2]);stack.isEmpty();//false

Complexity:O(1)

filter(callback)

This method returns all the items that satisfies the condition represented by the callback. The callback must accept the item the iteration is working on. The method starts from the top of the stack.

varstack=newStack(6,8,4,2);varcallback=function(item){//this function checks if the item is lower than 4 or greater than 6returnitem<4||item>6;};stack.filter(callback);//[2, 8];

In this example we deal with more complex items.

varitemA={parent:null,item:0};varitemB={parent:itemA,item:1};varitemC={parent:itemB,item:2};varitemD={parent:null,item:3};varstack=newStack(itemA,itemB,itemC,itemD);varcallback=function(item){//this function checks if itemA is in the same hierarchy of the itemwhile(item.parent||item===itemA)item=item.parent;returnitem===itemA;};stack.filter(callback);//[itemC, itemB, itemA]

Complexity:O(n)

indexOf(item, callback)

This method returns the first position of the item that satisfy the condition represented by the callback function. The callback could be omitted. In this case the method returns the first position where the item is stored. If nothing has been found, the method returns -1, otherwise the position relates to the top of the stack.

varstack=newStack(6,8,4,2);stack.indexOf(4);//1stack.indexOf(5);//-1

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 is working on. In that case the item parameter could be omitted because it won't be used to evaluate the condition.

varstack=newStack(6,8,4,2);varcallback=function(item){//this function checks if there is a number lower than 5.returnitem<5;};stack.indexOf(null,callback);//0

In this example we deal with more complex items.

varitemA={parent:null; key:0};varitemB={parent:itemA; key:1};varstack=newStack(itemA,itemB);varcallback=function(item){//this function checks if there is an item whose parent is itemAreturnitem.parent===itemA;};stack.indexOf(null,callback);//0

Complexity:O(n)

lastIndexOf(item, callback)

This method returns the last position of the item that satisfy the condition represented by the callback function. The callback could be omitted. In this case the method returns the last position where the item is stored. If nothing has been found, the method returns -1, otherwise the position is relates to the top of the stack.

varstack=newStack(6,2,4,8,6,8,4,2);stack.lastIndexOf(4);//5stack.lastIndexOf(5);//-1

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 is working on. In that case the item parameter could be omitted because it won't be used to evaluate the condition.

varstack=newStack(6,2,4,8,6,8,4,2);varcallback=function(item){//this function checks if there is a number lower than 5.returnitem<5;};stack.lastIndexOf(null,callback);//6

In this example we deal with more complex items.

varitemA={parent:null; key:0};varitemB={parent:itemA; key:1};varstack=newStack(itemA,itemB,itemB);varcallback=function(item){//this function checks if there is an item whose parent is itemAreturnitem.parent===itemA;};stack.lastIndexOf(null,callback);//1

Complexity:O(n)

allIndexedOf(item, callback)

This method returns all the positions of the items that satisfy the condition represented by the callback function. The callback could be omitted. In this case the method returns all the positions where the item is stored. The positions relate to the top of the stack.

varstack=newStack(6,2,4,8,6,8,4,2);stack.allIndexesOf(4);//[1, 5]stack.allIndexesOf(5);//[]

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 is working on. In that case the item parameter could be omitted because it won't be used to evaluate the condition.

varstack=newStack(6,2,4,8,6,8,4,2);varcallback=function(item){//this function checks if there is a number lower than 5.returnitem<5;};stack.allIndexesOf(null,callback);//[0, 1, 5, 6]

In this example we deal with more complex items.

varitemA={parent:null; key:0};varitemB={parent:itemA; key:1};varstack=newStack(itemA,itemB,itemB);varcallback=function(item){//this function checks if there is an item whose parent is itemAreturnitem.parent===itemA;};stack.allIndexesOf(null,callback);//[0, 1]

Complexity:O(n)

clone()

This method returns a clone of the stack. The items are cloned only if there's a methodclone() to invoke, otherwise they will be shared with the old stack. This problem there is not if items are not object.

varstack=newStack(6,8,4,2);varclone=stack.clone();//clone contains (from the top) 2, 4, 8, 6

Complexity:O(n)

cloneDistinct()

This method returns a clone of the stack containing only one copy of the same item. The items are cloned only if there's a methodcloneDistinct() to invoke or at least a methodclone(), otherwise they will be shared with the old stack. This problem there is not if items are not object.

varstack=newStack(2,8,4,6,8,4,2,6);varclone=stack.cloneDistinct();//clone contains (from the top) 6, 2, 4, 8

Complexity:O(n·m)

m: the number of distinct elements stored in the stack.The time required depends strongly from the number of items duplicated. If the number of items duplicated is very high, then m tend to be near 1 (when the stack contains only one distinct item) so complexity will beO(n). If the number of items duplicated is very low, then m tend to be near n (when the stack doesn't contain any duplicated items) so complexity will beO(n2).

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp