Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,5 +1,7 @@
|
|||
{{data structure}}[[Category:Classic CS problems and programs]]
|
||||
A '''stack''' is a container of elements with last in, first out access policy. Sometimes it also called '''LIFO'''. The stack is accessed through its '''top'''. The basic stack operations are:
|
||||
A '''stack''' is a container of elements with last in, first out access policy.
|
||||
Sometimes it also called '''LIFO'''. The stack is accessed through its '''top'''.
|
||||
The basic stack operations are:
|
||||
|
||||
* ''push'' stores a new element onto the stack top;
|
||||
* ''pop'' returns the last pushed stack element, while removing it from the stack;
|
||||
|
|
@ -9,7 +11,12 @@ Sometimes the last pushed stack element is made accessible for immutable access
|
|||
|
||||
* ''top'' (sometimes called ''peek'' to keep with the ''p'' theme) returns the topmost element without modifying the stack.
|
||||
|
||||
Stacks allow a very simple hardware implementation. They are common in almost all processors. In programming stacks are also very popular for their way ('''LIFO''') of resource management, usually memory. Nested scopes of language objects are naturally implemented by a stack (sometimes by multiple stacks). This is a classical way to implement local variables of a reentrant or recursive subprogram. Stacks are also used to describe a formal computational framework. See [[wp:Stack_automaton|stack machine]]. Many algorithms in pattern matching, compiler construction (e.g. [[wp:Recursive_descent|recursive descent parsers]]), and machine learning (e.g. based on [[wp:Tree_traversal|tree traversal]]) have a natural representation in terms of stacks.
|
||||
Stacks allow a very simple hardware implementation.
|
||||
They are common in almost all processors. In programming stacks are also very popular for their way ('''LIFO''') of resource management, usually memory.
|
||||
Nested scopes of language objects are naturally implemented by a stack (sometimes by multiple stacks).
|
||||
This is a classical way to implement local variables of a reentrant or recursive subprogram. Stacks are also used to describe a formal computational framework.
|
||||
See [[wp:Stack_automaton|stack machine]].
|
||||
Many algorithms in pattern matching, compiler construction (e.g. [[wp:Recursive_descent|recursive descent parsers]]), and machine learning (e.g. based on [[wp:Tree_traversal|tree traversal]]) have a natural representation in terms of stacks.
|
||||
|
||||
Create a stack supporting the basic operations: push, pop, empty.
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,3 @@ check_foo! :
|
|||
{foo empty?) {nil} {"not " .} ifte
|
||||
"empty" .
|
||||
cr << }
|
||||
|
||||
Output:
|
||||
foo is not empty
|
||||
foo is empty
|
||||
|
|
|
|||
|
|
@ -4,5 +4,5 @@ function Stack() {
|
|||
this.push = function(element) {this.data.push(element)}
|
||||
this.pop = function() {return this.data.pop()}
|
||||
this.empty = function() {return this.data.length == 0}
|
||||
this.peek = function() {return this.data[0]}
|
||||
this.peek = function() {return this.data[this.data.length - 1]}
|
||||
}
|
||||
|
|
|
|||
24
Task/Stack/JavaScript/stack-3.js
Normal file
24
Task/Stack/JavaScript/stack-3.js
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
function makeStack() {
|
||||
var stack = [];
|
||||
|
||||
var popStack = function () {
|
||||
return stack.pop();
|
||||
};
|
||||
var pushStack = function () {
|
||||
return stack.push.apply(stack, arguments);
|
||||
};
|
||||
var isEmpty = function () {
|
||||
return stack.length === 0;
|
||||
};
|
||||
var peekStack = function () {
|
||||
return stack[stack.length-1];
|
||||
};
|
||||
|
||||
return {
|
||||
pop: popStack,
|
||||
push: pushStack,
|
||||
isEmpty: isEmpty,
|
||||
peek: peekStack,
|
||||
top: peekStack
|
||||
};
|
||||
}
|
||||
6
Task/Stack/Smalltalk/stack.st
Normal file
6
Task/Stack/Smalltalk/stack.st
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
s := Stack new.
|
||||
s push: 1.
|
||||
s push: 2.
|
||||
s push: 3.
|
||||
s pop.
|
||||
s top. "2"
|
||||
Loading…
Add table
Add a link
Reference in a new issue