Professional Documents
Culture Documents
SECTION 3
DISCUSSION:
Stack is one of the simplest but important data structures used in computer
algorithms. It finds applications in such diverse tasks as pattern recognition, list and
tree traversals, evaluation of expressions, resolving recursion, etc.
Abstractly, we can think of a stack as a linearly ordered set of elements on
which is imposed the discipline of “last-in, first-out”.
The two basic operations defined on a stack (Both operations are allowed at the top
of the stack only.)
a. insertion (push)
b. deletion (pop)
Insertion Deletion
(here only) (here only)
an
an-1
.
.
ai
ai-1
.
.
a2
a1
Section 3 Page 1 of 7
Jennifer Laraya-Llovido
Data Structures & Algorithms
There are two simple ways of implementing the ADT stack as a concrete data
structure, namely:
1. as a sequentially allocated one-dimensional array or vector
2. as a linked linear list
1 2 3 4 5 6 7 8 9 . . . . . . n- n
1
S(1:n) X X X X X X X X X
Deletion
Insertion
To initialize the stack, we set top Å0. It follows from this that the condition
top = 0 means that the stack is empty. The condition top = n on the other hand
means that the stack is full. An attempt to insert onto a full stack results in an
overflow condition; an attempt to delete from an empty stack results in an underflow
condition. Procedures to implement the insertion and deletion operations must test
for the occurrence of these conditions.
Section 3 Page 2 of 7
Jennifer Laraya-Llovido
Data Structures & Algorithms
This is an old problem in Computer Science. The problem has its roots in the
fact that the form in which we have been taught to write arithmetic expressions since
our elementary school days (which is infix) is not the form suitable for the efficient
evaluation of an expression by the computer (which is postfix). Thus, the need to
convert from infix to postfix form.
To put the problem in proper perspective, let us clarify what we mean by the infix
and postfix forms of an expression.
Section 3 Page 3 of 7
Jennifer Laraya-Llovido
Data Structures & Algorithms
Now, the theorem: A postfix expression is well-formed iff the rank of every
proper head is greater than or equal to 1 and the rank of the expression is one.
Shown below are examples of arithmetic expressions in infix form and the
corresponding postfix expressions. It is easy to verify that the expressions are well-
formed and that both forms evaluate to the same quantity.
Section 3 Page 4 of 7
Jennifer Laraya-Llovido
Data Structures & Algorithms
a+b*c/d abc*d / +
a/b ∧ c∧d abcd ∧ ∧/
a*( b + (c + d) / e ) – f abcd + e / + * f –
Even with these few examples we can already deduce some general patterns or
rules. Can you cite any?
1. The order of the operands in both forms is the same, whether or not
parentheses are present in the infix expression.
2. The infix expression contains no parentheses, then the order of the operators
in the postfix expression is according to their priority ( ∧ followed by + or – ).
For operators of the same priority, the order in the infix form is preserved.
3. If the infix expression contains parenthesized subexpressions, Rule 2 applies
for each subexpression.
operand 0 - +1
+ - 1 2 -1
* / 3 4 -1
∧ 6 5 -1
( 7 0 -
Section 3 Page 5 of 7
Jennifer Laraya-Llovido
Data Structures & Algorithms
Section 3 Page 6 of 7
Jennifer Laraya-Llovido
Data Structures & Algorithms
Section 3 Page 7 of 7
Jennifer Laraya-Llovido