2. What is a stack?
• In array insertion and deletion can take place at both end, i.e.
start and end,
• But if insertion and deletion are restricted from one end, must
used STACKS and QUEUES.
• Stack is a special type of data structure.
• Stores a set of elements in a particular order
• Compared to a container
• Stack principle: LAST IN FIRST OUT
• = LIFO
• It means: the last element inserted is the first one to
be removed
4. Characteristics of a Stack
Structure
• A stack is a collection of elements, which can be
stored and retrieved one at a time.
• Elements are retrieved in reverse order of their time
of storage, i.e. the latest element stored is the next
element to be retrieved.
• A stack is sometimes referred to as a Last-In-First-
Out (LIFO) or First-In-Last-Out (FILO) structure.
Elements previously stored cannot be retrieved until
the latest element (usually referred to as the 'top'
element) has been retrieved.
5. ▓ Stack Terminologies
▒ Push (inserting An element In Stack)
▒ Pop (Deleting an element in stack)
▒ Top ( The last entered value)
▒ Max Stack ( maximum value, a stack can hold)
▒ Isempty ( either stack has any value or not)
• Isempty()=True-> empty
• Isempty()=False-> contain elements
▒ Overflow (stack is full, elements can’t be pushed
due to absence of space)
6. Operations perform on stack
Primary operations: Push and Pop
Push
◦ Add an element to the top of the stack.
Pop
◦ Remove the element at the top of the stack.
7.
8. Stack-Related Terms
▓ Top
░ A pointer that points the top element in the stack.
▓ Stack Underflow
░ When there is no element in the stack or stack holds
elements less than its capacity, the status of stack is
known as stack underflow.
TOP=NULL
▓ Stack Overflow
░ When the stack contains equal number of elements
as per its capacity and no more elements can be
added, the status of stack is known as stack overflow.
TOP=MAXSTK
9. How the stack routines work
empty stack; push(a), push(b); pop
empty stack
top = 0
a
push(a)
top = 1
b
a
push(b)
top = 2
a
pop()
top = 1
21. Expression evaluation
• An expression is a series of operators and
operands
• Operators: +, -, *, /, %, ^ (exponentiation)
• Operands: the values going to be operated
• Arithmetic expression is made up
– Operands (Numeric Variables or Constants)
– Arithmetic Operators (+, -, *, /)
– Power Operator (^)
– Parentheses
• The Expression is always evaluated from left
to right
22. • Order in which the expression is evaluated is:
– If the expression has parenthesis, then they are
evaluated first
– Exponential (^) is given highest priority
– Multiplication (*) and division (/) have the next
highest priority
– Addition (+) and subtraction (-) have the lowest
priority
• Evaluation of Expression
Steps to evaluate the following expression:
(2^3 + 6) * 2 – 9 / 3
= (8 + 6) * 2 – 9 / 3
= 14 * 2 – 9 / 3
= 28 – 3
= 25
23. • Stacks are useful in evaluation of arithmetic
expressions. Consider the expression
• 5 * 3 +2 + 6 * 4
The expression can be evaluated by first
multiplying 5 and 3, storing the result in
A, adding 2 and A, saving the result in A. We
then multiply 6 and 4 and save the answer in B.
We finish off by adding A and B and leaving the
final answer in A.
• A = 15 2 + = 17
• B = 6 4 * = 24
• A = 17 24 + = 41
24. 5 * 3 +2 + 6 * 4
• We can write this sequence of operations as follows:
5 3 * 2 + 6 4 * +
• This notation is known as postfix notation. We shall shortly
show how this form can be generated using a stack.
Basically there are 3 types of notations for expressions. The
standard form is known as the infix form. The other two are
postfix and prefix forms.
• Infix: The usual form, operator is between operands
• Prefix: the operator is in front of the operand(s)
• Postfix: the operand(s) is(are) in front of the operator
28. Infix to Postfix Conversion
**Convert infix expression
A+B*C+(D*E+F)*G
into postfix
A + B * C + ( D * E + F ) * G
1. Scanned from left to right. First
operand read is A and passed to
output
Stack
Output: A
29. Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
2. Next the ‘+’ operator is read, at this
stage, stack is empty. Therefore no
operators are popped and ‘+’ is
pushed into the stack. Thus the
stack and output will be:
+
Stack
Output: A
30. Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
3. Next the ‘B’ operand is read and
passed to the output. Thus the stack
and output will be:
+
Stack
Output: AB
31. Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
4. Next the ‘*’ operator is read, The
stack has ‘+’ operator which has
lower precedence than the ‘*’
operator. Therefore no operators
are popped and ‘*’ is pushed into
the stack. Thus the stack and output
will be:
*
+
Stack
Output: AB
32. Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
5. Next the ‘C’ operand is read and
passed to the output. Thus the stack
and output will be:
*
+
Stack
Output: ABC
33. Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
6. Next the ‘+’ operator is read, The
stack has ‘*’ operator which has
higher precedence than the ‘+’
operator. The Stack is popped and
passed to output. Next stack has ‘+’
operator which has same
precedence than the ‘+’ operator.
The Stack is popped and passed to
output. Now stack is empty,
therefore no operators are popped
and ‘+’ is pushed into the stack.
Thus the stack and output will be:
+
Stack
Output: ABC*+
34. Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
7. Next the left parenthesis ‘(’ is
read, Since all operators have lower
precedence than the left
parenthesis, it is pushed into the
stack. Thus the stack and output will
be:
(
+
Stack
Output: ABC*+
35. Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
8. Next the ‘D’ operand is read and
passed to the output. Thus the stack
and output will be:
(
+
Stack
Output: ABC*+D
36. Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
9. Next the ‘*’ operator is read. Now,
left parenthesis ‘(‘ has higher
precedence than ‘*’; it can not be
popped from the stack until a right
parenthesis ‘)’ has been read. Thus
the stack is not popped and ‘*’ is
pushed into the stack. Thus the
stack and output will be:
*
(
+
Stack
Output: ABC*+D
37. Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
10.Next the ‘E’ operand is read and
passed to the output. Thus the stack
and output will be:
*
(
+
Stack
Output: ABC*+DE
38. Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
11.Next the ‘+’ operator is read, The
stack has ‘*’ operator which has
higher precedence than the ‘+’
operator. The Stack is popped and
passed to output. Next stack has
left parenthesis ‘(’ which has not
been popped and ‘+’ operator is
pushed into the stack. Thus the
stack and output will be:
+
(
+
Stack
Output: AB*+DE*
39. Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
12.Next the ‘F’ operand is read and
passed to the output. Thus the stack
and output will be:
+
(
+
Stack
Output: ABC*+DE*F
40. Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
13.Next the ‘)’ has encountered now
popped till ‘( ‘ and passed to the
output. Thus the stack and output
will be:
+
Stack
Output: ABC*+DE*F+
41. Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
14.Next the ‘*’ operator is read, The
stack has ‘+’ operator which has
lower precedence than the ‘*’
operator. Therefore no operators
are popped and ‘*’ is pushed into
the stack. Thus the stack and output
will be:
*
+
Stack
Output: ABC*+DE*F+
42. Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
15.Next the ‘G’ operand is read and
passed to the output. Thus the stack
and output will be:
*
+
Stack
Output: ABC*+DE*F+G
43. Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
16.The end of expression is
encountered. The operators are
popped from the stacked and
passed to the output in the same
sequence in which these are
popped. Thus the stack and output
will be:
Stack
Output: ABC*+DE*F+G*+
44. Summary
• A stack is one of the most basic data structures in the study.
• It only has one end for insert and delete.
• An item in a stack is LIFO while it is FIFO in a queue
• Stack is used in many hidden areas of computer systems
• One popular important application is expression evaluation
• An expression can be in three forms: infix, prefix, and
postfix
• It is common to evaluate an expression by using a postfix
format