How to resolve the algorithm Stack step by step in the M2000 Interpreter programming language
How to resolve the algorithm Stack step by step in the M2000 Interpreter programming language
Table of Contents
Problem Statement
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:
Sometimes the last pushed stack element is made accessible for immutable access (for read) or mutable access (for write):
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 re-entrant or recursive subprogram. Stacks are also used to describe a formal computational framework. See stack machine. Many algorithms in pattern matching, compiler construction (e.g. recursive descent parsers), and machine learning (e.g. based on tree traversal) have a natural representation in terms of stacks.
Create a stack supporting the basic operations: push, pop, empty.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Stack step by step in the M2000 Interpreter programming language
Source code in the m2000 programming language
Module Checkit {
a=Stack
Stack a {
Push 100, 200, 300
}
Print StackItem(a, 1)=300
Stack a {
Print StackItem(1)=300
While not empty {
Read N
Print N
}
}
}
Checkit
Module Checkit {
Read a, b
Print a, b
}
\\ add parameters in a FIFO, and this FIFO merged to current stack
Push 100
Checkit 10, 20
Print StackItem(1)=100
Module Checkit {
Read a, b
Print a=20, b=100
}
Checkit 20
Function alfa {
k=0
n=0
while not empty {
k+=number
n++
}
if n=0 then Error "No parameters found"
=k/n
}
Print alfa(1,2,3,4)=2.5
You may also check:How to resolve the algorithm Non-decimal radices/Input step by step in the C# programming language
You may also check:How to resolve the algorithm Loops/While step by step in the AmigaE programming language
You may also check:How to resolve the algorithm Function frequency step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Earliest difference between prime gaps step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Search a list step by step in the Maple programming language