3. What is Stack?
• A stack is a collection of objects that are inserted and removed
according to the last-in, first-out (LIFO) principle
– A user may insert objects into a stack at any time, but may only access
or remove the most recently inserted object that remains at the so-
called “top” of the stack
5. Stack Applications
• Text editors usually provide an “undo” mechanism that cancels
recent editing operations and reverts to former states of a
document
– This undo operation can be accomplished by keeping text changes in a
stack
7. The Stack Abstract Data Type
• ADT: Abstract Data type is a type for objects whose behavior is
defined by a set of value and a set of operations
• Formally, a stack is an abstract data type (ADT) such that an
instance S supports the following two methods:
– S.push(e): Add element e to the top of stack S
– S.pop(): Remove and return the top element from the stack S; an error
occurs if the stack is empty
8. The Stack Abstract Data Type
• Additionally, let us define the following accessor methods for
convenience:
– S.top(): Return a reference to the top element of stack S, without
removing it; an error occurs if the stack is empty
– S.is_empty( ): Return True if stack S does not contain any elements
– len(S): Return the number of elements in stack S
9. Working of Stack
• A pointer called TOP is used to keep track of the top element
in the stack
• When initializing the stack, we set its value to -1 so that we
can check if the stack is empty by comparing TOP == -1
• On pushing an element, we increase the value of TOP and
place the new element in the position pointed to by TOP
• On popping an element, we return the element pointed to by
TOP and reduce its value.
• Before pushing, we check if the stack is already full
• Before popping, we check if the stack is already empty
11. Stack Operations – push()
• The procedure to perform push is here:
begin procedure push: stack, data
if stack is full
return null
endif
top ← top + 1
stack[top] ← data
end procedure
void push(int data)
{
if(!isFull())
{
top = top + 1;
stack[top] = data;
}
else
{
printf("Stack is full.n");
}
}
12. Stack Operations – pop()
• The procedure to perform pop is here:
begin procedure pop: stack
if stack is empty
return null
endif
data ← stack[top]
top ← top - 1
return data
end procedure
int pop(int data)
{
if(!isempty())
{
data = stack[top];
top = top - 1;
return data;
}
else
{
printf("Stack is empty.n");
}
}
13. Stack Operations – is_empty()
• The procedure to check the emptiness of a stack is here:
begin procedure isempty
if top less than 1
return true
else
return false
endif
end procedure
int isempty()
{
if(top == -1)
return 1;
else
return 0;
}
14. Stack Operations – top()
• The procedure to return the reference to top element is here:
begin procedure peek
return stack[top]
end procedure
int peek()
{
return stack[top];
}
16. Stack Applications in Computer Engineering
• Infix to Postfix Conversion
– The stack can be used to convert some infix expression into its postfix
equivalent, or prefix equivalent
– These postfix or prefix notations are used in computers to express some
expressions
• Expression Evaluation
– We also need the help of stack data structure to evaluate postfix
expressions
• Balancing Symbols
– Frequently a lack of one symbol will cause the compiler to spill out a
hundred lines of diagnostics without identifying the real error
– Stack can be used to balance the symbols such as {}, (), [] etc.
17. Stack Applications in Computer Engineering
• Processing Function Calls
– Stack plays an important role in programs that call several functions in
succession
• Reverse a String
– A Stack can be used to reverse the characters of a string
18. Infix to Postfix Conversion
• Infix Notation
– The infix notation is a convenient way of writing an expression in which each
operator is placed between the operands
• Prefix Notation
– The prefix notation places the operator before the operands (Polish notation)
• Postfix Notation
– The postfix notation places the operator after the operands (Reverse Polish
Notation)
Infix Notation Prefix Notation Postfix Notation
A * B * A B AB*
(A+B)/C /+ ABC AB+C/
(A*B) + (D-C) +*AB - DC AB*DC-+
19. Algorithm
• Step 1 : Scan the Infix Expression from left to right one character at a time.
• Step 2 : If the scanned character is an operand, append it to the output.
• Step 3 : Else, if it is an operator,
– Step 3.1 : If the precedence of the scanned operator is greater than that of the
operator in the stack (or the stack is empty or the stack contains a ‘(‘ or ‘[‘ or ‘{‘),
push it on stack.
– Step 3.2 : Else, Pop all the operators from the stack which are greater than or equal
to in precedence than that of the scanned operator.
• Step 4 : If the scanned character is an ‘(‘ or ‘[‘ or ‘{‘, push it onto the stack
• Step 5 : If the scanned character is an ‘)’or ‘]’ or ‘}’, pop the stack and output it
until a ‘(‘ or ‘[‘ or ‘{‘ respectively is encountered, and discard both the
parenthesis
• Step 6 : Repeat steps 2-6 until infix expression is scanned
• Step 7 : Print the output
• Step 8 : Pop and output from the stack until it is not empty
20. Example – Infix to Postfix Conversion
( A + B / C + D * ( E – F ) ^ G )
Expression
Stack
Output
(
A
+
B
/
C / +
+
D
*
(
E
-
F - ^
G ^ * +
21. Exercise Questions
1. Convert the infix expression ( AX * ( BX * ( ( ( CY + AY ) + BY ) * CX ) )
) to its postfix equivalent using stack.
2. Convert the infix expression ( ( H * ( ( ( ( A + ( ( B + C ) * D ) ) * F ) *
G ) * E ) ) + J ) to its postfix equivalent using stack.
3. Convert the infix expression a + b * ( c ^ d – e ) ^ ( f + g * h ) - i to its
postfix equivalent using stack.
22. Postfix Expression Evaluation
• The expressions written in postfix form are evaluated faster
compared to infix notation
• Algorithm
1. Create a stack to store operands
2. Scan the given expression and do the following for every scanned
element
a. If the element is a number, push it onto the stack
b. If the element is an operator, pop operands for the operator from the
stack. Evaluate the operator and push the result back to the stack
3. When the expression is ended, the number in the stack is the final
answer
25. Balancing Symbols
• Stacks can be used to check if the given expression has
balanced symbols or not
• Algorithm
1. Create a stack
2. While end of input is not reached
a) If the character read is not a symbol to be balanced, ignore it
b) If the character is an opening delimiter like ( , { or [ , PUSH it into the stack
c) If it is a closing symbol like ) , } , ] , then if the stack is empty report an error,
otherwise POP the stack
d) If the symbol POP-ed is not the corresponding delimiter, report an error
3. At the end of the input, if the stack is not empty report an error
26. Example – Balancing Symbols
[ { ( a – b ) * ( c – d ) } / f ]
Expression
Stack
[
{
(
(
27. Processing Function Calls
• Suppose we have a program containing three functions: A, B, and C.
function A invokes function B, which invokes the function C.
main()
{
………………
………………
funA();
return;
}
funA()
{
………………
………………
funB();
return;
}
funB()
{
………………
………………
funC();
return;
}
funC()
{
………………
………………
………………
return;
}
Stack
funA()
funB()
funC()
28. Reversing String
• Procedure
– Create an empty stack.
– One by one push all characters of string to stack.
– One by one pop all characters from stack and put them back to string
C o m p u t e r
String
Stack
C
Output
o
m
p
u
t
e
r
r e t u pm o C